简体   繁体   English

如何通过云形成模板从自定义AMI运行ec2实例

[英]How to run an ec2 instance from a custom ami through cloud formation template

I am new to Cloud Formation 我是云形成的新手

I want to launch an ec2 instance from a custom ami through cloud formation template. 我想通过云形成模板从自定义ami启动ec2实例。 How to do this? 这个怎么做?

It's done the same way you would do it with a community AMI. 与使用社区AMI的方式相同。 Simply pass the ID of your custom AMI to the ImageId property. 只需将自定义AMI的ID传递到ImageId属性即可。

Example: 例:

"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "ImageId" : "<Cusom_AMI_ID>",
    "KeyName" : { "Ref" : "KeyName" },
    "NetworkInterfaces": [ {
      "AssociatePublicIpAddress": "true",
      "DeviceIndex": "0",
      "GroupSet": [{ "Ref" : "myVPCEC2SecurityGroup" }],
      "SubnetId": { "Ref" : "PublicSubnet" }
    } ]
  }
}

All AMIs are specific to region though. 但是,所有AMI都是特定于区域的。 If you want to use that custom in multiple regions, you'll need to copy that custom AMI to the region(s) you want to use it in. 如果要在多个区域中使用该定制,则需要将该定制AMI复制到您要在其中使用的区域。

Source: Copying an AMI 来源: 复制AMI

Following has options to select more than just ami-id. 以下提供的选项不仅可以选择ami-id。 Hope it helps! 希望能帮助到你! Find ami-id part under Mappings. 在映射下找到ami-id部分。

AWSTemplateFormatVersion: '2010-09-09'
Metadata: 
  License: Apache-2.0
Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription: must be the name of an existing EC2 KeyPair.
    Default: <keypairname>
  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t2.micro
    AllowedValues: [t1.micro, t2.nano, t2.micro, t2.small, t2.medium]
    ConstraintDescription: Must be a valid EC2 instance type.

  VPC:
    Description: Select VPC.
    Type: AWS::EC2::VPC::Id
    Default: <vpc-id>
  Subnet:
    Description: Private Subnet to Deploy Docker MFA.
    Type: AWS::EC2::Subnet::Id
    Default: <subnet-id>

  AccessSecurityGroup:
    Description: Security Group That Allows Instance to Instance Access.
    Type: AWS::EC2::SecurityGroup::Id
    Default: <securitygroup-id>

Mappings:
  RegionMap:
    eu-central-1:
      AMI: <ami-id>

Resources:
  EC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref 'InstanceType'
      KeyName: !Ref 'KeyName'
      Tags:
      - Key: Name
        Value: My-Instance
      ImageId: 
        Fn::FindInMap:
        - RegionMap
        - Ref: AWS::Region
        - AMI
      NetworkInterfaces:
      - GroupSet:
        - Ref: AccessSecurityGroup
        AssociatePublicIpAddress: 'true'
        DeviceIndex: '0'
        DeleteOnTermination: 'true'
        SubnetId:
          Ref: Subnet
Outputs:
  InstanceId:
    Description: InstanceId of the newly created EC2 instance
    Value: !Ref 'EC2Instance'
  AZ:
    Description: Availability Zone of the newly created EC2 instance
    Value: !GetAtt [EC2Instance, AvailabilityZone]
  PublicDNS:
    Description: Public DNSName of the newly created EC2 instance
    Value: !GetAtt [EC2Instance, PublicDnsName]
  PublicIP:
    Description: Public IP address of the newly created EC2 instance
    Value: !GetAtt [EC2Instance, PublicIp]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Cloud Formation模板可创建EC2实例的映像? - AWS Cloud Formation template to create an Image of an EC2 Instance? 获取使用云形成模板创建的EC2实例信息 - Getting EC2 instance information created using cloud formation template 是否可以使用Cloud模板从用户获取“ instancecount”并相应地创建ec2实例? - Is it possible to get “instancecount” from user and create ec2 instance accordingly by using Cloud formation template.? 如何使用已经存在的Ec2实例在YML / JSON文件中生成云形成模板 - How to generate a cloud formation template in YML/JSON file with an already exists Ec2 instance AWS云形成:如何自动执行EC2实例克隆/快照 - AWS Cloud Formation : How to automate the EC2 instance cloning / snapshot 从Boto运行私有AMI ec2实例 - Run a private AMI ec2 instance from boto AWC:来自自定义AMI的EC2实例上的2个主机名 - AWC: 2 hostnames on EC2 instance from custom AMI 如何通过API创建我的EC2实例的AMI - How to create AMI of my EC2 instance through API 在从centos 7创建的自定义EC2 AMI上运行用户数据 - Run userdata on custom EC2 AMI created from centos 7 如何从以前的EC2实例的AMI在VPC中启动与EC2实例完全相同的副本 - How to Launch a exact same replica of a EC2 instance in VPC from the AMI of a previous EC2 instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM