简体   繁体   中英

Nested Mappings in Cloudformation template

I have a mapping for VPC setup in my cloudformation template which works fine if I have it like

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'] .

However, I want to have different CIDR ranges depending on PROD or NON-PROD environments. 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. 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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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