繁体   English   中英

使用 Terraform 将 AWS VPC 附加到 IGW

[英]Attaching an AWS VPC to an IGW with Terraform

我正在尝试使用 Terraform 构建 VPC 资源以提供 MWAA 构建。 在 AWS 文档中,我看到以下资源(除了子网等)被定义为创建一个完整的 VPC 环境。 我已经使用 Terraform 定义aws_vpcaws_internet_gateway ,但找不到InternetGatewayAttachment的 Terraform 模板 -适用于aws_vpn_gateway_attachment

  1. 如何将 VPC 资源附加到带 Terraform 的 IGW?
  2. 我需要一个资源,还是在 TF aws_internet_gateway 资源定义中隐含 vpc_id?

PS - 我来自 GCP,对 AWS 网络概念不太熟悉。

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: !Ref VpcCIDR
      EnableDnsSupport: true
      EnableDnsHostnames: true
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Ref EnvironmentName

  InternetGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      InternetGatewayId: !Ref InternetGateway
      VpcId: !Ref VPC

....

1 创建 VPC

2 创建互联网网关

资源“aws_vpc”“my_vpc”{cidr_block =“10.0.0.0/16”}

资源“aws_internet_gateway”“gw”{vpc_id = aws_vpc.my_vpc.id}

在互联网网关中,您可以提供 vpc 的名称。

如果您查看官方文档,您会看到 Internet 网关资源要求您指定 VPC ID。 Terraform 不支持在不立即将其连接到 VPC 的情况下创建 Internet 网关。

如何将 VPC 资源附加到带 Terraform 的 IGW?

你有两个选择——一个是X-Men 的答案显示的,即使用aws_vpcaws_internet_gateway

还有另一个选项可以稍后使用单独的aws_internet_gateway_attachment资源进行附加。

我需要一个资源,还是在 TF aws_internet_gateway 资源定义中隐含 vpc_id?

您不需要资源,它是从 Internet 网关上的vpc_id隐含的,但如果您想将附件推迟到以后,资源会很有帮助。

确保您没有在aws_internet_gatewayvpc_id中定义aws_internet_gateway_attachment - 这会导致 Terraform 尝试双重附加并导致这样的错误,我刚刚发现并发现了这个问题(:)(ID 已删除):

Internet 网关... Resource.AlreadyAssociated... 已连接到网络

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM