为不允许的 VPC 创建 EC2 实例

Create an EC2 instance for a VPC not allowed

是否可以将 EC2 实例模板的 VPCId 定义为 属性?

我想做的是,

"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...
},

在我的参数中我定义了 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."
},  
...

},

但是当我创建堆栈时(通过 .net api),它回滚并出现错误

Encountered unsupported property VpcId

这不是允许的吗,我找不到任何文档来执行此操作。做这个作为一个实验。如果使用模板创建,EC2 实例是否总是在默认 VPC 中创建?

VpcIdEc2Instance:Properties

中不受支持

使用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" : ""
},

您不能将 VPCId 作为参数,而是可以分配一个 SubnetId(在 VPC 中,您需要 EC2 实例)。