Skip to content

Commit 9909245

Browse files
author
Abdul Wahid
authored
Refactored container definitions out of module (#69)
* Refactored container definitions out of module * Turn cloudwatch logging optional Reason: if the creation of cloudwatch logs happens outside of this module, then that can be passed into the container definitions variable * chore: Update documentation * Turn cloudwatch logging optional * Turn cloudwatch logging optional - fix Despite turning cloudwatch group resource as optional, the module will still expect a log group arn * Lint fixes * Remove square brackets as input is a list * Revert container name to original * Comment about 'target_type' for Fargate * Update target group health check parameters * Documentation update
1 parent 98c991c commit 9909245

File tree

13 files changed

+379
-115
lines changed

13 files changed

+379
-115
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
- id: check-executables-have-shebangs
88
- id: pretty-format-json
99
args: ['--autofix', '--no-sort-keys', '--indent=2']
10-
- id: check-byte-order-marker
10+
- id: fix-byte-order-marker
1111
- id: check-case-conflict
1212
- id: check-executables-have-shebangs
1313
- id: check-merge-conflict
@@ -18,7 +18,7 @@ repos:
1818
args: ['--allow-missing-credentials']
1919
- id: trailing-whitespace
2020
- repo: https://github.com/antonbabenko/pre-commit-terraform
21-
rev: v1.77.0
21+
rev: v1.77.2
2222
hooks:
2323
- id: terraform_fmt
2424
- id: terraform_docs

CHANGELOG.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ All notable changes to this project will be documented in this file.
55
<a name="unreleased"></a>
66
## [Unreleased]
77

8+
- Comment about 'target_type' for Fargate
9+
- Revert container name to original
10+
- Remove square brackets as input is a list
11+
- Lint fixes
12+
- Turn cloudwatch logging optional - fix
13+
- Turn cloudwatch logging optional
14+
- chore: Update documentation
15+
- Turn cloudwatch logging optional
16+
- Refactored container definitions out of module
17+
18+
19+
<a name="6.7.0"></a>
20+
## [6.7.0] - 2023-02-01
21+
22+
- Add ecs-managed-tags parameter ([#66](https://github.com/umotif-public/terraform-aws-ecs-fargate/issues/66))
823
- Add readonlyRootFilesystem to container_definition ([#64](https://github.com/umotif-public/terraform-aws-ecs-fargate/issues/64))
924

1025

@@ -257,7 +272,8 @@ All notable changes to this project will be documented in this file.
257272
- Initial commit
258273

259274

260-
[Unreleased]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.6.0...HEAD
275+
[Unreleased]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.7.0...HEAD
276+
[6.7.0]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.6.0...6.7.0
261277
[6.6.0]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.5.2...6.6.0
262278
[6.5.2]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.5.1...6.5.2
263279
[6.5.1]: https://github.com/umotif-public/terraform-aws-ecs-fargate/compare/6.5.0...6.5.1

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,11 @@ No modules.
130130
|------|-------------|------|---------|:--------:|
131131
| <a name="input_capacity_provider_strategy"></a> [capacity\_provider\_strategy](#input\_capacity\_provider\_strategy) | (Optional) The capacity\_provider\_strategy configuration block. This is a list of maps, where each map should contain "capacity\_provider ", "weight" and "base" | `list(any)` | `[]` | no |
132132
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | The Amazon Resource Name (ARN) that identifies the cluster. | `string` | n/a | yes |
133+
| <a name="input_container_definitions"></a> [container\_definitions](#input\_container\_definitions) | A string containing json encoded array of container definitions for Task Definition | `string` | `null` | no |
133134
| <a name="input_container_name"></a> [container\_name](#input\_container\_name) | Optional name for the container to be used instead of name\_prefix. | `string` | `""` | no |
134135
| <a name="input_cpu_architecture"></a> [cpu\_architecture](#input\_cpu\_architecture) | cpu architecture for the task | `string` | `"X86_64"` | no |
135136
| <a name="input_create_repository_credentials_iam_policy"></a> [create\_repository\_credentials\_iam\_policy](#input\_create\_repository\_credentials\_iam\_policy) | Set to true if you are specifying `repository_credentials` variable, it will attach IAM policy with necessary permissions to task role. | `bool` | `false` | no |
137+
| <a name="input_deny_egress_to_anywhere"></a> [deny\_egress\_to\_anywhere](#input\_deny\_egress\_to\_anywhere) | When this parameter is true, no default egress rule will be created | `bool` | `false` | no |
136138
| <a name="input_deployment_controller_type"></a> [deployment\_controller\_type](#input\_deployment\_controller\_type) | Type of deployment controller. Valid values: CODE\_DEPLOY, ECS, EXTERNAL. Default: ECS. | `string` | `"ECS"` | no |
137139
| <a name="input_deployment_maximum_percent"></a> [deployment\_maximum\_percent](#input\_deployment\_maximum\_percent) | The upper limit of the number of running tasks that can be running in a service during a deployment | `number` | `200` | no |
138140
| <a name="input_deployment_minimum_healthy_percent"></a> [deployment\_minimum\_healthy\_percent](#input\_deployment\_minimum\_healthy\_percent) | The lower limit of the number of running tasks that must remain running and healthy in a service during a deployment | `number` | `50` | no |
@@ -141,10 +143,12 @@ No modules.
141143
| <a name="input_enable_deployment_circuit_breaker_rollback"></a> [enable\_deployment\_circuit\_breaker\_rollback](#input\_enable\_deployment\_circuit\_breaker\_rollback) | Whether to enable Amazon ECS to roll back the service if a service deployment fails. If rollback is enabled, when a service deployment fails, the service is rolled back to the last deployment that completed successfully. | `bool` | `"false"` | no |
142144
| <a name="input_enable_ecs_managed_tags"></a> [enable\_ecs\_managed\_tags](#input\_enable\_ecs\_managed\_tags) | Specifies whether to enable Amazon ECS managed tags for the tasks within the service | `bool` | `true` | no |
143145
| <a name="input_enable_execute_command"></a> [enable\_execute\_command](#input\_enable\_execute\_command) | Specifies whether to enable Amazon ECS Exec for the tasks within the service. | `bool` | `true` | no |
146+
| <a name="input_enable_logs"></a> [enable\_logs](#input\_enable\_logs) | Turn cloudwatch logging optional | `bool` | `true` | no |
144147
| <a name="input_force_new_deployment"></a> [force\_new\_deployment](#input\_force\_new\_deployment) | Enable to force a new task deployment of the service. This can be used to update tasks to use a newer Docker image with same image/tag combination (e.g. myimage:latest), roll Fargate tasks onto a newer platform version. | `bool` | `false` | no |
145-
| <a name="input_health_check"></a> [health\_check](#input\_health\_check) | A health block containing health check settings for the target group. Overrides the defaults. | `map(string)` | n/a | yes |
148+
| <a name="input_health_check"></a> [health\_check](#input\_health\_check) | A health block containing health check settings for the target group. Overrides the defaults. | `map(string)` | `{}` | no |
146149
| <a name="input_health_check_grace_period_seconds"></a> [health\_check\_grace\_period\_seconds](#input\_health\_check\_grace\_period\_seconds) | Seconds to ignore failing load balancer health checks on newly instantiated tasks to prevent premature shutdown, up to 7200. Only valid for services configured to use load balancers. | `number` | `300` | no |
147150
| <a name="input_load_balanced"></a> [load\_balanced](#input\_load\_balanced) | Whether the task should be loadbalanced. | `bool` | `true` | no |
151+
| <a name="input_log_groups_arn"></a> [log\_groups\_arn](#input\_log\_groups\_arn) | List of CloudWatch Group ARNs to use when enable\_logs is set to false | `list(string)` | `[]` | no |
148152
| <a name="input_log_retention_in_days"></a> [log\_retention\_in\_days](#input\_log\_retention\_in\_days) | Number of days the logs will be retained in CloudWatch. | `number` | `30` | no |
149153
| <a name="input_logs_kms_key"></a> [logs\_kms\_key](#input\_logs\_kms\_key) | The KMS key ARN to use to encrypt container logs. | `string` | `""` | no |
150154
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | A prefix used for naming resources. | `string` | n/a | yes |
@@ -167,10 +171,10 @@ No modules.
167171
| <a name="input_task_container_entrypoint"></a> [task\_container\_entrypoint](#input\_task\_container\_entrypoint) | The entrypoint that is passed to the container. | `list(string)` | `[]` | no |
168172
| <a name="input_task_container_environment"></a> [task\_container\_environment](#input\_task\_container\_environment) | The environment variables to pass to a container. | `map(string)` | `{}` | no |
169173
| <a name="input_task_container_environment_files"></a> [task\_container\_environment\_files](#input\_task\_container\_environment\_files) | The environment variable files (s3 object arns) to pass to a container. Files must use .env file extension. | `list(string)` | `[]` | no |
170-
| <a name="input_task_container_image"></a> [task\_container\_image](#input\_task\_container\_image) | The image used to start a container. | `string` | n/a | yes |
174+
| <a name="input_task_container_image"></a> [task\_container\_image](#input\_task\_container\_image) | The image used to start a container. | `string` | `"marcincuber/2048-game:latest"` | no |
171175
| <a name="input_task_container_memory"></a> [task\_container\_memory](#input\_task\_container\_memory) | The hard limit (in MiB) of memory for the container. | `number` | `null` | no |
172176
| <a name="input_task_container_memory_reservation"></a> [task\_container\_memory\_reservation](#input\_task\_container\_memory\_reservation) | The soft limit (in MiB) of memory to reserve for the container. | `number` | `null` | no |
173-
| <a name="input_task_container_port"></a> [task\_container\_port](#input\_task\_container\_port) | The port number on the container that is bound to the user-specified or automatically assigned host port | `number` | n/a | yes |
177+
| <a name="input_task_container_port"></a> [task\_container\_port](#input\_task\_container\_port) | The port number on the container that is bound to the user-specified or automatically assigned host port | `number` | `80` | no |
174178
| <a name="input_task_container_protocol"></a> [task\_container\_protocol](#input\_task\_container\_protocol) | Protocol that the container exposes. | `string` | `"HTTP"` | no |
175179
| <a name="input_task_container_secrets"></a> [task\_container\_secrets](#input\_task\_container\_secrets) | The secrets variables to pass to a container. | `list(map(string))` | `null` | no |
176180
| <a name="input_task_container_working_directory"></a> [task\_container\_working\_directory](#input\_task\_container\_working\_directory) | The working directory to run commands inside the container. | `string` | `""` | no |
@@ -179,15 +183,14 @@ No modules.
179183
| <a name="input_task_definition_memory"></a> [task\_definition\_memory](#input\_task\_definition\_memory) | The soft limit (in MiB) of memory to reserve for the task. | `number` | `512` | no |
180184
| <a name="input_task_health_check"></a> [task\_health\_check](#input\_task\_health\_check) | An optional healthcheck definition for the task | `map(number)` | `null` | no |
181185
| <a name="input_task_health_command"></a> [task\_health\_command](#input\_task\_health\_command) | A string array representing the command that the container runs to determine if it is healthy. | `list(string)` | `null` | no |
182-
| <a name="input_task_host_port"></a> [task\_host\_port](#input\_task\_host\_port) | The port number on the container instance to reserve for your container. | `number` | `0` | no |
186+
| <a name="input_task_host_port"></a> [task\_host\_port](#input\_task\_host\_port) | The port number on the container instance to reserve for your container. | `number` | `80` | no |
183187
| <a name="input_task_mount_points"></a> [task\_mount\_points](#input\_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 |
184188
| <a name="input_task_pseudo_terminal"></a> [task\_pseudo\_terminal](#input\_task\_pseudo\_terminal) | Allocate TTY in the container | `bool` | `null` | no |
185189
| <a name="input_task_start_timeout"></a> [task\_start\_timeout](#input\_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 |
186190
| <a name="input_task_stop_timeout"></a> [task\_stop\_timeout](#input\_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 |
187191
| <a name="input_volume"></a> [volume](#input\_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(any)` | `[]` | no |
188192
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | The VPC ID. | `string` | n/a | yes |
189193
| <a name="input_wait_for_steady_state"></a> [wait\_for\_steady\_state](#input\_wait\_for\_steady\_state) | If true, Terraform will wait for the service to reach a steady state (like aws ecs wait services-stable) before continuing. | `bool` | `false` | no |
190-
| <a name="deny_egress_to_anywhere"></a> [deny\_egress\_to\_anywhere](#deny\_egress\_to\_anywhere) | If true, Terraform will no create the default egress rule which allows all outgoing traffic. | `bool` | `false` | no |
191194

192195
## Outputs
193196

@@ -202,6 +205,7 @@ No modules.
202205
| <a name="output_target_group_arn"></a> [target\_group\_arn](#output\_target\_group\_arn) | The ARN of the Target Group used by Load Balancer. |
203206
| <a name="output_target_group_name"></a> [target\_group\_name](#output\_target\_group\_name) | The Name of the Target Group used by Load Balancer. |
204207
| <a name="output_task_definition_arn"></a> [task\_definition\_arn](#output\_task\_definition\_arn) | The Amazon Resource Name (ARN) of the task definition created |
208+
| <a name="output_task_definition_container_definitions"></a> [task\_definition\_container\_definitions](#output\_task\_definition\_container\_definitions) | A list of container definitions |
205209
| <a name="output_task_definition_name"></a> [task\_definition\_name](#output\_task\_definition\_name) | The name of the task definition created |
206210
| <a name="output_task_role_arn"></a> [task\_role\_arn](#output\_task\_role\_arn) | The Amazon Resource Name (ARN) specifying the ECS service role. |
207211
| <a name="output_task_role_name"></a> [task\_role\_name](#output\_task\_role\_name) | The name of the Fargate task service role. |

data.tf

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,34 @@ data "aws_iam_policy_document" "task_assume" {
1515

1616
# Task logging privileges
1717
data "aws_iam_policy_document" "task_permissions" {
18-
statement {
19-
effect = "Allow"
18+
dynamic "statement" {
19+
for_each = var.enable_logs ? [1] : []
20+
content {
21+
effect = "Allow"
22+
23+
resources = [
24+
aws_cloudwatch_log_group.main[0].arn,
25+
"${aws_cloudwatch_log_group.main[0].arn}:*"
26+
]
27+
28+
actions = [
29+
"logs:CreateLogStream",
30+
"logs:PutLogEvents",
31+
]
32+
}
33+
}
2034

21-
resources = [
22-
aws_cloudwatch_log_group.main.arn,
23-
"${aws_cloudwatch_log_group.main.arn}:*"
24-
]
35+
dynamic "statement" {
36+
for_each = var.enable_logs ? [] : [1]
2537

26-
actions = [
27-
"logs:CreateLogStream",
28-
"logs:PutLogEvents",
29-
]
38+
content {
39+
effect = "Allow"
40+
resources = var.log_groups_arn
41+
actions = [
42+
"logs:CreateLogStream",
43+
"logs:PutLogEvents"
44+
]
45+
}
3046
}
3147
}
3248

@@ -118,4 +134,4 @@ data "aws_iam_policy_document" "get_environment_files" {
118134

119135
data "aws_ecs_task_definition" "task" {
120136
task_definition = aws_ecs_task_definition.task.family
121-
}
137+
}

examples/core/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_version = ">= 1.0.11"
33

44
required_providers {
5-
aws = ">= 4.0.0"
5+
aws = ">= 4.6.0"
66
}
77
}
88

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
terraform {
2+
required_version = ">= 1.0.11"
3+
4+
required_providers {
5+
aws = ">= 4.8.0"
6+
}
7+
}
8+
9+
provider "aws" {
10+
region = "eu-west-1"
11+
}
12+
13+
#####
14+
# VPC and subnets
15+
#####
16+
data "aws_vpc" "default" {
17+
default = true
18+
}
19+
20+
data "aws_subnets" "all" {
21+
filter {
22+
name = "vpc-id"
23+
values = [data.aws_vpc.default.id]
24+
}
25+
}
26+
27+
#####
28+
# ECS cluster and fargate
29+
#####
30+
resource "aws_ecs_cluster" "cluster" {
31+
name = "ecs-external-container-definitions-test"
32+
33+
setting {
34+
name = "containerInsights"
35+
value = "disabled"
36+
}
37+
}
38+
39+
resource "aws_ecs_cluster_capacity_providers" "cluster" {
40+
cluster_name = aws_ecs_cluster.cluster.name
41+
42+
capacity_providers = ["FARGATE_SPOT", "FARGATE"]
43+
44+
default_capacity_provider_strategy {
45+
capacity_provider = "FARGATE_SPOT"
46+
}
47+
}
48+
49+
module "container_1" {
50+
source = "cloudposse/ecs-container-definition/aws"
51+
version = "0.58.2"
52+
53+
container_name = "example"
54+
container_image = "hello-world:latest"
55+
56+
port_mappings = [
57+
{
58+
containerPort = 80
59+
hostPort = 80
60+
protocol = "tcp"
61+
}
62+
]
63+
64+
}
65+
66+
module "container_2" {
67+
source = "cloudposse/ecs-container-definition/aws"
68+
version = "0.58.2"
69+
70+
container_name = "example-2"
71+
container_image = "hello-world:latest"
72+
73+
port_mappings = [
74+
{
75+
containerPort = 81
76+
hostPort = 81
77+
protocol = "udp"
78+
}
79+
]
80+
81+
container_depends_on = [
82+
{
83+
containerName = "example"
84+
condition = "START"
85+
}
86+
]
87+
}
88+
89+
#####
90+
# ALB
91+
#####
92+
module "alb" {
93+
source = "umotif-public/alb/aws"
94+
version = "~> 2.0"
95+
96+
name_prefix = "alb-example"
97+
load_balancer_type = "application"
98+
internal = false
99+
vpc_id = data.aws_vpc.default.id
100+
subnets = data.aws_subnets.all.ids
101+
}
102+
103+
resource "aws_lb_listener" "alb_80" {
104+
load_balancer_arn = module.alb.arn
105+
port = "80"
106+
protocol = "HTTP"
107+
108+
default_action {
109+
type = "forward"
110+
target_group_arn = module.fargate.target_group_arn[0]
111+
}
112+
}
113+
114+
#####
115+
# Security Group Config
116+
#####
117+
resource "aws_security_group_rule" "alb_ingress_80" {
118+
security_group_id = module.alb.security_group_id
119+
type = "ingress"
120+
protocol = "tcp"
121+
from_port = 80
122+
to_port = 80
123+
cidr_blocks = ["0.0.0.0/0"]
124+
ipv6_cidr_blocks = ["::/0"]
125+
}
126+
127+
resource "aws_security_group_rule" "task_ingress_80" {
128+
security_group_id = module.fargate.service_sg_id
129+
type = "ingress"
130+
protocol = "tcp"
131+
from_port = 80
132+
to_port = 80
133+
source_security_group_id = module.alb.security_group_id
134+
}
135+
136+
module "fargate" {
137+
source = "../../"
138+
139+
name_prefix = "ecs-fargate-example-2"
140+
vpc_id = data.aws_vpc.default.id
141+
private_subnet_ids = data.aws_subnets.all.ids
142+
143+
cluster_id = aws_ecs_cluster.cluster.id
144+
145+
container_definitions = jsonencode([
146+
module.container_1.json_map_object,
147+
module.container_2.json_map_object
148+
])
149+
150+
capacity_provider_strategy = [
151+
{
152+
capacity_provider = "FARGATE_SPOT",
153+
weight = 100
154+
}
155+
]
156+
157+
enable_deployment_circuit_breaker = true
158+
enable_deployment_circuit_breaker_rollback = true
159+
160+
health_check = {
161+
port = "traffic-port"
162+
path = "/"
163+
}
164+
165+
target_groups = [
166+
{
167+
container_name = "example"
168+
target_group_name = "tg-example"
169+
container_port = 80
170+
}
171+
]
172+
173+
}
174+
175+
output "first_container_json" {
176+
description = "Container definition in JSON format"
177+
value = module.container_1.json_map_encoded_list
178+
}
179+
180+
output "second_container_json" {
181+
description = "Container definition in JSON format"
182+
value = module.container_2.json_map_encoded_list
183+
}
184+
185+
output "task_definition_container_definitions" {
186+
description = "A list of container definitions"
187+
value = module.fargate.task_definition_container_definitions
188+
}

examples/fargate-efs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_version = ">= 1.0.11"
33

44
required_providers {
5-
aws = ">= 4.0.0"
5+
aws = ">= 4.8.0"
66
}
77
}
88

examples/fargate-spot/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_version = ">= 1.0.11"
33

44
required_providers {
5-
aws = ">= 4.0.0"
5+
aws = ">= 4.6.0"
66
}
77
}
88

examples/multiple-target-groups/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_version = ">= 1.0.11"
33

44
required_providers {
5-
aws = ">= 4.0.0"
5+
aws = ">= 4.6.0"
66
}
77
}
88

0 commit comments

Comments
 (0)