amazon-web-services - 有没有办法从子网列表中选择在 CloudFormation 中启动 EC2 实例?

标签 amazon-web-services aws-cloudformation

我正在尝试创建一个 CF 模板来启动三个不同的环境,但我希望能够选择在哪个子网中启动 EC2 实例,而无需在模板中对特定子网进行硬编码。然而,在下面的示例中,我收到一个错误,指出 SubnetId 必须是字符串。我想不出任何其他方法来实现这一目标。有什么想法吗?

Parameters:
  EnvironmentType:
    Type: String
    Default: Dev  
    AllowedValues:
      - Dev  
      - Test 
      - Production
    Description: Select Environment Type (Dev, Test, Production)
  SubnetIdList:
    Type: String
    AllowedValues: 
      - Public1
      - Public2
      - Private
    Description: Select a subnet
  
    

Mappings:
  InstanceSize:
    Dev:
      "EC2" : "t3.micro"
    Test:
      "EC2" : "t3.small"
    Production:
      "EC2" : "t3.medium"
  Sub:
    Public1:
      "Subnet" : "subnet-05daa558dc3f65529" #public 1
    Public2:
      "Subnet" : "subnet-0f57bb83e0fc545f4" #public 2
    Private:
      "Subnet" : "subnet-0eb76c49954acc803" #Private
    
    
    

Resources:
  EC2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0080e4c5bc078760e
      InstanceType: !FindInMap [InstanceSize, !Ref EnvironmentType, EC2]
      KeyName: Ashkelon
      SubnetId: [Sub, !Ref SubnetIdList, Subnet]

最佳答案

我认为您的 Ec2 子网参数定义中缺少 !FindInMap。见下文

Resources:
  EC2:
     Type: AWS::EC2::Instance
     Properties:
     ImageId: ami-0080e4c5bc078760e
     InstanceType: !FindInMap [InstanceSize, !Ref EnvironmentType, EC2]
     KeyName: Ashkelon
     SubnetId: !FindInMap [Sub, !Ref SubnetIdList, Subnet]

关于amazon-web-services - 有没有办法从子网列表中选择在 CloudFormation 中启动 EC2 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56586513/

相关文章:

amazon-web-services - AWS Cloudformation - 引用标签

aws-cloudformation - 多次创建cloudformation资源

node.js - AWS lambda找不到lambda层路径

amazon-web-services - Hive 查询抛出异常 - 编译语句 : FAILED: ArrayIndexOutOfBoundsException null 时出错

amazon-web-services - aws s3 ls 递归 grep 扩展 '.mov' 和总大小

amazon-web-services - 如何在 Amazon Linux 实例上安装 Railo?

python - 为什么 AWS Lambda 在将函数传递给装饰器时会超时?

amazon-web-services - 在 AWS 上将 impdp/expdp 与 RDS Oracle 结合使用

amazon-elastic-beanstalk - Cloudformation 模板的 ACM 证书验证失败

amazon-web-services - 基于速率的 WAF 规则的云形成支持