【问题标题】:How to launch an Amazon EC2 in a particular VPC in YAML CloudFormation template如何在 YAML CloudFormation 模板中的特定 VPC 中启动 Amazon EC2
【发布时间】:2019-12-30 23:11:07
【问题描述】:

如何使用 CloudFormation 中的 YAML 模板在 VPC 的特定子网中启动 Amazon EC2 实例?

【问题讨论】:

    标签: amazon-web-services amazon-ec2 yaml amazon-cloudformation amazon-vpc


    【解决方案1】:

    如果将来有人访问这个,我可以通过指定以下内容来解决这个问题:AvailabilityZone、SecurityGroupIds(不是 SecurityGroups)和 SubnetId。

    Resources:
        EC2Instance: 
            Properties:
              AvailabilityZone: us-east-1b
              ImageId: ami-Id
              InstanceType: 
                Ref: InstanceType
              KeyName: 
                Ref: KeyName
              Tags:
                  -
                    Key: "Name"
                    Value:
                      Ref: InstanceName
              SecurityGroupIds:
                  - sg-idHere
              SubnetId: subnet-idHere
            Type: "AWS::EC2::Instance" 
    

    确保安全组对您尝试使用的 VPC 可用。 SubnetId 应代表 VPC。

    层次结构: VPC->SubnetID->SecurityGroupId

    【讨论】:

      【解决方案2】:

      这是在新加坡地区创建 ec2 实例的 CF 模板。我刚刚使用了这个模板。如果您在其他地区运行,请更改 ImageId 名称以与您所在地区会面

      ---
      AWSTemplateFormatVersion: '2010-09-09'
      Description: 'VPC with private subnets in two availability zones'
      Parameters:
        PrivateSubnet:
          Description: Private Subnet to Attach NAT Gateway.
          Type: AWS::EC2::Subnet::Id
      
        InstanceType:
          Description: EC2 instance type
          Type: String
          Default: t2.micro
          AllowedValues: [t2.micro, t2.small, t2.medium, t2.large, m3.medium, m3.large,
            m3.xlarge, m3.2xlarge, m4.large, m4.xlarge, m4.2xlarge, m4.4xlarge, m4.10xlarge,
            c4.large, c4.xlarge, c4.2xlarge, c4.4xlarge, c4.8xlarge, c3.large, c3.xlarge,
            c3.2xlarge, c3.4xlarge, c3.8xlarge, r3.large, r3.xlarge, r3.2xlarge, r3.4xlarge,
            r3.8xlarge, i2.xlarge, i2.2xlarge, i2.4xlarge, i2.8xlarge]
          ConstraintDescription: Please choose a valid instance type.
      
        SSHKeyName:
          Description: EC2 instance type
          Type: String
          ConstraintDescription: Please choose a valid KeyName
      
        VolumeSize:
          Description: size of volume
          Type: Number
          Default: 20
          ConstraintDescription: Please choose a valid Number
          AllowedValues: [20, 30, 40, 50]
      
        IOPS:
          Description: total ipos
          Type: Number
          Default: 100
          ConstraintDescription: Please choose a valid Number
          AllowedValues: [100, 200, 500, 1000]
      
        ImageId:
          Type: String
          Description: 'value for region singapore. If you using other version please choose right'
          Default: 'ami-33e4bc49'
      
      
      Resources:
        EC2Example:
          Type: "AWS::EC2::Instance"
          Properties:
            SubnetId: !Ref PrivateSubnet
            ImageId: !Ref ImageId
            InstanceType: !Ref InstanceType
            KeyName: !Ref SSHKeyName
            BlockDeviceMappings:
              -
                DeviceName: /dev/sda1
                Ebs:
                  VolumeType: io1
                  Iops: !Ref IOPS
                  DeleteOnTermination: false
                  VolumeSize: !Ref VolumeSize
      
      Outputs:
        EC2Example:
          Description: 'Ec2 instance EC2Example'
          Value: !Ref EC2Example
          Export:
            Name: !Sub '${AWS::StackName}-EC2Example'
      

      【讨论】:

      • 我可以从描述中看出这是复制/“合作”。 “VPC:两个可用区中的公共子网和私有子网,一个 cloudonaut.io 模板”这可能是这里最好的答案。只是想说现在可以使用参数类型代替字符串。 docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
      • 好的。我将更新描述以使其有意义。感谢您的评论@mutant_city
      【解决方案3】:

      CloudFormation 模板包含一个SubnetId 参数:

      Type: "AWS::EC2::Instance"
      Properties: 
        SubnetId: String
      

      只需插入现有子网的 ID(例如subnet-1234abcd)。

      【讨论】:

      • 嗨 John,谢谢,但它不起作用,我之前尝试过,但我的实例仍然在我的默认 VPC 中启动。
      • 您是否指定了NetworkInterfaces?如果是这样,这将覆盖SubnetId
      猜你喜欢
      • 2017-12-18
      • 1970-01-01
      • 2020-04-19
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 2012-04-23
      • 2021-12-04
      相关资源
      最近更新 更多