如何在 AWS CloudFormation 中指定子网和 VPC ID?
How do I specify subnet and VPC IDs in AWS CloudFormation?
我希望我的 CloudFormation 模板使用现有的子网和 VPC。我不想创建新的。
如何参数化这些?
当我查看 AWS::EC2::VPC
和 AWS::EC2::Subnet
的文档时,这些资源似乎仅用于创建 new VPC 和子网。对吗?
我是否应该直接将实例资源指向我希望它使用的现有 VPC 和子网?
例如 - 如果我的模板中有一个实例资源,我将它直接指向一个现有的子网,如下所示:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": {
"Ref": "InstanceType"
},
"SubnetId": {
"Ref": "subnet-abc123"
},
...
验证模板时出现此错误:
Template contains errors.: Template format error: Unresolved resource dependencies [subnet-abc123] in the Resources block of the template
我尝试使用映射来执行此操作,但仍然出现错误:
"Mappings": {
"SubnetID": {
"TopKey": {
"Default": "subnet-abc123"
}
}
在实例资源中使用这个:
"SubnetId": {
"Fn::FindInMap": [
"SubnetID",
{
"Ref": "TopKey"
},
"Default"
]
}
我在尝试验证时遇到此错误:
Template contains errors.: Template format error: Unresolved resource dependencies [TopKey] in the Resources block of the template
在Parameters
部分指定它们,并在Resources
部分引用它们。 CF 会让你先 select VPC 然后 Subnet.
"Parameters" : {
"VpcId" : {
"Type" : "AWS::EC2::VPC::Id",
"Description" : "VPCId of Virtual Private Cloud (VPC).",
"Default" : ""
},
"VpcSubnet": {
"Description" : "SubnetId in VPC",
"Type" : "AWS::EC2::Subnet::Id",
"Default" : ""
},
"Resources" : {
...
"Ec2Instance" : {
"Properties" : {
"SubnetId" : { "Ref" : "VpcSubnet" },
如果您希望使用特定的 VPC 和子网,只需插入它们的值:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"SubnetId": "subnet-abc123",
"ImageId": "ami-abcd1234"
}
}
}
子网始终属于 VPC,因此指定子网将自动 select 匹配的 VPC。
我希望我的 CloudFormation 模板使用现有的子网和 VPC。我不想创建新的。
如何参数化这些?
当我查看 AWS::EC2::VPC
和 AWS::EC2::Subnet
的文档时,这些资源似乎仅用于创建 new VPC 和子网。对吗?
我是否应该直接将实例资源指向我希望它使用的现有 VPC 和子网?
例如 - 如果我的模板中有一个实例资源,我将它直接指向一个现有的子网,如下所示:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": {
"Ref": "InstanceType"
},
"SubnetId": {
"Ref": "subnet-abc123"
},
...
验证模板时出现此错误:
Template contains errors.: Template format error: Unresolved resource dependencies [subnet-abc123] in the Resources block of the template
我尝试使用映射来执行此操作,但仍然出现错误:
"Mappings": {
"SubnetID": {
"TopKey": {
"Default": "subnet-abc123"
}
}
在实例资源中使用这个:
"SubnetId": {
"Fn::FindInMap": [
"SubnetID",
{
"Ref": "TopKey"
},
"Default"
]
}
我在尝试验证时遇到此错误:
Template contains errors.: Template format error: Unresolved resource dependencies [TopKey] in the Resources block of the template
在Parameters
部分指定它们,并在Resources
部分引用它们。 CF 会让你先 select VPC 然后 Subnet.
"Parameters" : {
"VpcId" : {
"Type" : "AWS::EC2::VPC::Id",
"Description" : "VPCId of Virtual Private Cloud (VPC).",
"Default" : ""
},
"VpcSubnet": {
"Description" : "SubnetId in VPC",
"Type" : "AWS::EC2::Subnet::Id",
"Default" : ""
},
"Resources" : {
...
"Ec2Instance" : {
"Properties" : {
"SubnetId" : { "Ref" : "VpcSubnet" },
如果您希望使用特定的 VPC 和子网,只需插入它们的值:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"SubnetId": "subnet-abc123",
"ImageId": "ami-abcd1234"
}
}
}
子网始终属于 VPC,因此指定子网将自动 select 匹配的 VPC。