-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
126 lines (94 loc) · 4.88 KB
/
main.tf
File metadata and controls
126 lines (94 loc) · 4.88 KB
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
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
################################################################################
# Transit Gateway
################################################################################
resource "aws_ec2_transit_gateway" "this" {
description = var.description != "" ? var.description : "Transit Gateway - ${var.name}"
amazon_side_asn = var.amazon_side_asn
auto_accept_shared_attachments = var.enable_auto_accept_shared_attachments ? "enable" : "disable"
default_route_table_association = var.enable_default_route_table_association ? "enable" : "disable"
default_route_table_propagation = var.enable_default_route_table_propagation ? "enable" : "disable"
dns_support = var.enable_dns_support ? "enable" : "disable"
vpn_ecmp_support = var.enable_vpn_ecmp_support ? "enable" : "disable"
multicast_support = var.enable_multicast_support ? "enable" : "disable"
tags = merge(var.tags, {
Name = var.name
})
}
################################################################################
# VPC Attachments
################################################################################
resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
for_each = var.vpc_attachments
transit_gateway_id = aws_ec2_transit_gateway.this.id
vpc_id = each.value.vpc_id
subnet_ids = each.value.subnet_ids
appliance_mode_support = each.value.appliance_mode_support ? "enable" : "disable"
dns_support = each.value.dns_support ? "enable" : "disable"
transit_gateway_default_route_table_association = each.value.transit_gateway_default_route_table_association
transit_gateway_default_route_table_propagation = each.value.transit_gateway_default_route_table_propagation
tags = merge(var.tags, {
Name = "${var.name}-${each.key}"
}, each.value.tags)
}
################################################################################
# Custom Route Tables
################################################################################
resource "aws_ec2_transit_gateway_route_table" "this" {
for_each = var.route_tables
transit_gateway_id = aws_ec2_transit_gateway.this.id
tags = merge(var.tags, {
Name = "${var.name}-${each.value.name}"
})
}
################################################################################
# Static Routes
################################################################################
resource "aws_ec2_transit_gateway_route" "this" {
for_each = {
for idx, route in var.routes :
"${route.route_table_key}-${route.destination_cidr}" => route
}
destination_cidr_block = each.value.destination_cidr
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[each.value.route_table_key].id
transit_gateway_attachment_id = each.value.blackhole ? null : aws_ec2_transit_gateway_vpc_attachment.this[each.value.attachment_key].id
blackhole = each.value.blackhole
}
################################################################################
# Route Table Associations
################################################################################
resource "aws_ec2_transit_gateway_route_table_association" "this" {
for_each = var.route_table_associations
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.this[each.value.attachment_key].id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[each.value.route_table_key].id
}
################################################################################
# Route Table Propagations
################################################################################
resource "aws_ec2_transit_gateway_route_table_propagation" "this" {
for_each = var.route_table_propagations
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.this[each.value.attachment_key].id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.this[each.value.route_table_key].id
}
################################################################################
# Resource Access Manager (RAM) - Cross-Account Sharing
################################################################################
resource "aws_ram_resource_share" "this" {
count = length(var.ram_principals) > 0 ? 1 : 0
name = "${var.name}-tgw-share"
allow_external_principals = true
tags = merge(var.tags, {
Name = "${var.name}-tgw-share"
})
}
resource "aws_ram_resource_association" "this" {
count = length(var.ram_principals) > 0 ? 1 : 0
resource_arn = aws_ec2_transit_gateway.this.arn
resource_share_arn = aws_ram_resource_share.this[0].arn
}
resource "aws_ram_principal_association" "this" {
count = length(var.ram_principals)
principal = var.ram_principals[count.index]
resource_share_arn = aws_ram_resource_share.this[0].arn
}