This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
173 lines (140 loc) · 6.57 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
locals {
vpc_attachments_without_default_route_table_association = {
for k, v in var.vpc_attachments : k => v if lookup(v, "transit_gateway_default_route_table_association", true) != true
}
vpc_attachments_without_default_route_table_propagation = {
for k, v in var.vpc_attachments : k => v if lookup(v, "transit_gateway_default_route_table_propagation", true) != true
}
# List of maps with key and route values
vpc_attachments_with_routes = chunklist(flatten([
for k, v in var.vpc_attachments : setproduct([{ key = k }], v["tgw_routes"]) if length(lookup(v, "tgw_routes", {})) > 0
]), 2)
tgw_default_route_table_tags_merged = merge(
{
"Name" = format("%s", var.name)
},
var.tags,
var.tgw_default_route_table_tags,
)
vpc_route_table_destination_cidr = flatten([
for k, v in var.vpc_attachments : [
for rtb_id in lookup(v, "vpc_route_table_ids", []) : {
rtb_id = rtb_id
cidr = v["tgw_destination_cidr"]
}
]
])
}
resource "aws_ec2_transit_gateway" "this" {
count = var.create_tgw ? 1 : 0
description = coalesce(var.description, var.name)
amazon_side_asn = var.amazon_side_asn
default_route_table_association = var.enable_default_route_table_association ? "enable" : "disable"
default_route_table_propagation = var.enable_default_route_table_propagation ? "enable" : "disable"
auto_accept_shared_attachments = var.enable_auto_accept_shared_attachments ? "enable" : "disable"
vpn_ecmp_support = var.enable_vpn_ecmp_support ? "enable" : "disable"
dns_support = var.enable_dns_support ? "enable" : "disable"
tags = merge(
{
"Name" = format("%s", var.name)
},
var.tags,
var.tgw_tags,
)
}
resource "aws_ec2_tag" "this" {
for_each = var.create_tgw && var.enable_default_route_table_association ? local.tgw_default_route_table_tags_merged : {}
resource_id = aws_ec2_transit_gateway.this[0].association_default_route_table_id
key = each.key
value = each.value
}
#########################
# Route table and routes
#########################
resource "aws_ec2_transit_gateway_route_table" "this" {
count = var.create_tgw ? 1 : 0
transit_gateway_id = aws_ec2_transit_gateway.this[0].id
tags = merge(
{
"Name" = format("%s", var.name)
},
var.tags,
var.tgw_route_table_tags,
)
}
# VPC attachment routes
resource "aws_ec2_transit_gateway_route" "this" {
count = length(local.vpc_attachments_with_routes)
destination_cidr_block = local.vpc_attachments_with_routes[count.index][1]["destination_cidr_block"]
blackhole = lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", null)
transit_gateway_route_table_id = var.create_tgw ? aws_ec2_transit_gateway_route_table.this[0].id : var.transit_gateway_route_table_id
transit_gateway_attachment_id = tobool(lookup(local.vpc_attachments_with_routes[count.index][1], "blackhole", false)) == false ? aws_ec2_transit_gateway_vpc_attachment.this[local.vpc_attachments_with_routes[count.index][0]["key"]].id : null
}
resource "aws_route" "this" {
for_each = { for x in local.vpc_route_table_destination_cidr : x.rtb_id => x.cidr }
route_table_id = each.key
destination_cidr_block = each.value
transit_gateway_id = aws_ec2_transit_gateway.this[0].id
}
###########################################################
# VPC Attachments, route table association and propagation
###########################################################
resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
for_each = var.vpc_attachments
transit_gateway_id = lookup(each.value, "tgw_id", var.create_tgw ? aws_ec2_transit_gateway.this[0].id : null)
vpc_id = each.value["vpc_id"]
subnet_ids = each.value["subnet_ids"]
dns_support = lookup(each.value, "dns_support", true) ? "enable" : "disable"
ipv6_support = lookup(each.value, "ipv6_support", false) ? "enable" : "disable"
appliance_mode_support = lookup(each.value, "appliance_mode_support", false) ? "enable" : "disable"
transit_gateway_default_route_table_association = lookup(each.value, "transit_gateway_default_route_table_association", true)
transit_gateway_default_route_table_propagation = lookup(each.value, "transit_gateway_default_route_table_propagation", true)
tags = merge(
{
Name = format("%s-%s", var.name, each.key)
},
var.tags,
var.tgw_vpc_attachment_tags,
)
}
resource "aws_ec2_transit_gateway_route_table_association" "this" {
for_each = local.vpc_attachments_without_default_route_table_association
# Create association if it was not set already by aws_ec2_transit_gateway_vpc_attachment resource
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.this[each.key].id
transit_gateway_route_table_id = coalesce(lookup(each.value, "transit_gateway_route_table_id", null), var.transit_gateway_route_table_id, aws_ec2_transit_gateway_route_table.this[0].id)
}
resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
for_each = local.vpc_attachments_without_default_route_table_propagation
# Create association if it was not set already by aws_ec2_transit_gateway_vpc_attachment resource
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.this[each.key].id
transit_gateway_route_table_id = coalesce(lookup(each.value, "transit_gateway_route_table_id", null), var.transit_gateway_route_table_id, aws_ec2_transit_gateway_route_table.this[0].id)
}
##########################
# Resource Access Manager
##########################
resource "aws_ram_resource_share" "this" {
count = var.create_tgw && var.share_tgw ? 1 : 0
name = coalesce(var.ram_name, var.name)
allow_external_principals = var.ram_allow_external_principals
tags = merge(
{
"Name" = format("%s", coalesce(var.ram_name, var.name))
},
var.tags,
var.ram_tags,
)
}
resource "aws_ram_resource_association" "this" {
count = var.create_tgw && var.share_tgw ? 1 : 0
resource_arn = aws_ec2_transit_gateway.this[0].arn
resource_share_arn = aws_ram_resource_share.this[0].id
}
resource "aws_ram_principal_association" "this" {
count = var.create_tgw && var.share_tgw ? length(var.ram_principals) : 0
principal = var.ram_principals[count.index]
resource_share_arn = aws_ram_resource_share.this[0].arn
}
resource "aws_ram_resource_share_accepter" "this" {
count = !var.create_tgw && var.share_tgw ? 1 : 0
share_arn = var.ram_resource_share_arn
}