不允许为VPC创建EC2实例

Dha*_*777 2 amazon-ec2 amazon-web-services aws-cloudformation amazon-vpc aws-sdk

是否可以将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...
},
Run Code Online (Sandbox Code Playgroud)

在我的参数中,我定义了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."
},  
...
Run Code Online (Sandbox Code Playgroud)

},

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

遇到不受支持的属性VpcId

不允许这样做吗,我找不到任何文档来做到这一点。做这个实验。如果使用模板创建EC2实例,是否总是在默认VPC中创建它?

hel*_*loV 5

VpcId 不支持 Ec2Instance: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" : ""
},
Run Code Online (Sandbox Code Playgroud)