Skip to content

Commit 56ac0f1

Browse files
authored
Add full support for volumes and container timeouts (#16)
1 parent 4a985e2 commit 56ac0f1

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ resource "aws_ecs_cluster" "cluster" {
2828
2929
module "ecs-farage" {
3030
source = "umotif-public/ecs-fargate/aws"
31-
version = "~> 1.3.0"
31+
version = "~> 1.4.0"
3232
3333
name_prefix = "ecs-fargate-example"
3434
vpc_id = "vpc-abasdasd132"
@@ -64,6 +64,7 @@ Module is to be used with Terraform > 0.12.
6464

6565
* [ECS Fargate](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/core)
6666
* [ECS Fargate Spot](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/fargate-spot)
67+
* [ECS Fargate with EFS](https://github.com/umotif-public/terraform-aws-ecs-fargate/tree/master/examples/fargate-efs)
6768

6869
## Authors
6970

@@ -124,6 +125,9 @@ No requirements.
124125
| task\_definition\_memory | The soft limit (in MiB) of memory to reserve for the task. | `number` | `512` | no |
125126
| task\_health\_check | An optional healthcheck definition for the task | `object({ command = list(string), interval = number, timeout = number, retries = number, startPeriod = number })` | `null` | no |
126127
| task\_host\_port | The port number on the container instance to reserve for your container. | `number` | `0` | no |
128+
| task\_mount\_points | The mount points for data volumes in your container. Each object inside the list requires "sourceVolume", "containerPath" and "readOnly". For more information see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html | `list(object({ sourceVolume = string, containerPath = string, readOnly = bool }))` | `null` | no |
129+
| task\_start\_timeout | Time duration (in seconds) to wait before giving up on resolving dependencies for a container. If this parameter is not specified, the default value of 3 minutes is used (fargate). | `number` | `null` | no |
130+
| task\_stop\_timeout | Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own. The max stop timeout value is 120 seconds and if the parameter is not specified, the default value of 30 seconds is used. | `number` | `null` | no |
127131
| volume | (Optional) A set of volume blocks that containers in your task may use. This is a list of maps, where each map should contain "name", "host\_path", "docker\_volume\_configuration" and "efs\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html | `list` | `[]` | no |
128132
| vpc\_id | The VPC ID. | `string` | n/a | yes |
129133

examples/core/main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ module "fargate" {
110110
path = "/"
111111
}
112112

113+
task_stop_timeout = 90
114+
113115
### To use task credentials, below paramaters are required
114116
# create_repository_credentials_iam_policy = false
115117
# repository_credentials = aws_secretsmanager_secret.task_credentials.arn

examples/fargate-efs/main.tf

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
provider "aws" {
2+
region = "eu-west-1"
3+
}
4+
5+
#####
6+
# VPC and subnets
7+
#####
8+
module "vpc" {
9+
source = "terraform-aws-modules/vpc/aws"
10+
version = "~> 2.21"
11+
12+
name = "simple-vpc"
13+
14+
cidr = "10.0.0.0/16"
15+
16+
azs = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
17+
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
18+
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
19+
20+
enable_nat_gateway = false
21+
}
22+
23+
#####
24+
# ALB
25+
#####
26+
module "alb" {
27+
source = "umotif-public/alb/aws"
28+
version = "~> 1.0"
29+
30+
name_prefix = "alb-example"
31+
load_balancer_type = "application"
32+
internal = false
33+
vpc_id = module.vpc.vpc_id
34+
subnets = module.vpc.public_subnets
35+
}
36+
37+
resource "aws_lb_listener" "alb_80" {
38+
load_balancer_arn = module.alb.arn
39+
port = "80"
40+
protocol = "HTTP"
41+
42+
default_action {
43+
type = "forward"
44+
target_group_arn = module.fargate.target_group_arn
45+
}
46+
}
47+
48+
#####
49+
# Security Group Config
50+
#####
51+
resource "aws_security_group_rule" "alb_ingress_80" {
52+
security_group_id = module.alb.security_group_id
53+
type = "ingress"
54+
protocol = "tcp"
55+
from_port = 80
56+
to_port = 80
57+
cidr_blocks = ["0.0.0.0/0"]
58+
ipv6_cidr_blocks = ["::/0"]
59+
}
60+
61+
resource "aws_security_group_rule" "task_ingress_80" {
62+
security_group_id = module.fargate.service_sg_id
63+
type = "ingress"
64+
protocol = "tcp"
65+
from_port = 80
66+
to_port = 80
67+
source_security_group_id = module.alb.security_group_id
68+
}
69+
70+
#####
71+
# EFS
72+
#####
73+
resource "aws_efs_file_system" "efs" {
74+
creation_token = "efs-html"
75+
76+
tags = {
77+
Name = "efs-html"
78+
}
79+
}
80+
81+
#####
82+
# ECS cluster and fargate
83+
#####
84+
resource "aws_ecs_cluster" "cluster" {
85+
name = "ecs-spot-test"
86+
capacity_providers = ["FARGATE_SPOT", "FARGATE"]
87+
88+
default_capacity_provider_strategy {
89+
capacity_provider = "FARGATE_SPOT"
90+
}
91+
92+
setting {
93+
name = "containerInsights"
94+
value = "disabled"
95+
}
96+
}
97+
98+
module "fargate" {
99+
source = "../../"
100+
101+
name_prefix = "ecs-fargate-example"
102+
vpc_id = module.vpc.vpc_id
103+
private_subnet_ids = module.vpc.public_subnets
104+
lb_arn = module.alb.arn
105+
cluster_id = aws_ecs_cluster.cluster.id
106+
107+
platform_version = "1.4.0"
108+
109+
task_container_image = "marcincuber/2048-game:latest"
110+
task_definition_cpu = 256
111+
task_definition_memory = 512
112+
113+
task_container_port = 80
114+
task_container_assign_public_ip = true
115+
116+
health_check = {
117+
port = "traffic-port"
118+
path = "/"
119+
}
120+
121+
capacity_provider_strategy = [
122+
{
123+
capacity_provider = "FARGATE_SPOT",
124+
weight = 100
125+
}
126+
]
127+
128+
task_stop_timeout = 90
129+
130+
task_mount_points = [
131+
{
132+
"sourceVolume" = aws_efs_file_system.efs.creation_token,
133+
"containerPath" = "/usr/share/nginx/html",
134+
"readOnly" = true
135+
}
136+
]
137+
138+
volume = [
139+
{
140+
name = "efs-html",
141+
efs_volume_configuration = [
142+
{
143+
"file_system_id" : aws_efs_file_system.efs.id,
144+
"root_directory" : "/usr/share/nginx"
145+
}
146+
]
147+
}
148+
]
149+
}

main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,15 @@ resource "aws_ecs_task_definition" "task" {
181181
%{if var.task_container_cpu != null~}
182182
"cpu": ${var.task_container_cpu},
183183
%{~endif}
184+
%{if var.task_start_timeout != null~}
185+
"startTimeout": ${var.task_start_timeout},
186+
%{~endif}
187+
%{if var.task_stop_timeout != null~}
188+
"stopTimeout": ${var.task_stop_timeout},
189+
%{~endif}
190+
%{if var.task_mount_points != null~}
191+
"mountPoints": ${jsonencode(var.task_mount_points)},
192+
%{~endif}
184193
"environment": ${jsonencode(local.task_environment)}
185194
}]
186195
EOF

variables.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,22 @@ variable "task_container_working_directory" {
234234
default = ""
235235
type = string
236236
}
237+
238+
variable "task_start_timeout" {
239+
type = number
240+
description = "Time duration (in seconds) to wait before giving up on resolving dependencies for a container. If this parameter is not specified, the default value of 3 minutes is used (fargate)."
241+
default = null
242+
}
243+
244+
variable "task_stop_timeout" {
245+
type = number
246+
description = "Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own. The max stop timeout value is 120 seconds and if the parameter is not specified, the default value of 30 seconds is used."
247+
default = null
248+
}
249+
250+
variable "task_mount_points" {
251+
description = "The mount points for data volumes in your container. Each object inside the list requires \"sourceVolume\", \"containerPath\" and \"readOnly\". For more information see https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html "
252+
type = list(object({ sourceVolume = string, containerPath = string, readOnly = bool }))
253+
default = null
254+
}
255+

0 commit comments

Comments
 (0)