-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
228 lines (190 loc) · 5.7 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -----------------------------------
# Terraform Configuration
# -----------------------------------
terraform {
# Backend configuration for storing the Terraform state in S3 with DynamoDB table for state locking
backend "s3" {
encrypt = true
bucket = "pillarbox-monitoring-tfstate"
key = "terraform/20-pillarbox-monitoring-app/terraform.tfstate"
dynamodb_table = "pillarbox-monitoring-terraform-statelock"
profile = "prod"
}
# Specify required providers and their versions
required_providers {
aws = {
source = "hashicorp/aws"
version = "~>5.4.0"
}
}
}
# -----------------------------------
# AWS Provider Setup
# -----------------------------------
provider "aws" {
allowed_account_ids = [local.account_id]
# Apply default tags to all AWS resources
default_tags {
tags = local.default_tags
}
}
# Provider to access the production account with assumed role
provider "aws" {
alias = "prod"
allowed_account_ids = [var.account_ids["prod"]]
dynamic "assume_role" {
for_each = local.is_prod ? [] : [1]
content {
role_arn = "arn:aws:iam::${var.account_ids["prod"]}:role/route53-access-role"
}
}
}
# -----------------------------------
# AWS Data Sources
# -----------------------------------
# Get current AWS region
data "aws_region" "current" {}
# Retrieve the VPC information
data "aws_vpc" "main_vpc" {
id = local.vpc_id
}
# Retrieve public subnets based on VPC and tags
data "aws_subnets" "public_subnets" {
filter {
name = "vpc-id"
values = [data.aws_vpc.main_vpc.id]
}
tags = {
Name = "*public*"
}
}
# Retrieve private subnets based on VPC and tags
data "aws_subnets" "private_subnets" {
filter {
name = "vpc-id"
values = [data.aws_vpc.main_vpc.id]
}
tags = {
Name = "*private*"
}
}
# Retrieve private route tables based on VPC and tags
data "aws_route_tables" "private_route_tables" {
filter {
name = "vpc-id"
values = [data.aws_vpc.main_vpc.id]
}
tags = {
Name = "*private*"
}
}
# -----------------------------------
# AWS ECS and Service Discovery Setup
# -----------------------------------
# Create Service Discovery namespace for the VPC
resource "aws_service_discovery_private_dns_namespace" "service_discovery_namespace" {
name = var.private_domain_name
vpc = data.aws_vpc.main_vpc.id
}
# Create ECS Cluster
resource "aws_ecs_cluster" "ecs_cluster" {
name = local.ecs_cluster_name
}
# -----------------------------------
# AWS VPC Endpoints Setup
# -----------------------------------
# Security Group for VPC Endpoints
resource "aws_security_group" "vpc_endpoints_sg" {
name = "vpc-endpoints-sg"
description = "Associated to ECR/s3 VPC Endpoints"
vpc_id = local.vpc_id
# Ingress rule to allow access to VPC endpoints
ingress {
description = "Allow Nodes to pull images from ECR via VPC endpoints"
protocol = "tcp"
from_port = 443
to_port = 443
security_groups = [
aws_security_group.dispatch_task_sg.id,
aws_security_group.transfer_task_sg.id,
]
}
tags = {
Name = "vpc-endpoints-sg"
}
}
# VPC Endpoint for ECR API
resource "aws_vpc_endpoint" "ecr_api" {
vpc_id = data.aws_vpc.main_vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.ecr.api"
vpc_endpoint_type = "Interface"
subnet_ids = data.aws_subnets.private_subnets.ids
security_group_ids = [aws_security_group.vpc_endpoints_sg.id]
private_dns_enabled = true
tags = {
Name = "vpc-ecr-api-endpoint"
}
}
# VPC Endpoint for ECR DKR
resource "aws_vpc_endpoint" "ecr_dkr" {
vpc_id = data.aws_vpc.main_vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.ecr.dkr"
vpc_endpoint_type = "Interface"
subnet_ids = data.aws_subnets.private_subnets.ids
security_group_ids = [aws_security_group.vpc_endpoints_sg.id]
private_dns_enabled = true
tags = {
Name = "vpc-ecr-dkr-endpoint"
}
}
# VPC Endpoint for S3
resource "aws_vpc_endpoint" "s3" {
vpc_id = data.aws_vpc.main_vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = data.aws_route_tables.private_route_tables.ids
tags = {
Name = "vpc-s3-endpoint"
}
}
# VPC Endpoint for CloudWatch Logs
resource "aws_vpc_endpoint" "cloudwatch_logs" {
vpc_id = data.aws_vpc.main_vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.logs"
vpc_endpoint_type = "Interface"
subnet_ids = data.aws_subnets.private_subnets.ids
security_group_ids = [aws_security_group.vpc_endpoints_sg.id]
private_dns_enabled = true
tags = {
Name = "vpc-cloudwatch-logs-endpoint"
}
}
# Security Group for the SNS VPC Endpoint
resource "aws_security_group" "sns_vpc_endpoint_sg" {
name = "sns-vpc-endpoint-sg"
description = "Associated to SNS Endpoint"
vpc_id = local.vpc_id
# Ingress rule to allow access to VPC endpoints
ingress {
description = "Allow Grafana to publish to SNS topics"
protocol = "tcp"
from_port = 443
to_port = 443
security_groups = [aws_security_group.grafana_sg.id]
}
tags = {
Name = "sns-vpc-endpoint-sg"
}
}
# VPC Endpoint for SNS
resource "aws_vpc_endpoint" "sns" {
vpc_id = data.aws_vpc.main_vpc.id
service_name = "com.amazonaws.${data.aws_region.current.name}.sns"
vpc_endpoint_type = "Interface"
subnet_ids = data.aws_subnets.private_subnets.ids
security_group_ids = [aws_security_group.sns_vpc_endpoint_sg.id]
private_dns_enabled = true
tags = {
Name = "vpc-sns-endpoint"
}
}