0

是否可以将EC2实例模板的VPCId定义为属性?为VPC创建EC2实例不允许

我试图做的是一样的东西,

"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),它回滚出错

遇到不支持的属性VpcId

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

回答

3

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" : "" 
}, 
+0

对于酒店?我已经尝试过并更新了这个问题。 – Dhanuka777

+0

@ Dhanuka777,对不起,我没有正确阅读你的Q. – helloV

1

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

+0

这就是我的答案所说的。 – helloV