2017-02-03 62 views
2

我對CloudFormation過程相當陌生,現在我正在取得一些進展,但我想將映射關聯到環境參數和區域,並且我在考慮類似於:AWS CloudFormation映射區域之間的不同環境

{ 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Description": "Basic stack", 
    "Parameters": { 

    "EnvironmentType": { 
     "Description": "Production or Development environment", 
     "Type": "String", 
     "AllowedValues": ["Prod", "Dev"], 
     "ConstraintDescription": "Must be an allowed value" 
    } 
    }, 

    "Mappings":{ 
    "VPC": { 
     "Prod": { 
     "us-east-1" : "vpc-12345678", 
     "eu-central-1" : "vpc-abcdefgh", 
     "ap-southeast-1" : "vpc-abcd1234" 
     }, 
     "Dev": { "us-east-1" : "vpc-1234efgh" } 
    } 
    }, 

    "Resources": { 
    "ApplicationSecurityGroup": { 
     "Type": "AWS::EC2::SecurityGroup", 
     "Properties": { 
     "VpcId": { 
      "Fn::FindInMap" : [ 
      "VPC", 
       { "Ref" : "EnvironmentType" }, 
       { "Ref": "AWS::Region" } 
      ] 
     }, 
     "SecurityGroupEgress": [ 
      { 
      "IpProtocol": "tcp", 
      "FromPort": "80", 
      "ToPort": "80", 
      "CidrIp": "0.0.0.0/0" 
      }, 
      { 
      "IpProtocol": "tcp", 
      "FromPort": "443", 
      "ToPort": "443", 
      "CidrIp": "0.0.0.0/0" 
      } 
     ] 
     } 
    } 
    } 
} 

但是,當我嘗試這個時,我得到一個模板格式錯誤'映射屬性名'us-east-1'只能包含字母數字字符。

如何根據環境和地區選擇合適的VPC ID?

回答

2

嘗試反向傳遞給Fn::FindInMap你的兩個映射層(AWS::Region其次EnvironmentType):

{ 
    "AWSTemplateFormatVersion": "2010-09-09", 
    "Description": "Basic stack", 
    "Parameters": { 

    "EnvironmentType": { 
     "Description": "Production or Development environment", 
     "Type": "String", 
     "AllowedValues": ["Prod", "Dev"], 
     "ConstraintDescription": "Must be an allowed value" 
    } 
    }, 

    "Mappings":{ 
    "VPC": { 
     "us-east-1": { 
     "Prod": "vpc-12345678", 
     "Dev": "vpc-1234efgh" 
     }, 
     "eu-central-1": { 
     "Prod": "vpc-abcdefgh" 
     }, 
     "ap-southeast-1": { 
     "Prod": "vpc-abcd1234" 
     } 
    } 
    }, 

    "Resources": { 
    "ApplicationSecurityGroup": { 
     "Type": "AWS::EC2::SecurityGroup", 
     "Properties": { 
     "VpcId": { 
      "Fn::FindInMap" : [ 
      "VPC", 
      { "Ref": "AWS::Region" }, 
      { "Ref" : "EnvironmentType" } 
      ] 
     }, 
     "SecurityGroupEgress": [ 
      { 
      "IpProtocol": "tcp", 
      "FromPort": "80", 
      "ToPort": "80", 
      "CidrIp": "0.0.0.0/0" 
      }, 
      { 
      "IpProtocol": "tcp", 
      "FromPort": "443", 
      "ToPort": "443", 
      "CidrIp": "0.0.0.0/0" 
      } 
     ] 
     } 
    } 
    } 
} 
+0

這個工作十分感謝! –