How to launch an Amazon EC2 in a particular VPC in YAML CloudFormation template

cloudformation template to launch ec2 instance
cloudformation vpc template json
cloudformation template to launch ec2 instance in vpc
sample cloudformation template to launch ec2 instance
cloudformation get subnets from vpc
sample vpc cloudformation template
aws::ec2::securitygroup
cloudformation ec2 key pair

How can I launch an Amazon EC2 instance in a particular subnet of a VPC using a YAML template in CloudFormation?

Here is the CF template for create a ec2 instance in region singapore. I have just used this template. If you are running in the other region please change ImageId name to met with you region

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

Amazon EC2 template snippets, This article describes how to use AWS CloudFormation to create and manage CloudFormation supports JSON or YAML, and we'll be using the latter. will see in the official documentation is unnecessary use of double and single quotes. So if we run this template through CloudFormation with the stack� Specifies a launch template for an Amazon EC2 instance. A launch template contains the parameters to launch an instance. For more information, see Launching an instance from a launch template in the Amazon Elastic Compute Cloud User Guide. Syntax. To declare this entity in your AWS CloudFormation template, use the following syntax:

The CloudFormation template includes a SubnetId parameter:

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

Simply insert the ID of the existing Subnet (eg subnet-1234abcd).

AWS::EC2::VPC - AWS CloudFormation, If you're building separate VPC templates for each use case, you can make existing and CloudFormation in particular, to create and maintain an AWS VPC . YAML Basics: In YAML, indentation with double spaces indicates hierarchy ( no tabs!) When creating a stack using this template within the AWS� EC2 block device mapping examples Assigning an Amazon EC2 elastic IP using AWS::EC2::EIP snippet Assigning an existing elastic IP to an amazon EC2 instance using AWS::EC2::EIPAssociation snippet Assigning an existing VPC elastic IP to an Amazon EC2 instance using AWS::EC2::EIPAssociation snippet Elastic network interface (ENI) template snippets Amazon EC2 instance resource Amazon EC2 instance

If anyone comes access this in the future, I was able to solve this by specifying the following: AvailabilityZone, SecurityGroupIds (not SecurityGroups), and 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" 

Make sure that the security group is available to the VPC you are trying to use. The SubnetId should represent the VPC.

Hierarchy: VPC->SubnetID->SecurityGroupId

Building a VPC with CloudFormation - Part 1, You must create a VPC in Amazon Web Services (AWS) for Launch the template: You must enter the command on a single line. of the CloudFormation template YAML file that you saved. For more information about how large to make your VPC, see Your VPC and Subnets in the Amazon Virtual Private Cloud User Guide. Syntax. To declare this entity in your AWS CloudFormation template, use the following syntax:

Building a VPC with CloudFormation - Part 2, A VPC is a virtual network inside AWS where you can isolate your setup VPC you can wrap it into our required form using a legacy VPC wrapper: Launch Stack that forwards HTTP, HTTPS and NTP traffic from a single private subnet to the Internet. Example: If you use the vpc-2azs.yaml template, you will need two Nat � The launch template to use to launch the instances. Any parameters that you specify in the AWS CloudFormation template override the same parameters in the launch template. You can specify either the name or ID of a launch template, but not both. Required: No

Installing a cluster on AWS using CloudFormation templates , Contribute to awslabs/aws-cloudformation-templates development by creating Use two subnets for VPC (#116) Use sample AWS CloudFormation templates to learn how to declare specific AWS resources or solve a particular use case. Before launching a template, always review the resources that it will create and� VPC_Instance_With_Association.template: Example of launching and Amazon EC2 instance in a pre-existing Amazon Virtual Private Cloud and associating it with an existing VPC-based Elastic IP Address. Launch Stack: VPC_EC2_Instance_with_EIP_and_Security_Group.template: Launch an Amazon EC2 instance and an associated Elastic IP address in an

VPC, Type: AWS::EC2::VPC::Id Resources: EC2InstanceSG: Type: You can also run the validate-template command below aws cloudformation validate-template -- template-body file://singleec2instance.yaml --region eu-west-1 "Description": " Deploy Single EC2 Linux Instance",� For more information, see CreateLaunchConfiguration in the Amazon EC2 Auto Scaling API Reference and Launch Configurations in the Amazon EC2 Auto Scaling User Guide. Syntax. To declare this entity in your AWS CloudFormation template, use the following syntax:

Comments
  • I can tell this was copied/'collaborated' from the description. "VPC: public and private subnets in two availability zones, a cloudonaut.io template" It's probably the best answer here. Just wanted to say that there are Parameter types that you can put instead of Strings now. docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
  • ok. I will update the description for making sense. Thank you for your comment @mutant_city
  • Hi John, Thank You, But its not working, I've tried this before, and still my instances are launching in my default VPC.
  • Did you specify a NetworkInterfaces? If so, this overrides SubnetId.