简体   繁体   中英

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. How to do this?

It's done the same way you would do it with a community AMI. Simply pass the ID of your custom AMI to the ImageId property.

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. 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.

Source: Copying an AMI

Following has options to select more than just ami-id. Hope it helps! Find ami-id part under Mappings.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM