Skip to content

Commit e6bc1c4

Browse files
authored
Create ecs service module (#3321)
Create a new module for ecs services. This allows us to simply add additional service types, e.g. such as a background service for good-job
2 parents 22cbc85 + 429331a commit e6bc1c4

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
resource "aws_security_group" "this" {
2+
name = "${var.server_type}-service-${var.environment}"
3+
description = "Security Group for communication with ECS Service"
4+
vpc_id = var.network_params.vpc_id
5+
lifecycle {
6+
ignore_changes = [description]
7+
}
8+
}
9+
10+
resource "aws_security_group_rule" "egress_all" {
11+
type = "egress"
12+
from_port = 0
13+
to_port = 0
14+
protocol = "-1"
15+
cidr_blocks = ["0.0.0.0/0"]
16+
security_group_id = aws_security_group.this.id
17+
}
18+
19+
resource "aws_ecs_service" "this" {
20+
name = "mavis-${var.environment}-${var.server_type}"
21+
cluster = var.cluster_id
22+
task_definition = aws_ecs_task_definition.this.arn
23+
desired_count = var.desired_count
24+
launch_type = "FARGATE"
25+
enable_execute_command = true
26+
health_check_grace_period_seconds = 60
27+
28+
network_configuration {
29+
subnets = var.network_params.subnets
30+
security_groups = [aws_security_group.this.id]
31+
}
32+
deployment_controller {
33+
type = var.deployment_controller
34+
}
35+
dynamic "deployment_circuit_breaker" {
36+
for_each = var.deployment_controller == "ECS" ? [1] : []
37+
content {
38+
enable = true
39+
rollback = true
40+
}
41+
}
42+
dynamic "load_balancer" {
43+
for_each = var.loadbalancer != null ? [1] : []
44+
content {
45+
target_group_arn = var.loadbalancer.target_group_arn
46+
container_name = var.container_name
47+
container_port = var.loadbalancer.container_port
48+
}
49+
}
50+
deployment_minimum_healthy_percent = 100
51+
deployment_maximum_percent = 200
52+
lifecycle {
53+
ignore_changes = [
54+
task_definition,
55+
load_balancer,
56+
# desired_count TODO: Uncomment once we include autoscaling
57+
]
58+
create_before_destroy = true
59+
}
60+
}
61+
62+
resource "aws_ecs_task_definition" "this" {
63+
family = "mavis-${var.server_type}-task-definition-${var.environment}"
64+
requires_compatibilities = ["FARGATE"]
65+
network_mode = "awsvpc"
66+
cpu = var.task_config.cpu
67+
memory = var.task_config.memory
68+
execution_role_arn = var.task_config.execution_role_arn
69+
task_role_arn = var.task_config.task_role_arn
70+
container_definitions = jsonencode([
71+
{
72+
name = var.container_name
73+
image = var.task_config.docker_image
74+
essential = true
75+
portMappings = [
76+
{
77+
containerPort = 4000
78+
hostPort = 4000
79+
}
80+
]
81+
environment = concat(var.task_config.environment, [{ name = "SERVER_TYPE", value = var.server_type }])
82+
secrets = var.task_config.secrets
83+
logConfiguration = {
84+
logDriver = "awslogs"
85+
options = {
86+
awslogs-group = var.task_config.log_group_name
87+
awslogs-region = var.task_config.region
88+
awslogs-stream-prefix = "${var.environment}-${var.server_type}-logs"
89+
}
90+
}
91+
healthCheck = {
92+
command = var.task_config.health_check_command
93+
interval = 30
94+
timeout = 5
95+
retries = 3
96+
startPeriod = 10
97+
}
98+
}
99+
])
100+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
output "security_group_id" {
2+
value = aws_security_group.this.id
3+
description = "The ID of the security group for the ECS service"
4+
}
5+
6+
output "service" {
7+
value = {
8+
id = aws_ecs_service.this.id
9+
name = aws_ecs_service.this.name
10+
}
11+
description = "Essential attributes of the ECS service"
12+
}
13+
14+
output "task_definition" {
15+
value = aws_ecs_task_definition.this.family
16+
description = "Essential attributes of the ECS task definition"
17+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
variable "environment" {
2+
type = string
3+
description = "Application environment (for example production or staging)"
4+
nullable = false
5+
}
6+
7+
variable "server_type" {
8+
type = string
9+
description = "Type of server to be deployed. This is set as an environment variable in the main container, and is used to determine how the application is launched"
10+
nullable = false
11+
}
12+
13+
variable "desired_count" {
14+
type = number
15+
description = "The initial amount of instances when creating the service"
16+
nullable = false
17+
}
18+
19+
variable "task_config" {
20+
type = object({
21+
environment = list(object({
22+
name = string
23+
value = string
24+
}))
25+
secrets = list(object({
26+
name = string
27+
valueFrom = string
28+
}))
29+
cpu = number
30+
memory = number
31+
docker_image = string
32+
execution_role_arn = string
33+
task_role_arn = string
34+
log_group_name = string
35+
region = string
36+
health_check_command = list(string)
37+
})
38+
description = "Task configuration variables for the task definition ECS service"
39+
nullable = false
40+
}
41+
42+
variable "cluster_id" {
43+
type = string
44+
description = "The ID of the ECS cluster."
45+
nullable = false
46+
}
47+
48+
variable "network_params" {
49+
type = object({
50+
subnets = list(string)
51+
vpc_id = string
52+
})
53+
description = "Network configuration for the ECS service"
54+
nullable = false
55+
}
56+
57+
variable "loadbalancer" {
58+
type = object({
59+
target_group_arn = string
60+
container_port = number
61+
})
62+
description = "Load balancer configuration for the ECS service if the service should be user-facing"
63+
default = null
64+
nullable = true
65+
}
66+
67+
variable "deployment_controller" {
68+
type = string
69+
description = "Deployment controller type for the ECS service"
70+
default = "ECS"
71+
nullable = false
72+
}
73+
74+
variable "container_name" {
75+
type = string
76+
description = "Name of the essential container in the task. Also the container which is serviced by the load balancer if applicable."
77+
default = "application"
78+
nullable = false
79+
}

0 commit comments

Comments
 (0)