amazon-web-services - AWS CloudFormation 映射区域之间的不同环境

标签 amazon-web-services aws-cloudformation

我对 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?

最佳答案

尝试反转传递给 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"
          }
        ]
      }
    }
  }
}

关于amazon-web-services - AWS CloudFormation 映射区域之间的不同环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42031891/

相关文章:

amazon-web-services - 如何解压旧的 Amazon AMI?

amazon-web-services - 导入地形 aws_iam_policy

amazon-web-services - 具有代理设置的 AWS API Gateway 自定义授权方 - 添加自定义 header 到请求

python - s3 : what is the 'key' parameter of the s3. put_object() 方法?

amazon-web-services - Cloudformation - 对 4 个 cfn 帮助程序脚本之间的交互感到困惑..这就是我所做的并且它有效

amazon-web-services - AWS CloudFormation StacksSets 是否支持创建像 CloudFormation Stacks 这样的快速链接?

amazon-web-services - CloudFormation - 从存储库获取模板

node.js - 在 AWS EC2 上部署 https node express 服务器托管网站

amazon-web-services - 如何在 CloudFormation 中正确外部化 SecurityGroupEgress 和 SecurityGroupIngress

amazon-web-services - 为什么我的 sqs 访问策略语句被覆盖而不是添加另一个策略?