-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
210 lines (177 loc) · 5.87 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# main.tf
module "global" {
source = "./modules/global"
stage = var.stage
organisation = var.organisation
}
data "aws_s3_bucket" "state_bucket" {
bucket = "${var.organisation}-terraform-bucket-${var.stage}"
}
data "aws_dynamodb_table" "lock_table" {
name = "${var.organisation}-terraform-lock-table-${var.stage}"
}
# Providers
provider "aws" {
region = "eu-west-2"
profile = local.profile
allowed_account_ids = var.allowed_account_ids
assume_role {
role_arn = "arn:aws:iam::${local.account}:role/${var.organisation}-terraform-assumed-role-${var.stage}"
}
}
provider "aws" {
region = "eu-west-2"
profile = local.profile
allowed_account_ids = var.allowed_account_ids
assume_role {
role_arn = "arn:aws:iam::${local.account}:role/${var.organisation}-terraform-assumed-role-${var.stage}"
}
alias = "eu-west-2"
}
provider "aws" {
region = "us-east-1"
profile = local.profile
allowed_account_ids = var.allowed_account_ids
assume_role {
role_arn = "arn:aws:iam::${local.account}:role/${var.organisation}-terraform-assumed-role-${var.stage}"
}
alias = "us-east-1"
}
provider "aws" {
region = "us-west-1"
profile = local.profile
allowed_account_ids = var.allowed_account_ids
assume_role {
role_arn = "arn:aws:iam::${local.account}:role/${var.organisation}-terraform-assumed-role-${var.stage}"
}
alias = "us-west-1"
}
# Regional Stacks
module "eu-west-2" {
providers = {
aws = aws.eu-west-2
}
source = "./modules/regional"
organisation = var.organisation
region = "eu-west-2"
task_role_arn = module.global.ecs_task_role_arn
task_policy_arn = module.global.ecs_task_policy_arn
first_create = var.first_create
region_cidr_blocks = var.region_cidr_blocks
vpc_peering_connection_ids = {
us-east-1 = aws_vpc_peering_connection.eu_west_2_to_us_east_1.id,
us-west-1 = aws_vpc_peering_connection.eu_west_2_to_us_west_1.id
}
}
module "us-east-1" {
providers = {
aws = aws.us-east-1
}
source = "./modules/regional"
organisation = var.organisation
region = "us-east-1"
task_role_arn = module.global.ecs_task_role_arn
task_policy_arn = module.global.ecs_task_policy_arn
first_create = var.first_create
region_cidr_blocks = var.region_cidr_blocks
vpc_peering_connection_ids = {
eu-west-2 = aws_vpc_peering_connection.eu_west_2_to_us_east_1.id,
us-west-1 = aws_vpc_peering_connection.us_east_1_to_us_west_1.id
}
}
module "us-west-1" {
providers = {
aws = aws.us-west-1
}
source = "./modules/regional"
organisation = var.organisation
region = "us-west-1"
task_role_arn = module.global.ecs_task_role_arn
task_policy_arn = module.global.ecs_task_policy_arn
first_create = var.first_create
region_cidr_blocks = var.region_cidr_blocks
vpc_peering_connection_ids = {
eu-west-2 = aws_vpc_peering_connection.eu_west_2_to_us_west_1.id,
us-east-1 = aws_vpc_peering_connection.us_east_1_to_us_west_1.id
}
}
# VPC Peering Connections
resource "aws_vpc_peering_connection" "eu_west_2_to_us_east_1" {
provider = aws.eu-west-2
vpc_id = module.eu-west-2.vpc_id
peer_vpc_id = module.us-east-1.vpc_id
peer_region = "us-east-1"
tags = {
Name = "${var.organisation}-vpc-peering-eu-west-2-to-us-east-1"
}
}
resource "aws_vpc_peering_connection_accepter" "us_east_1_accept_eu_west_2" {
provider = aws.us-east-1
vpc_peering_connection_id = aws_vpc_peering_connection.eu_west_2_to_us_east_1.id
auto_accept = true
tags = {
Name = "${var.organisation}-vpc-peering-us-east-1-accept-eu-west-2"
}
}
resource "aws_vpc_peering_connection" "eu_west_2_to_us_west_1" {
provider = aws.eu-west-2
vpc_id = module.eu-west-2.vpc_id
peer_vpc_id = module.us-west-1.vpc_id
peer_region = "us-west-1"
tags = {
Name = "${var.organisation}-vpc-peering-eu-west-2-to-us-west-1"
}
}
resource "aws_vpc_peering_connection_accepter" "us_west_1_accept_eu_west_2" {
provider = aws.us-west-1
vpc_peering_connection_id = aws_vpc_peering_connection.eu_west_2_to_us_west_1.id
auto_accept = true
tags = {
Name = "${var.organisation}-vpc-peering-us-west-1-accept-eu-west-2"
}
}
resource "aws_vpc_peering_connection" "us_east_1_to_us_west_1" {
provider = aws.us-east-1
vpc_id = module.us-east-1.vpc_id
peer_vpc_id = module.us-west-1.vpc_id
peer_region = "us-west-1"
tags = {
Name = "${var.organisation}-vpc-peering-us-east-1-to-us-west-1"
}
}
resource "aws_vpc_peering_connection_accepter" "us_west_1_accept_us_east_1" {
provider = aws.us-west-1
vpc_peering_connection_id = aws_vpc_peering_connection.us_east_1_to_us_west_1.id
auto_accept = true
tags = {
Name = "${var.organisation}-vpc-peering-us-west-1-accept-us-east-1"
}
}
resource "aws_route53_zone_association" "cross_region_eu_west_2" {
for_each = {
for k, v in local.cross_region_associations : k => v
if v.target_region == "eu-west-2"
}
provider = aws.eu-west-2
vpc_id = local.vpc_ids["eu-west-2"]
zone_id = local.hosted_zone_ids[each.value.source_region]
}
resource "aws_route53_zone_association" "cross_region_us_east_1" {
for_each = {
for k, v in local.cross_region_associations : k => v
if v.target_region == "us-east-1"
}
provider = aws.us-east-1
vpc_id = local.vpc_ids["us-east-1"]
zone_id = local.hosted_zone_ids[each.value.source_region]
}
resource "aws_route53_zone_association" "cross_region_us_west_1" {
for_each = {
for k, v in local.cross_region_associations : k => v
if v.target_region == "us-west-1"
}
provider = aws.us-west-1
vpc_id = local.vpc_ids["us-west-1"]
zone_id = local.hosted_zone_ids[each.value.source_region]
}
data "aws_caller_identity" "current" {}