首页 > 解决方案 > Aws cloudformation - 如何使用字符串参数来防止重复使用相同的字符串

问题描述

1.
在我的代码中,字符串“HelloWorldApi”使用很多作为参考。
是否可以使用我定义的参数“APIName”来替换重复字符串“HelloWorldApi”,
以便在创建另一个堆栈并重命名 API 时不需要逐个更新这些字符串?

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Parameters": {
      "APIName": {
        "Type": "String",
        "Default": "HelloWorldApi"
      }
    },
    "Resources": {
      "HelloWorldApi": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": "hello-api",
          "Description": "API used for practice",
          "FailOnWarnings": true
        }
      },
      "APIAuthorizer" :{
        "Type" : "AWS::ApiGateway::Authorizer",
        "Properties" : {
            "RestApiId" : {
              "Ref": "HelloWorldApi"
            }
          }
      },
      "BannerDBModel": {
          "Type" : "AWS::ApiGateway::Model",
          "Properties" : {
              "Name" : "postBannerModel",
              "RestApiId" : {
                "Ref": "HelloWorldApi"
              },
              "Schema" : {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "title": "ProductsInputModel",
                "type": "object",
                "properties": {
                    "url": {"type": "string"}
                }                
              }
           }
      },
      "PostRequestValidator": {
        "Type" : "AWS::ApiGateway::RequestValidator",
        "Properties" : {
            "Name" : "PostRequestValidator",
            "RestApiId" : {
                "Ref": "HelloWorldApi"
            }
        }
      },
      "BannerResource": {
        "Type": "AWS::ApiGateway::Resource",
        "Properties": {
            "RestApiId": {
                "Ref": "HelloWorldApi"
            },
            "ParentId": {
                "Fn::GetAtt": [
                    "HelloWorldApi",
                    "RootResourceId"
                ]
            },
            "PathPart": "banner"
        }
      },
      "getBannerMethod": {
        "Type": "AWS::ApiGateway::Method",
        "DependsOn": ["HelloWorldApi"],
        "Properties": {
          "RestApiId": {
            "Ref": "HelloWorldApi"
          },
          "ResourceId": {
            "Ref": "BannerResource"
          },
          "HttpMethod": "GET",
          "AuthorizationType": "NONE"
        }
      },
      "Deployment": {
          "DependsOn": ["HelloWorldApi"],
          "Type": "AWS::ApiGateway::Deployment",
          "Properties": {
            "RestApiId": {
              "Ref": "HelloWorldApi"
            }
          }
      }
    }
  }

2.
假设我已经通过下面的代码创建了一个堆栈,如果APIName的默认值不小心设置为与原始版本不同的HelloWorld,我运行updateStack后会立即显示错误吗?
那时会发生什么?正在创建另一个 API HelloWorld?

根据评论更新代码

{
    "AWSTemplateFormatVersion": "2010-09-09",
    
    "Parameters": {
      "RestAPI": {
        "Type": "String",
        "Default": "HelloWorldApi"
      }
    },
    "Resources": {
      "RestAPI": {
        "Type": "AWS::ApiGateway::RestApi",
        "Properties": {
          "Name": "hello-api",
          "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"}
                }                
              }
           }
      }
    }
  }

标签: amazon-web-servicesamazon-cloudformation

解决方案


要重用此模板,您应该将命名资源重命名HelloWorldApi为更通用的名称。

如果您当前重命名资源并尝试重新部署,它将删除与HelloWorldApiAPI 关联的所有资源以及 API 本身,同时再次重新部署 API。

您使用的字符串是引用AWS::ApiGateway::RestApi资源而不是参数中的值APIName。如果您此时更新此参数值,它不会影响堆栈,因为它看起来好像没有使用。

总之,字符串的引用HelloWorldApiResourcesAWS::ApiGateway::RestApi资源逻辑名称。

确保资源不与参数同名。

模板如下所示

{
    "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"}
                }                
              }
           }
      }
    }
  }

推荐阅读