Dhanuka777
Dhanuka777

Reputation: 8616

Create an EC2 instance for a VPC not allowed

Is it possible to define VPCId for an EC2 instance template as a property?

I am trying to do is something like,

"Resources" : {
"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : { 
    "SecurityGroups": [ { "Ref": "AWSSecurityGroups" } ],     
    "KeyName" : { "Ref" : "KeyName" },
    "InstanceType" : { "Ref" : "InstanceType" },
    "Tags" : [ { "Key" : "Name", "Value" : "Softnas-CF" }],
    "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
    "VpcId" :  { "Ref" : "VPCId" },     
   .....some other stuff...
},

In my parameters I define VPCId,

"Parameters" : {
....
"VPCId": {
   "Description": "Name of an existing VPC ID",
   "Type": "AWS::EC2::VPC::Id",
   "ConstraintDescription": "must be the name of an existing VPC Id."
},  
...

},

But when I creating the stack (via the .net api), it Rollback with the error

Encountered unsupported property VpcId

Isn't this allowed, I couldn't find any documentation to do this. Doing this as an experiment. Is the EC2 instance always gets created in the default VPC if created using templates?

Upvotes: 4

Views: 3338

Answers (2)

Dhanuka777
Dhanuka777

Reputation: 8616

You cannot give a VPCId as a parameter, instead you can assign a SubnetId (in the VPC you need the EC2 instance to be).

Upvotes: 2

helloV
helloV

Reputation: 52393

VpcId is not supported in Ec2Instance:Properties

Use SubnetId.

"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "SecurityGroupIds" : [ { "Ref" : "xxxxx" } ],
    "Tags" : [ { "Key" : "Name", "Value" : "xxx" } ],
    "ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
    "SubnetId" : { "Ref" : "VpcSubnet" },
    "InstanceType"   : { "Ref" : "InstanceType" },
    ....

"VpcSubnet": {
  "Description" : "Enter the VPC subnet",
  "Type" : "AWS::EC2::Subnet::Id",
  "Default" : ""
},

Upvotes: 7

Related Questions