-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterraform_refactoring_example.tf
More file actions
167 lines (142 loc) · 4.97 KB
/
terraform_refactoring_example.tf
File metadata and controls
167 lines (142 loc) · 4.97 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
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
# Example of how to refactor local Terraform to use centralized infrastructure
# Data source to access centralized infrastructure state
data "terraform_remote_state" "infrastructure" {
backend = "s3"
config = {
bucket = "hokusai-infrastructure-tfstate"
key = "prod/infrastructure.tfstate"
region = "us-east-1"
}
}
# Remove these resources from local terraform (they're now in central infrastructure):
# - aws_lb.main
# - aws_lb.auth
# - aws_lb.registry
# - aws_lb_target_group.api
# - aws_lb_target_group.mlflow
# - aws_lb_target_group.auth
# - aws_lb_target_group.registry_api
# - aws_lb_target_group.registry_mlflow
# - aws_lb_listener.* (all listeners)
# - aws_lb_listener_rule.* (all routing rules)
# - aws_route53_record.auth
# - aws_route53_record.registry
# - aws_iam_role.ecs_task_execution
# - aws_iam_role.ecs_task
# - aws_security_group.alb
# Update ECS service to reference remote infrastructure
resource "aws_ecs_service" "api" {
name = "${var.project_name}-api"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.api.arn
desired_count = var.api_desired_count
launch_type = "FARGATE"
network_configuration {
subnets = module.vpc.private_subnets
security_groups = [aws_security_group.ecs_tasks.id]
assign_public_ip = false
}
load_balancer {
# Reference target group from remote state instead of local resource
target_group_arn = data.terraform_remote_state.infrastructure.outputs.api_target_group_arn
container_name = "${var.project_name}-api"
container_port = 8001
}
# Reference listener from remote state
depends_on = [data.terraform_remote_state.infrastructure]
}
resource "aws_ecs_service" "mlflow" {
name = "${var.project_name}-mlflow"
cluster = aws_ecs_cluster.main.id
task_definition = aws_ecs_task_definition.mlflow.arn
desired_count = var.mlflow_desired_count
launch_type = "FARGATE"
network_configuration {
subnets = module.vpc.private_subnets
security_groups = [aws_security_group.ecs_tasks.id]
assign_public_ip = false
}
load_balancer {
# Reference target group from remote state
target_group_arn = data.terraform_remote_state.infrastructure.outputs.mlflow_target_group_arn
container_name = "${var.project_name}-mlflow"
container_port = 5000
}
depends_on = [data.terraform_remote_state.infrastructure]
}
# Update task definitions to use remote IAM roles
resource "aws_ecs_task_definition" "api" {
family = "${var.project_name}-api-${var.environment}"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.api_cpu
memory = var.api_memory
# Reference IAM roles from remote state
execution_role_arn = data.terraform_remote_state.infrastructure.outputs.ecs_task_execution_role_arn
task_role_arn = data.terraform_remote_state.infrastructure.outputs.ecs_task_role_arn
container_definitions = jsonencode([
{
name = "${var.project_name}-api"
image = "${aws_ecr_repository.api.repository_url}:${var.api_image_tag}"
portMappings = [
{
containerPort = 8001
protocol = "tcp"
}
]
environment = [
{
name = "ENVIRONMENT"
value = var.environment
},
{
name = "MLFLOW_TRACKING_URI"
value = "https://${data.terraform_remote_state.infrastructure.outputs.registry_dns_name}/mlflow"
}
]
logConfiguration = {
logDriver = "awslogs"
options = {
"awslogs-group" = aws_cloudwatch_log_group.api.name
"awslogs-region" = var.aws_region
"awslogs-stream-prefix" = "ecs"
}
}
}
])
}
# Update security group to allow traffic from ALB
resource "aws_security_group" "ecs_tasks" {
name_prefix = "${var.project_name}-ecs-tasks-"
description = "Security group for ECS tasks"
vpc_id = module.vpc.vpc_id
ingress {
from_port = 0
to_port = 65535
protocol = "tcp"
# Reference ALB security group from remote state
security_groups = [data.terraform_remote_state.infrastructure.outputs.alb_security_group_id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
}
# Update outputs to reference remote infrastructure
output "api_endpoint" {
description = "URL for the API endpoint"
value = "https://${data.terraform_remote_state.infrastructure.outputs.registry_dns_name}/api"
}
output "mlflow_endpoint" {
description = "URL for the MLflow UI"
value = "https://${data.terraform_remote_state.infrastructure.outputs.registry_dns_name}/mlflow"
}
output "main_alb_dns" {
description = "DNS name of the main ALB"
value = data.terraform_remote_state.infrastructure.outputs.main_alb_dns_name
}