【问题标题】:How can I add an EC2 instance automatically to existing security group, elastic IP and VPC?如何将 EC2 实例自动添加到现有安全组、弹性 IP 和 VPC?
【发布时间】:2017-04-13 15:35:00
【问题描述】:

我从我的图像 (AMI) 中为新实例创建了新模板 (*.json- see ettach)。

如何将实例自动添加到现有的安全组、弹性ip和VPC?

谢谢

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Ec2 block device mapping",
"Resources": {
    "MyEC2Instance": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-1ff5111",
            "AvailabilityZone": "us-west-1a",
            "KeyName": "Test",
            "Tags": [{
                "Key": "Name",
                "Value": "RoiTest"
            }]
        }
    },

【问题讨论】:

  • 请帮帮我,谢谢

标签: cloud amazon-cloudformation


【解决方案1】:

CloudFormation User Guide 的Resource Types Reference section 是搜索详细信息(例如您要询问的内容)的良好起点。具体来说,您应该查看AWS:EC2:Instance 和AWS::EC2::EIPAssociation 引用。

要将 EC2 实例与 VPC 安全组关联,请添加 SecurityGroupIds 属性。要在 VPC 中创建实例,您实际上必须定义其子网(该子网又与 VPC 关联),因此您添加了 SubnetId 属性。最后,将弹性 IP 关联到您创建 EIP 关联资源的实例。

这是您的模板的外观:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "Ec2 block device mapping",
  "Resources": {
    "MyEC2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": "ami-1ff5111",
        "AvailabilityZone": "us-west-1a",
        "SubnetId": "<your existing subnet id here>",
        "SecurityGroupIds": [ "<your existing security group id here>" ],
        "KeyName": "Test",
        "Tags": [{
          "Key": "Name",
            "Value": "RoiTest"
        }]
      }
    },
    "MyEIPAssociation": {
      "Type": "AWS::EC2::EIPAssociation",
      "Properties": {
        "AllocationId": "<your existing elastic IP allocation id here>",
        "InstanceId": { "Ref": "MyEC2Instance" }
      }
    },
    ... (other resources in your template)
  }
}

指出 SecurityGroupIds 属性值是一个数组可能很有用,因此您可以拥有一个具有多个安全组的实例。

【讨论】:

  • 谢谢!它部分工作。我仍然无法关联现有的安全组准备在代码中哪里写?
  • 如果您在 VPC 中创建 EC2 实例,它只能与同一 VPC 中的安全组关联。您确定您现有的安全组是一个 VPC 安全组,并且它与您在其中创建 EC2 实例的同一 VPC 相关联吗?
  • 首先,谢谢。我没有为 vpc 创建 ec2 实例。我该怎么做?,我需要在我的 json 模板中写什么?当然我还需要安全组。现在'如果我启动我的模板,我有新实例但没有关联到任何 vpc' 安全组。请帮助我:)
猜你喜欢
  • 1970-01-01
  • 2020-03-27
  • 2013-08-03
  • 2017-10-25
  • 2021-05-29
  • 2018-04-26
  • 1970-01-01
  • 2013-11-29
  • 2012-06-16
相关资源
最近更新 更多