Amazon web services 如何在AWS CloudFormation中指定子网和VPC ID?

Amazon web services 如何在AWS CloudFormation中指定子网和VPC ID?,amazon-web-services,amazon-ec2,amazon-vpc,amazon-cloudformation,Amazon Web Services,Amazon Ec2,Amazon Vpc,Amazon Cloudformation,我希望我的CloudFormation模板使用现有的子网和VPC。我不想创造新的 如何将这些参数化 当我查看AWS::EC2::VPC和AWS::EC2::Subnet的文档时,似乎这些资源仅用于创建新的VPC和子网。对吗 我是否应该将实例资源直接指向我希望它使用的现有VPC和子网 例如,如果我的模板中有一个实例资源,并将其直接指向现有子网,如下所示: { "Resources": { "MyServer": { "Typ

我希望我的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

参数
部分中指定它们,并在
参考资料
部分中引用它们。CF将允许您首先选择专有网络,然后选择子网

  "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" },

如果您希望使用特定的专有网络和子网,只需插入它们的值:

{
  "Resources": {
    "MyServer": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "SubnetId": "subnet-abc123",
        "ImageId": "ami-abcd1234"
      }
    }
}

子网始终属于专有网络,因此指定子网将自动选择匹配的专有网络。

但如何将其指向已存在的专有网络和子网?在您的示例中,我看不出在哪里可以插入现有子网和VPC ID a la VPC-abc123或子网-123456您将在参数中指定它们作为值。如果您在AWS控制台上尝试此操作,那么UI实际上会以HTML形式向您显示选项。只要尝试一下这个解决方案,您就会看到它是如何工作的。