-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
175 lines (138 loc) · 6.84 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
174
175
locals {
# 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 var.create_tgw && can(v.tgw_routes)
]), 2)
tgw_default_route_table_tags_merged = merge(
var.tags,
{ Name = var.name },
var.tgw_default_route_table_tags,
)
vpc_route_table_destination_cidr = flatten([
for k, v in var.vpc_attachments : [
for rtb_id in try(v.vpc_route_table_ids, []) : {
rtb_id = rtb_id
cidr = v.tgw_destination_cidr
}
]
])
}
################################################################################
# Transit Gateway
################################################################################
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"
multicast_support = var.enable_multicast_support ? "enable" : "disable"
vpn_ecmp_support = var.enable_vpn_ecmp_support ? "enable" : "disable"
dns_support = var.enable_dns_support ? "enable" : "disable"
transit_gateway_cidr_blocks = var.transit_gateway_cidr_blocks
timeouts {
create = try(var.timeouts.create, null)
update = try(var.timeouts.update, null)
delete = try(var.timeouts.delete, null)
}
tags = merge(
var.tags,
{ Name = var.name },
var.tgw_tags,
)
}
resource "aws_ec2_tag" "this" {
for_each = { for k, v in local.tgw_default_route_table_tags_merged : k => v if var.create_tgw && var.enable_default_route_table_association }
resource_id = aws_ec2_transit_gateway.this[0].association_default_route_table_id
key = each.key
value = each.value
}
################################################################################
# VPC Attachment
################################################################################
resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
for_each = var.vpc_attachments
transit_gateway_id = var.create_tgw ? aws_ec2_transit_gateway.this[0].id : each.value.tgw_id
vpc_id = each.value.vpc_id
subnet_ids = each.value.subnet_ids
dns_support = try(each.value.dns_support, true) ? "enable" : "disable"
ipv6_support = try(each.value.ipv6_support, false) ? "enable" : "disable"
appliance_mode_support = try(each.value.appliance_mode_support, false) ? "enable" : "disable"
transit_gateway_default_route_table_association = try(each.value.transit_gateway_default_route_table_association, true)
transit_gateway_default_route_table_propagation = try(each.value.transit_gateway_default_route_table_propagation, true)
tags = merge(
var.tags,
{ Name = var.name },
var.tgw_vpc_attachment_tags,
try(each.value.tags, {}),
)
}
################################################################################
# Route Table / 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(
var.tags,
{ Name = var.name },
var.tgw_route_table_tags,
)
}
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 = try(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(try(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
}
resource "aws_ec2_transit_gateway_route_table_association" "this" {
for_each = {
for k, v in var.vpc_attachments : k => v if var.create_tgw && try(v.transit_gateway_default_route_table_association, true) != true
}
# 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 = var.create_tgw ? aws_ec2_transit_gateway_route_table.this[0].id : try(each.value.transit_gateway_route_table_id, var.transit_gateway_route_table_id)
}
resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
for_each = {
for k, v in var.vpc_attachments : k => v if var.create_tgw && try(v.transit_gateway_default_route_table_propagation, true) != true
}
# 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 = var.create_tgw ? aws_ec2_transit_gateway_route_table.this[0].id : try(each.value.transit_gateway_route_table_id, var.transit_gateway_route_table_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(
var.tags,
{ Name = coalesce(var.ram_name, var.name) },
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
}