Specifying a VPC in a CloudFormation template for an EC2 instance

amazon-cloudformationamazon-vpcamazon-web-services

I'm launching an EC2 instance via a CloudFormation template, however, the specified instance t2.micro requires a VPC.

How do I specify a VPC in the CloudFormation template?

Here's my template:

{
    "Description" : "Single Instance",

    "Resources" : {
        "EC2Instance" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
                "ImageId" : "ami-b73b63a0",
                "InstanceType" : "t2.micro",
                "KeyName" : "my-key",
                "Tags" : [
                    {
                        "Key" : "Name",
                        "Value" : "test"
                    }
                  ]
            }
        }
    }
}

Best Answer

For EC2 Instances, you get to skip specifying the VPC ID and instead, just specify the subnet you want the instance in. From there, the VPC is assumed.

Inside of your "Properties" array, add the following:

"SubnetId" : "subnet-XXXXXXXX"
Related Topic