如何在 AWS CloudFormation 中指定子网和 VPC ID? [英] How do I specify subnet and VPC IDs in AWS CloudFormation?
问题描述
我希望我的 CloudFormation 模板使用现有的子网和 VPC.我不想创建新的.
I want my CloudFormation template to use existing subnets and VPCs. I don't want to create new ones.
我如何参数化这些?
当我查看AWS::EC2::VPC
和AWS::EC2::Subnet
的文档时,似乎这些资源仅用于创建新 VPC 和子网.对吗?
When I look at the docs for AWS::EC2::VPC
and AWS::EC2::Subnet
, it seems these resources are only for creating new VPCs and subnets. Is that correct?
我是否应该直接将实例资源指向我希望它使用的现有 VPC 和子网?
Should I just point the instance resource directly to the existing VPC and subnets I want it to use?
例如 - 如果我的模板中有一个实例资源并且我将它直接指向现有子网,如下所示:
For example - if I have an instance resource in my template and I point it directly to an existing subnet, like this:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": {
"Ref": "InstanceType"
},
"SubnetId": {
"Ref": "subnet-abc123"
},
...
我在验证模板时收到此错误:
I get this error when validating the template:
Template contains errors.: Template format error: Unresolved resource dependencies [subnet-abc123] in the Resources block of the template
我尝试使用映射来执行此操作,但仍然出现错误:
I tried to do this with mappings but still getting an error:
"Mappings": {
"SubnetID": {
"TopKey": {
"Default": "subnet-abc123"
}
}
在实例资源中使用这个:
And with this in the instance resource:
"SubnetId": {
"Fn::FindInMap": [
"SubnetID",
{
"Ref": "TopKey"
},
"Default"
]
}
尝试验证时出现此错误:
I get this error when trying to validate:
Template contains errors.: Template format error: Unresolved resource dependencies [TopKey] in the Resources block of the template
推荐答案
如果您希望使用特定的 VPC 和子网,只需插入它们的值:
If you wish to use a specific VPC and subnet, just insert their values:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"SubnetId": "subnet-abc123",
"ImageId": "ami-abcd1234"
}
}
}
子网始终属于 VPC,因此指定子网将自动选择匹配的 VPC.
A subnet always belongs to a VPC, so specifying the subnet will automatically select the matching VPC.
这篇关于如何在 AWS CloudFormation 中指定子网和 VPC ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!