如何在cloudformation中指定子网和VPC ID? [英] How do I specify the subnet and VPC IDs in cloudformation?

查看:279
本文介绍了如何在cloudformation中指定子网和VPC ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想创建新的子网和VPC,我已经创建了它们,并且希望我的cloudformation模板使用它们。

I don't want to create new subnets and VPCs I already have them created and I want my cloudformation template to use them.

我在什么参数中指定这是我还是对它的工作方式感到困惑?

In what parameter do I specify this or I'm I confused as to how this works?

当我查看 AWS :: EC2 :: VPC和 AWS :: EC2的文档时::: Subnet,看来这些资源仅用于创建 new 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 this correct?

我应该直接将实例资源指向吗?

Should I just point the instance resource directly to the existing VPC and subnet 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. I have this in mappings:

  "Mappings": {
    "SubnetID": {
      "TopKey": {
        "Default": "subnet-abc123"
      }
    }

这在实例资源中:

"SubnetId": {
  "Fn::FindInMap": [
    "SubnetID",
    {
      "Ref": "TopKey"
    },
    "Default"
  ]
}

在尝试验证时出现此错误:

And 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.

这篇关于如何在cloudformation中指定子网和VPC ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆