Skip to content

Commit a9827f9

Browse files
committed
refactor: split ECS module main.tf into separate files
1 parent 6003cf1 commit a9827f9

File tree

8 files changed

+238
-245
lines changed

8 files changed

+238
-245
lines changed

modules/ecs/autoscaling.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "aws_autoscaling_group" "this" {
2+
desired_capacity = var.auto_scaling_group_desired_capacity != null ? var.auto_scaling_group_desired_capacity : 1
3+
max_size = var.auto_scaling_group_max_size
4+
min_size = var.auto_scaling_group_min_size
5+
vpc_zone_identifier = var.private_subnet_ids
6+
7+
launch_template {
8+
id = aws_launch_template.this.id
9+
version = "$Latest"
10+
}
11+
12+
target_group_arns = [aws_lb_target_group.instance_target[0].arn]
13+
protect_from_scale_in = true
14+
15+
tag {
16+
key = "Name"
17+
value = "${var.service_name}-ec2"
18+
propagate_at_launch = true
19+
}
20+
}

modules/ecs/iam.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
resource "aws_iam_role" "instance_role" {
2+
name = "${local.iam_instance_role_name_prefix}-${random_pet.name.id}"
3+
assume_role_policy = data.aws_iam_policy_document.this.json
4+
}
5+
6+
resource "aws_iam_role_policy_attachment" "this" {
7+
role = aws_iam_role.instance_role.name
8+
policy_arn = local.iam_role_policy_arn
9+
}
10+
11+
resource "aws_iam_role" "task_role" {
12+
name = "ecs-task-${var.task_definition_family}"
13+
assume_role_policy = data.aws_iam_policy_document.ecs_task_assume_policy.json
14+
15+
inline_policy {
16+
name = "ecs-task-permissions"
17+
policy = jsonencode({
18+
Version = "2012-10-17"
19+
Statement = [
20+
{
21+
Action = [
22+
"ecr:*",
23+
"logs:*",
24+
"ssm:*",
25+
"kms:Decrypt",
26+
"secretsmanager:GetSecretValue",
27+
"sts:AssumeRoleWithWebIdentity"
28+
]
29+
Effect = "Allow"
30+
Resource = "*"
31+
}
32+
]
33+
})
34+
}
35+
}

modules/ecs/launch-template.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
resource "aws_launch_template" "this" {
2+
name_prefix = var.launch_template_name_prefix
3+
image_id = var.launch_template_image_id != null ? var.launch_template_image_id : local.launch_template_image_id
4+
instance_type = var.launch_template_instance_type != null ? var.launch_template_instance_type : local.launch_template_instance_type
5+
key_name = var.launch_template_key_name
6+
7+
user_data = base64encode(<<EOF
8+
#!/bin/bash
9+
echo ECS_CLUSTER=${var.cluster_arn} >> /etc/ecs/ecs.config
10+
EOF
11+
)
12+
13+
network_interfaces {
14+
associate_public_ip_address = true
15+
subnet_id = var.private_subnet_ids[0]
16+
security_groups = [aws_security_group.this.id]
17+
}
18+
19+
iam_instance_profile {
20+
name = aws_iam_instance_profile.this.name
21+
}
22+
23+
lifecycle {
24+
create_before_destroy = true
25+
}
26+
}

modules/ecs/load-balancer.tf

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
resource "aws_lb_target_group" "instance_target" {
2+
count = var.endpoint_details != null ? 1 : 0
3+
name = format("%s-%s-instance", var.service_name, terraform.workspace)
4+
port = local.main_container_port
5+
protocol = "HTTP"
6+
vpc_id = var.vpc_id
7+
target_type = local.alb_instance_target_type
8+
9+
health_check {
10+
protocol = "HTTP"
11+
interval = 10
12+
unhealthy_threshold = 6
13+
matcher = "200,301-399"
14+
}
15+
}
16+
17+
resource "aws_lb_target_group" "ip_target" {
18+
count = var.endpoint_details != null ? 1 : 0
19+
name = format("%s-%s-ip", var.service_name, terraform.workspace)
20+
port = local.main_container_port
21+
protocol = "HTTP"
22+
vpc_id = var.vpc_id
23+
target_type = local.alb_ip_target_type
24+
25+
health_check {
26+
protocol = "HTTP"
27+
interval = 10
28+
unhealthy_threshold = 6
29+
matcher = "200,301-399"
30+
}
31+
}
32+
33+
resource "aws_lb_listener_rule" "default_rule" {
34+
count = var.endpoint_details != null ? 1 : 0
35+
listener_arn = var.endpoint_details.lb_listener_arn
36+
priority = 10
37+
38+
condition {
39+
host_header {
40+
values = [var.endpoint_details.domain_url]
41+
}
42+
}
43+
44+
dynamic "action" {
45+
for_each = var.authenticate_oidc_details != null ? [1] : []
46+
47+
content {
48+
type = "authenticate-oidc"
49+
50+
authenticate_oidc {
51+
authorization_endpoint = local.authenticate_oidc_authorization_endpoint
52+
token_endpoint = local.authenticate_oidc_token_endpoint
53+
user_info_endpoint = local.authenticate_oidc_user_info_endpoint
54+
issuer = local.authenticate_oidc_issuer
55+
session_cookie_name = format("TOKEN-OIDC-%s", var.authenticate_oidc_details.client_id)
56+
scope = "openid email"
57+
on_unauthenticated_request = "authenticate"
58+
client_id = var.authenticate_oidc_details.client_id
59+
client_secret = var.authenticate_oidc_details.client_secret
60+
}
61+
}
62+
}
63+
64+
action {
65+
type = "forward"
66+
target_group_arn = aws_lb_target_group.ip_target[0].arn
67+
}
68+
}
69+
70+
resource "aws_lb_listener_rule" "events_post_rule" {
71+
listener_arn = var.endpoint_details.lb_listener_arn
72+
priority = 1
73+
74+
condition {
75+
path_pattern {
76+
values = ["/events"]
77+
}
78+
}
79+
80+
condition {
81+
http_request_method {
82+
values = ["POST"]
83+
}
84+
}
85+
86+
condition {
87+
host_header {
88+
values = [var.endpoint_details.domain_url]
89+
}
90+
}
91+
92+
action {
93+
type = "forward"
94+
target_group_arn = aws_lb_target_group.ip_target[0].arn
95+
}
96+
}

0 commit comments

Comments
 (0)