EC2 Transit Gateway VPN 附件

问题描述 投票:0回答:1

我正在创建一个 VPN 附件,但我收到这样的错误,我什至添加了 ec2 标签

错误:未找到匹配的 EC2 Transit Gateway VPN 附件
[10:01:00] 错误 错误:没有匹配的 EC2 Transit Gateway VPN 找到附件了
[10:01:00] 信息,但在文件中找到了一个值 “/tmp/tmp5nojlx7c.json”。如果您想使用
[10:01:00] INFO 这个值,添加一个“变量”块到 配置。
[10:01:00] 错误
[10:01:00] 信息
[10:01:00] 错误 data.aws_ec2_transit_gateway_vpn_attachment.vpn_attachment,
[10:01:00] INFO 要消除这些警告,请使用 TF_VAR_... 提供环境变量
[10:01:00] vpn.tf 第 55 行数据中出现错误 “aws_ec2_transit_gateway_vpn_attachment”“vpn_attachment”:
[10:01:00] INFO 所有配置的某些“全局”设置 在您的组织中。到
[10:01:00] 错误 55:数据 “aws_ec2_transit_gateway_vpn_attachment”“vpn_attachment”{

# Create VPN Gateway
resource "aws_vpn_gateway" "vpn_gateway" {
  vpc_id = module.vpc.vpc_id

  tags = {
    Name = "xxx-${var.instance}"
  }
}

# Create Customer Gateway
resource "aws_customer_gateway" "customer_gateway" {
  bgp_asn    = xx
  ip_address = "xx"
  type       = "xx"

  tags = {
    Name = "xxxx-${var.instance}"
  }
}

# Create Transit Gateway
resource "aws_ec2_transit_gateway" "transit_gateway" {
  description = "Transit Gateway for VPN"

  tags = {
    Name = "transit-gateway-${var.instance}"
  }
}
# Create VPN Connection
resource "aws_vpn_connection" "vpn_connection" {
  customer_gateway_id    = aws_customer_gateway.customer_gateway.id
  vpn_gateway_id         = aws_vpn_gateway.vpn_gateway.id
  type                   = "ipsec.1"
  static_routes_only     = true

  tags = {
    Name = "vpn-connection-${var.instance}"
  }
}
   
# Create Transit Gateway Route Table
resource "aws_ec2_transit_gateway_route_table" "vpn_route_table" {
  transit_gateway_id = aws_ec2_transit_gateway.transit_gateway.id

  tags = {
    Name = "vpn-route-table-${var.instance}"
  }
}

# Data block for VPN Attachment
data "aws_ec2_transit_gateway_vpn_attachment" "vpn_attachment" {
  vpn_connection_id  = aws_vpn_connection.vpn_connection.id
  transit_gateway_id = aws_ec2_transit_gateway.transit_gateway.id
}

# AWS EC2 Tag for VPN Attachment 
resource "aws_ec2_tag" "vpn_attachment_tag" {
  resource_id = aws_vpn_connection.vpn_connection.transit_gateway_attachment_id
  key         = "name"
  value       = "EC2 Transit Gateway VPN Attachment"
}

# Create Transit Gateway Route
resource "aws_ec2_transit_gateway_route" "vpn_route" {
 destination_cidr_block           = var.static_routes_destinations
 transit_gateway_route_table_id   = aws_ec2_transit_gateway_route_table.vpn_route_table.id
 transit_gateway_attachment_id    = data.aws_ec2_transit_gateway_vpn_attachment.vpn_attachment
}
amazon-web-services terraform terraform-provider-aws
1个回答
0
投票

要解决此问题,您还必须添加相应的资源来创建中转网关。例如:

resource "aws_ec2_transit_gateway" "transit_gateway" {
  description = "transit gateway"
}

# Create VPN Connection
resource "aws_vpn_connection" "vpn_connection" {
  customer_gateway_id    = aws_customer_gateway.customer_gateway.id
  vpn_gateway_id         = aws_vpn_gateway.vpn_gateway.id
  type                   = "ipsec.1"
  static_routes_only     = true

  tags = {
    Name = "vpn-connection-${var.instance}"
  }
}
   
# Create Transit Gateway Route Table
resource "aws_ec2_transit_gateway_route_table" "vpn_route_table" {
  transit_gateway_id = aws_ec2_transit_gateway.transit_gateway.id

  tags = {
    Name = "vpn-route-table-${var.instance}"
  }
}

# AWS EC2 Tag for VPN Attachment 
resource "aws_ec2_tag" "vpn_attachment_tag" {
  resource_id = aws_vpn_connection.vpn_connection.transit_gateway_attachment_id
  key         = "name"
  value       = "EC2 Transit Gateway VPN Attachment"
}

# Create Transit Gateway Route
resource "aws_ec2_transit_gateway_route" "vpn_route" {
 destination_cidr_block           = var.static_routes_destinations
 transit_gateway_route_table_id   = aws_ec2_transit_gateway_route_table.vpn_route_table.id
 transit_gateway_attachment_id    = aws_vpn_connection.vpn_connection.transit_gateway_attachment_id
}

我还认为您不需要数据源,因为数据源的文档说明了以下内容:

EC2 Transit Gateway VPN 连接是由引用 EC2 Transit Gateway 的 VPN 连接隐式创建的,因此不存在托管资源。为方便起见,

aws_vpn_connection
资源包含一个
transit_gateway_attachment_id
属性,可以替换此数据源的某些用法。[...]

© www.soinside.com 2019 - 2024. All rights reserved.