简体   繁体   English

Cloudformation模板中的嵌套映射

[英]Nested Mappings in Cloudformation template

I have a mapping for VPC setup in my cloudformation template which works fine if I have it like 我在cloudformation模板中有一个VPC设置的映射,如果有的话它可以正常工作

SubnetConfig:
    VPC:
      CIDR: '10.1.0.0/16'
    PublicOne:
      CIDR: '10.1.0.0/22'
    PublicTwo:
      CIDR: '10.1.4.0/22'
    PrivateOne:
      CIDR: '10.1.8.0/22'
    PrivateTwo:
      CIDR: '10.1.12.0/22'

I can get the values using !FindInMap ['SubnetConfig', 'VPC', 'CIDR'] OR !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR'] . 我可以使用!FindInMap ['SubnetConfig', 'VPC', 'CIDR'] OR !FindInMap ['SubnetConfig', 'PublicOne', 'CIDR']

However, I want to have different CIDR ranges depending on PROD or NON-PROD environments. 但是,我想根据PROD或非PROD环境使用不同的CIDR范围。 In this case my mapping would be like: 在这种情况下,我的映射就像:

SubnetConfig:
    PROD:
      VPC:
        CIDR: '10.1.0.0/16'
      PublicOne:
        CIDR: '10.1.0.0/24'
      PublicTwo:
        CIDR: '10.1.1.0/24'
      PrivateOne:
        CIDR: '10.1.2.0/24'
      PrivateTwo:
        CIDR: '10.1.3.0/24'
    NON-PROD:
      VPC:
        CIDR: '10.2.0.0/16'
      PublicOne:
        CIDR: '10.2.0.0/22'
      PublicTwo:
        CIDR: '10.2.4.0/22'
      PrivateOne:
        CIDR: '10.2.8.0/22'
      PrivateTwo:
        CIDR: '10.2.12.0/22'

Ofcourse, cloudformation does not allow this kind of mapping. 当然,cloudformation不允许这种映射。 Is there a way to define this mapping? 有没有一种方法可以定义此映射? I've referred to this post but it doesn't help 我已经提到了这篇文章,但没有帮助

One alternative might be to flatten the mapping one level, eg 一种替代方法是将映射平坦化一个级别,例如

Mappings:
  SubnetConfig:
    PROD:
      VPCCIDR: '10.1.0.0/16'
      PublicOneCIDR: '10.1.0.0/24'
      PublicTwoCIDR: '10.1.1.0/24'
      PrivateOneCIDR: '10.1.2.0/24'
      PrivateTwoCIDR: '10.1.3.0/24'
    NON-PROD:
      VPCCIDR: '10.2.0.0/16'
      PublicOneCIDR: '10.2.0.0/22'
      PublicTwoCIDR: '10.2.4.0/22'
      PrivateOneCIDR: '10.2.8.0/22'
      PrivateTwoCIDR: '10.2.12.0/22'

One approach can be that you can create 2 properties files in JSON format, 1 having the range for PROD and other for NON PROD. 一种方法是您可以创建2个JSON格式的属性文件,其中1个具有PROD范围,另一个具有NON PROD范围。 Then probably you can have your code read the property file based on the environment where it is getting deployed, pick the suitable file, read the values and pass them to your CF Template while it is getting deployed. 然后,可能可以让您的代码根据要部署的环境读取属性文件,选择合适的文件,读取值,然后在部署时将其传递给CF模板。 So instead of hard coding the IP ranges in template you can make them parameterized and read from the input params that you are passing to your template from code. 因此,与其对模板中的IP范围进行硬编码,还可以对其进行参数化,并从要从代码传递给模板的输入参数中读取它们。

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

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