Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 355
Dhanuka777 Avatar asked Sep 06 '25 03:09

Dhanuka777


1 Answers

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" : ""
},
like image 65
helloV Avatar answered Sep 08 '25 19:09

helloV