[英]How do I specify subnet and VPC IDs in AWS CloudFormation?
我希望我的 CloudFormation 模板使用现有的子网和 VPC。 我不想创造新的。
我如何参数化这些?
当我查看AWS::EC2::VPC
和AWS::EC2::Subnet
的文档时,这些资源似乎仅用于创建新的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
如果您希望使用特定的 VPC 和 subnet ,只需插入它们的值:
{
"Resources": {
"MyServer": {
"Type": "AWS::EC2::Instance",
"Properties": {
"InstanceType": "t2.micro",
"SubnetId": "subnet-abc123",
"ImageId": "ami-abcd1234"
}
}
}
子网始终属于 VPC,因此指定子网将自动选择匹配的 VPC。
在Parameters
部分指定它们,并在Resources
部分引用它们。 CF 将让您先选择VPC ,然后选择子网。
"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" },
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.