简体   繁体   English

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

[英]How to use Mappings in AWS CloudFormation templates in nested stacks

Let's consider the following Mappings and FindInMap are used in the same AWS CloudFormation template.让我们考虑在同一 AWS CloudFormation 模板中使用以下MappingsFindInMap They will work.他们会工作的。

Now, consider the VpcIds under Mappings are in the master.yaml template, and I am trying to create the EgressOnlyInternetGateway resource from the nested.yaml template using those Mappings located in the master.yaml template. Now, consider the VpcIds under Mappings are in the master.yaml template, and I am trying to create the EgressOnlyInternetGateway resource from the nested.yaml template using those Mappings located in the master.yaml template.

How can I accomplish this?我怎样才能做到这一点?

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

Update: I am trying to create a resource MyTestNestedSg in MyTestNestedStack ( MyTestNestedStack.yaml ) using the a mapped parameter defined in MyTestMasterStack as showing below.更新:我正在尝试使用MyTestMasterStack中定义的映射参数在MyTestNestedStackMyTestNestedStack.yaml )中创建资源MyTestNestedSg ,如下所示。 I am getting error: Parameter values specified for a template which does not require them , against MyTestNestedStack .我收到错误:针对MyTestNestedStack Parameter values specified for a template which does not require them

How can I resolve this?我该如何解决这个问题?

Please note that the resource MyTestMasterSg under MyTestMasterStack is just for the completeness.请注意, 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::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

You can't do this.你不能这样做。 You have to past resolved mapping values through Parameters to your AWS::CloudFormation::Stack resource.您必须通过参数将解析的映射值传递到您的AWS::CloudFormation::Stack资源。

Nested stacks should be self sufficient and they don't have access to the parent stack's parameters, mappings or resources.嵌套堆栈应该是自给自足的,它们无权访问父堆栈的参数、映射或资源。 They can only work with data that you explicitly pass through Parameters of AWS::CloudFormation::Stack resource.它们只能处理您通过AWS::CloudFormation::Stack资源的Parameters明确传递的数据。

So in parent stack you would have to do:所以在堆栈中你必须这样做:

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

Update更新

Your MyTestNestedStack.yaml is missing Paramters :您的MyTestNestedStack.yaml缺少Paramters

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM