【问题标题】:CloudFormation Transform::Include parametersCloudFormation 转换::包含参数
【发布时间】:2019-05-30 04:33:16
【问题描述】:

我想为我的文件使用带有一些动态参数的 AWS 宏 Transform::Include。

Resources:
  'Fn::Transform':
    Name: 'AWS::Include'
    Parameters:
      TestMacroVariable: 
        Default: 2
        Type: Number
      Location: !Sub "s3://${InstallBucketName}/test.yaml"

test.yaml:

DataAutoScalingGroup:
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties:
    LaunchConfigurationName: 
      Ref: DataLaunchConfiguration
    MinSize: '1'
    MaxSize: '100'
    DesiredCapacity: 
      Ref: TestMacroVariable
...

致电后:aws cloudformation describe-stack-events --stack-name $stack

我明白了:

"ResourceStatusReason": "参数TestMacroVariable的值 在转换下 Include 必须解析为字符串、数字、布尔值或 这些列表中的任何一个.. 用户请求回滚。”


当我尝试这样做时:

Resources:
  'Fn::Transform':
    Name: 'AWS::Include'
    Parameters:
      TestMacroVariable: 2
      Location: !Sub "s3://${InstallBucketName}/test.yaml"

我明白了:

"ResourceStatusReason": "模板格式错误:未解析的资源 资源块中的依赖项 [TestMacroVariable] 模板”

当我根本不提供 TestMacroVariable 时,错误也是一样的。


尝试了不同的类型:字符串、数字、布尔值、列表 - 它们都不起作用。

【问题讨论】:

    标签: amazon-ec2 amazon-cloudformation


    【解决方案1】:

    基于@BatteryAcid 所说,您可以使用 Sub 函数直接从文件中引用 Cloudformation 模板中的参数:

    在您的 CF 模板中:

    Parameters:
      TableName:
        Type: String
        Description: Table Name of the Dynamo DB Users table
        Default: 'Users'
    

    在您包含的文件中:

         "Resource": [
          {
            "Fn::Sub": [
              "arn:${AWS::Partition}:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${tableName}",
              {
                "tableName": {
                  "Ref": "TableName"
                }
              }
            ]
          }
    

    或者不必是参数,而是模板中的任何资源:

    Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${QueryTelemtryFunction.Arn}/invocations
    

    【讨论】:

      【解决方案2】:

      作为替代方案,您可以将整个 S3 路径作为参数传入并在Location 中引用它:

      Parameters:
        MyS3Path:
          Type: String
          Default: 's3://my-cf-templates/my-include.yaml'  
      
      ...
      
      'Fn::Transform':
        Name: 'AWS::Include'
        Parameters:
          Location: !Ref MyS3Path
      
      

      【讨论】:

        【解决方案3】:

        据我所知,AWS::IncludeParameters 部分中除了 Location 键之外不能有任何其他内容。在这里查看AWS DOC

        【讨论】:

        • Location 是该文档中列出的唯一参数,但您可以添加文档所需的任何其他参数。例如,如果您包含一个需要两个参数 XY 的用户数据文档,则可以在 AWS::IncludeParameters 部分中定义 XY
        猜你喜欢
        • 2019-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 2018-11-09
        • 2017-01-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多