我是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"
}
]
}
}
}
}
但是当我尝试这个时,我得到一个模板格式错误'Mappings属性名'us-east-1'必须只包含字母数字字符。'
如何根据环境和区域选择正确的VPC ID?
答案 0 :(得分:4)
尝试颠倒传递给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"
}
]
}
}
}
}