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

How to use Mappings in AWS CloudFormation templates in nested stacks

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

现在,考虑 Mappings 下的 VpcIdsmaster.yaml 模板中,我正在尝试从 nested.yaml 创建 EgressOnlyInternetGateway 资源使用位于 master.yaml 模板中的那些 Mappings 的模板。

我怎样才能做到这一点?

# 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"]

更新: 我正在尝试使用 MyTestNestedStack (MyTestNestedStack.yaml) 中定义的映射参数在 MyTestNestedSg 中创建资源 [= =24=] 如下图所示。我收到错误:Parameter values specified for a template which does not require them,反对 MyTestNestedStack

我该如何解决这个问题?

请注意 MyTestMasterStack 下的资源 MyTestMasterSg 仅供参考。

# 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::Region", !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

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

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

所以在 parent 堆栈中你必须做:

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