-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
117 lines (91 loc) · 3.37 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
locals {
metadata = {
package = "terraform-aws-vpc-connectivity"
version = trimspace(file("${path.module}/../../VERSION"))
module = basename(path.module)
name = var.name
}
module_tags = var.module_tags_enabled ? {
"module.terraform.io/package" = local.metadata.package
"module.terraform.io/version" = local.metadata.version
"module.terraform.io/name" = local.metadata.module
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
"module.terraform.io/instance" = local.metadata.name
} : {}
}
locals {
address_family = {
"IPV4" = "ipv4"
"IPV6" = "ipv6"
}
primary_peering = var.bgp_peerings[0]
secondary_peerings = slice(var.bgp_peerings, 1, length(var.bgp_peerings))
routers = jsondecode(file("${path.module}/routers.json"))
router_ids = local.routers[*].id
}
###################################################
# Private VIF (Virtual Interface) of Direct Connect
###################################################
resource "aws_dx_private_virtual_interface" "this" {
name = var.name
connection_id = var.connection
dx_gateway_id = (var.gateway.type == "DIRECT_CONNECT_GATEWAY"
? var.gateway.id
: null
)
vpn_gateway_id = (var.gateway.type == "VIRTUAL_PRIVATE_GATEWAY"
? var.gateway.id
: null
)
vlan = var.vlan
mtu = var.jumbo_frame_enabled ? 9001 : 1500
sitelink_enabled = var.sitelink_enabled
## BGP Peering
address_family = local.address_family[local.primary_peering.address_family]
bgp_asn = local.primary_peering.bgp_asn
bgp_auth_key = local.primary_peering.bgp_auth_key
amazon_address = local.primary_peering.amazon_address
customer_address = local.primary_peering.customer_address
tags = merge(
{
"Name" = local.metadata.name
},
local.module_tags,
var.tags,
)
}
###################################################
# Additional BGP Peerings for Private VIF
###################################################
resource "aws_dx_bgp_peer" "this" {
for_each = {
for peering in local.secondary_peerings :
"${peering.address_family}/${peering.bgp_asn}" => peering
}
virtual_interface_id = aws_dx_private_virtual_interface.this.id
address_family = local.address_family[each.value.address_family]
bgp_asn = each.value.bgp_asn
bgp_auth_key = each.value.bgp_auth_key
amazon_address = each.value.amazon_address
customer_address = each.value.customer_address
}
###################################################
# Sample Router Configuration for Private VIF (Virtual Interface)
###################################################
data "aws_dx_router_configuration" "this" {
count = var.router_configuration.router != null ? 1 : 0
virtual_interface_id = aws_dx_private_virtual_interface.this.id
router_type_identifier = var.router_configuration.router
lifecycle {
precondition {
condition = contains(local.router_ids, var.router_configuration.router)
error_message = "Not supported router ID: ${var.router_configuration.router}."
}
}
}
resource "local_file" "this" {
count = var.router_configuration.router != null ? 1 : 0
filename = coalesce(var.router_configuration.output_path,
"${path.root}/outputs/${var.name}.${var.router_configuration.router}.conf")
content = one(data.aws_dx_router_configuration.this[*].customer_router_config)
}