How to launch an instance in a pre created VPC and it should be provisioned with Public IP as well?

57 Views Asked by At

I am using below cloud formation template to launch an instance in already created VPC. This instance is getting created in required VPC. But I need this instance to be provisioned with Public IP as well. Tried many options and couldn't solve it. Please can some one help me.

 {
 "AWSTemplateFormatVersion": "2010-09-09",
 "Description": "Ec2 block device mapping",
 "Resources": {
   "MyEC2Instance": {
     "Type": "AWS::EC2::Instance",
     "Properties": {
       "ImageId": "ami-0bbd146b",
       "AvailabilityZone": "us-west-2a",
       "SubnetId": "subnet-e1b24b86",
       "SecurityGroupIds": [ "sg-fad0c383" ],
       "KeyName": "TibcoBuild",
       "Tags": [{
         "Key": "Name",
           "Value": "VPCTest"
       }]
     }
   }
}
}
1

There are 1 best solutions below

1
On BEST ANSWER

What did you try? It's actually pretty straightforward and documented here: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html

"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
    "KeyName" : { "Ref" : "KeyName" },
    "NetworkInterfaces": [ {
      "AssociatePublicIpAddress": "true",
      "DeviceIndex": "0",
      "GroupSet": [{ "Ref" : "myVPCEC2SecurityGroup" }],
      "SubnetId": { "Ref" : "PublicSubnet" }
    } ]
  }
}