aws cloudformation how to use a string parameter to prevent repetitive use of same string?

To reuse this template you should rename the resource named HelloWorldApi to something more generic.

If you currently renamed the resource and attempted to redeploy it would remove any resources that are associated with the HelloWorldApi API as well as the API itself whilst redeploying the API again.

The string you’re using is referencing the AWS::ApiGateway::RestApi resource not the value in the APIName parameter. If you updated this parameter value at the moment it would not affect the stack as it appears as though it is not used.

In summary the referencing of the string HelloWorldApi in Resources is referring to the AWS::ApiGateway::RestApi resources logical name.

Ensure the resources do not share the same name as the parameters.

The template would look like the below

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Parameters": {
      "RestAPIName": {
        "Type": "String",
        "Default": "HelloWorldApi"
      }
    },
    "Resources": {
      "RestAPI": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": { "Ref": "RestAPIName" },
          "Description": "API used for practice",
          "FailOnWarnings": true
        }
      },
      "APIAuthorizer" :{
        "Type" : "AWS::ApiGateway::Authorizer",
        "Properties" : {
            "RestApiId" : {
              "Ref": "RestAPI"
            }
          }
      },
      "BannerDBModel": {
          "Type" : "AWS::ApiGateway::Model",
          "Properties" : {
              "Name" : "postBannerModel",
              "RestApiId" : {
                "Ref": "RestAPI"
              },
              "Schema" : {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "ProductsInputModel",
                "type": "object",
                "properties": {
                    "url": {"type": "string"}
                }                
              }
           }
      }
    }
  }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top