微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在嵌套堆栈中的 AWS CloudFormation 模板中使用映射

如何解决如何在嵌套堆栈中的 AWS CloudFormation 模板中使用映射

让我们考虑在同一 AWS CloudFormation 模板中使用以下 MappingsFindInMap。他们会工作的。

现在,考虑 VpcIds 下的 Mappings 位于 master.yaml 模板中,我正在尝试使用 EgressOnlyInternetGateway 模板从 nested.yaml 模板创建 Mappings 资源那些位于 master.yaml 模板中的 # master.yaml Mappings: VpcIds: us-east-1: "123456789012": "vpc-00011122233344455" "234567890123": "vpc-11122233344455566" us-west-1: "123456789012": "vpc-22233344455566677" "234567890123": "vpc-33344455566677788" # nested.yaml Resources: EgressOnlyInternetGateway: Type: AWS::EC2::EgressOnlyInternetGateway Properties: VpcId: !FindInMap [VpcIds,!Ref "AWS::Region",!Ref "AWS::AccountId"]

我怎样才能做到这一点?

MyTestnestedSg

更新:我正在尝试使用 MyTestnestedStack 中定义的映射参数在 MyTestnestedStack.yaml (MyTestMasterStack) 中创建资源 Parameter values specified for a template which does not require them显示如下。我收到错误MyTestnestedStack,针对 MyTestMasterSg

我该如何解决这个问题?

请注意,MyTestMasterStack 下的资源 # MyTestMasterStack.yaml Mappings: VpcIds: us-east-1: "123456789012": "vpc-00011122233344455" "234567890123": "vpc-11122233344455566" Resources: MyTestnestedStack: Type: AWS::CloudFormation::Stack Properties: Parameters: VpcId: !FindInMap [VpcIds,!Ref "AWS::AccountId"] TemplateURL: "https://s3.amazonaws.com/my_template_bucket_name/MyTestnestedStack.yaml" TimeoutInMinutes: 60 MyTestMasterSg: Type: AWS::EC2::SecurityGroup Properties: VpcId: "vpc-017a12485ad93e94a" GroupDescription: Testing resource creation wtih Mappings from the parent Stack GroupName: MyTestMasterSg SecurityGroupIngress: - CidrIp: 10.1.0.0/16 FromPort: 80 IpProtocol: tcp ToPort: 80 # MyTestnestedStack.yaml Resources: MyTestnestedSg: Type: AWS::EC2::SecurityGroup Properties: VpcId: !Ref VpcId GroupDescription: Testing resource creation wtih Mappings from the parent Stack GroupName: MyTestnestedSg SecurityGroupIngress: - CidrIp: 10.1.0.0/16 FromPort: 8080 IpProtocol: tcp ToPort: 8080 只是为了完整性。

blockRequests()

解决方法

你不能这样做。您必须通过 Parameters 将解析的映射值传递到您的 AWS::CloudFormation::Stack 资源。

嵌套堆栈应该是自给自足的,它们无权访问父堆栈的参数、映射或资源。它们只能处理您明确通过 ParametersAWS::CloudFormation::Stack 资源传递的数据。

因此在堆栈中,您必须执行以下操作:

MyNestedStack:
  Type: AWS::CloudFormation::Stack
  Properties: 
    Parameters: 
      VpcId : !FindInMap [VpcIds,!Ref "AWS::Region",!Ref "AWS::AccountId"]
  TemplateURL: String

更新

您的 MyTestNestedStack.yaml 丢失了 Paramters

Parameters:
  
  VpcId:
    Type: AWS::EC2::VPC::Id

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。