Skip to content

Commit ecb91ed

Browse files
committed
Add example,fix vars and add conditional creation for module
1 parent f2a934a commit ecb91ed

File tree

6 files changed

+152
-41
lines changed

6 files changed

+152
-41
lines changed

README.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# terraform-aws-ecs-fargate-task-definition
2-
Terraform module to create AWS ECS Fargate Task Definition
2+
Terraform module to create AWS ECS Fargate Task Definition.
33

44
## Terraform versions
55

@@ -9,7 +9,23 @@ Terraform 0.12. Pin module version to `~> v1.0`. Submit pull-requests to `master
99

1010
```hcl
1111
module "ecs-task-definition" {
12-
...
12+
source = "umotif-public/ecs-fargate-task-definition/aws"
13+
version = "~> 1.0"
14+
15+
enabled = true
16+
name_prefix = "test-container"
17+
task_container_image = "httpd:2.4"
18+
19+
container_name = "test-container-name"
20+
task_container_port = "80"
21+
task_host_port = "80"
22+
23+
task_definition_cpu = "512"
24+
task_definition_memory = "1024"
25+
26+
task_container_environment = {
27+
"ENVIRONEMNT" = "Test"
28+
}
1329
}
1430
```
1531

@@ -19,7 +35,7 @@ Module is to be used with Terraform > 0.12.
1935

2036
## Examples
2137

22-
* [Example]()
38+
* [ECS Fargate Task Definition](https://github.com/umotif-public/terraform-aws-ecs-fargate-task-definition/tree/master/examples/core)
2339

2440
## Authors
2541

@@ -30,8 +46,10 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
3046

3147
| Name | Description | Type | Default | Required |
3248
|------|-------------|:----:|:-----:|:-----:|
33-
| cloudwatch\_log\_group\_name | CloudWatch log group name required to enabled logDriver in container definitions for ecs task. | map(string) | n/a | yes |
49+
| cloudwatch\_log\_group\_name | CloudWatch log group name required to enabled logDriver in container definitions for ecs task. | string | `""` | no |
3450
| container\_name | Optional name for the container to be used instead of name\_prefix. | string | `""` | no |
51+
| docker\_volume\_configuration | \(Optional\) Used to configure a docker volume option "docker\_volume\_configuration". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs\_task\_definition.html | list | `[]` | no |
52+
| enabled | Whether to create the resources. Set to `false` to prevent the module from creating any resources | bool | `"true"` | no |
3553
| name\_prefix | A prefix used for naming resources. | string | n/a | yes |
3654
| placement\_constraints | \(Optional\) A set of placement constraints rules that are taken into consideration during task placement. Maximum number of placement\_constraints is 10. This is a list of maps, where each map should contain "type" and "expression" | list | `[]` | no |
3755
| proxy\_configuration | \(Optional\) The proxy configuration details for the App Mesh proxy. This is a list of maps, where each map should contain "container\_name", "properties" and "type" | list | `[]` | no |
@@ -59,7 +77,7 @@ Module managed by [Marcin Cuber](https://github.com/marcincuber) [LinkedIn](http
5977
| execution\_role\_unique\_id | The stable and unique string identifying the role. |
6078
| task\_definition\_arn | Full ARN of the Task Definition \(including both family and revision\). |
6179
| task\_definition\_family | The family of the Task Definition. |
62-
| task\_definition\_td\_revision | The revision of the task in a particular family. |
80+
| task\_definition\_revision | The revision of the task in a particular family. |
6381
| task\_role\_arn | The Amazon Resource Name \(ARN\) specifying the ECS service role. |
6482
| task\_role\_create\_date | The creation date of the IAM role. |
6583
| task\_role\_id | The ID of the role. |

examples/core/main.tf

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,27 @@ provider "aws" {
33
}
44

55
#####
6-
# VPC and subnets
6+
# task definition
77
#####
8-
module "vpc" {
9-
source = "terraform-aws-modules/vpc/aws"
10-
version = "~> 2.21"
8+
module "ecs-task-definition" {
9+
source = "../.."
1110

12-
name = "simple-vpc"
11+
enabled = true
12+
name_prefix = "test-container"
13+
task_container_image = "httpd:2.4"
1314

14-
cidr = "10.0.0.0/16"
15+
container_name = "test-container-name"
16+
task_container_port = "80"
17+
task_host_port = "80"
1518

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+
task_definition_cpu = "512"
20+
task_definition_memory = "1024"
1921

20-
enable_nat_gateway = false
22+
task_container_environment = {
23+
"ENVIRONEMNT" = "Test"
24+
}
25+
26+
cloudwatch_log_group_name = "/test-cloudwatch/log-group"
27+
task_container_command = ["/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""]
2128
}
29+

examples/core/outputs.tf

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
output "task_role_arn" {
2+
value = module.ecs-task-definition.task_role_arn
3+
}
4+
5+
output "task_role_name" {
6+
value = module.ecs-task-definition.task_role_name
7+
}
8+
9+
output "task_role_create_date" {
10+
value = module.ecs-task-definition.task_role_create_date
11+
}
12+
13+
output "task_role_id" {
14+
value = module.ecs-task-definition.task_role_id
15+
}
16+
17+
output "task_role_unique_id" {
18+
value = module.ecs-task-definition.task_role_unique_id
19+
}
20+
21+
output "execution_role_arn" {
22+
value = module.ecs-task-definition.execution_role_arn
23+
}
24+
25+
output "execution_role_name" {
26+
value = module.ecs-task-definition.execution_role_name
27+
}
28+
29+
output "execution_role_create_date" {
30+
value = module.ecs-task-definition.execution_role_create_date
31+
}
32+
33+
output "execution_role_id" {
34+
value = module.ecs-task-definition.execution_role_id
35+
}
36+
37+
output "execution_role_unique_id" {
38+
value = module.ecs-task-definition.execution_role_unique_id
39+
}
40+
41+
output "task_definition_arn" {
42+
value = module.ecs-task-definition.task_definition_arn
43+
}
44+
45+
output "task_definition_family" {
46+
value = module.ecs-task-definition.task_definition_family
47+
}
48+
49+
output "task_definition_revision" {
50+
value = module.ecs-task-definition.task_definition_revision
51+
}
52+
53+
output "container_port" {
54+
value = module.ecs-task-definition.container_port
55+
}

main.tf

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,46 @@
22
# Execution IAM Role
33
#####
44
resource "aws_iam_role" "execution" {
5+
count = var.enabled ? 1 : 0
6+
57
name = "${var.name_prefix}-execution-role"
68
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
79

810
tags = var.tags
911
}
1012

1113
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attach" {
12-
role = aws_iam_role.execution.name
14+
count = var.enabled ? 1 : 0
15+
16+
role = aws_iam_role.execution[0].name
1317
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
1418
}
1519

1620
resource "aws_iam_role_policy" "read_repository_credentials" {
17-
count = length(var.repository_credentials) != 0 ? 1 : 0
21+
count = length(var.repository_credentials) != 0 && var.enabled ? 1 : 0
1822

1923
name = "${var.name_prefix}-read-repository-credentials"
20-
role = aws_iam_role.execution.id
24+
role = aws_iam_role.execution[0].id
2125
policy = data.aws_iam_policy_document.read_repository_credentials.json
2226
}
2327

2428
#####
2529
# IAM - Task role, basic. Append policies to this role for S3, DynamoDB etc.
2630
#####
2731
resource "aws_iam_role" "task" {
32+
count = var.enabled ? 1 : 0
33+
2834
name = "${var.name_prefix}-task-role"
2935
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
3036

3137
tags = var.tags
3238
}
3339

3440
resource "aws_iam_role_policy" "log_agent" {
41+
count = var.enabled ? 1 : 0
42+
3543
name = "${var.name_prefix}-log-permissions"
36-
role = aws_iam_role.task.id
44+
role = aws_iam_role.task[0].id
3745
policy = data.aws_iam_policy_document.task_permissions.json
3846
}
3947

@@ -51,13 +59,15 @@ locals {
5159
}
5260

5361
resource "aws_ecs_task_definition" "task" {
62+
count = var.enabled ? 1 : 0
63+
5464
family = var.name_prefix
55-
execution_role_arn = aws_iam_role.execution.arn
65+
execution_role_arn = aws_iam_role.execution[0].arn
5666
network_mode = "awsvpc"
5767
requires_compatibilities = ["FARGATE"]
5868
cpu = var.task_definition_cpu
5969
memory = var.task_definition_memory
60-
task_role_arn = aws_iam_role.task.arn
70+
task_role_arn = aws_iam_role.task[0].arn
6171

6272
container_definitions = <<EOF
6373
[{
@@ -113,18 +123,27 @@ EOF
113123
dynamic "volume" {
114124
for_each = var.volume
115125
content {
116-
name = volume.value.name
117-
host_path = lookup(volume.value, "host_path", null)
118-
docker_volume_configuration = lookup(volume.value, "docker_volume_configuration", null)
126+
name = volume.value.name
127+
host_path = lookup(volume.value, "host_path", null)
128+
129+
dynamic "docker_volume_configuration" {
130+
for_each = var.docker_volume_configuration
131+
content {
132+
scope = lookup(docker_volume_configuration.value, "scope", null)
133+
autoprovision = lookup(docker_volume_configuration.value, "autoprovision", null)
134+
driver = lookup(docker_volume_configuration.value, "driver", null)
135+
driver_opts = lookup(docker_volume_configuration.value, "driver_opts", null)
136+
labels = lookup(docker_volume_configuration.value, "labels", null)
137+
}
138+
}
119139
}
120140
}
121141

122-
123142
tags = merge(
124143
var.tags,
125144
{
126145
Name = var.container_name != "" ? var.container_name : var.name_prefix
127-
},
146+
}
128147
)
129148
}
130149

outputs.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,66 @@
11
output "task_role_arn" {
22
description = "The Amazon Resource Name (ARN) specifying the ECS service role."
3-
value = aws_iam_role.task.arn
3+
value = join("", aws_iam_role.task.*.arn)
44
}
55

66
output "task_role_name" {
77
description = "The name of the Fargate task service role."
8-
value = aws_iam_role.task.name
8+
value = join("", aws_iam_role.task.*.name)
99
}
1010

1111
output "task_role_create_date" {
1212
description = "The creation date of the IAM role."
13-
value = aws_iam_role.task.create_date
13+
value = join("", aws_iam_role.task.*.create_date)
1414
}
1515

1616
output "task_role_id" {
1717
description = "The ID of the role."
18-
value = aws_iam_role.task.id
18+
value = join("", aws_iam_role.task.*.id)
1919
}
2020

2121
output "task_role_unique_id" {
2222
description = "The stable and unique string identifying the role."
23-
value = aws_iam_role.task.unique_id
23+
value = join("", aws_iam_role.task.*.unique_id)
2424
}
2525

2626
output "execution_role_arn" {
2727
description = "The Amazon Resource Name (ARN) of execution role."
28-
value = aws_iam_role.execution.arn
28+
value = join("", aws_iam_role.execution.*.arn)
2929
}
3030

3131
output "execution_role_name" {
3232
description = "The name of the execution service role."
33-
value = aws_iam_role.execution.name
33+
value = join("", aws_iam_role.execution.*.name)
3434
}
3535

3636
output "execution_role_create_date" {
3737
description = "The creation date of the IAM role."
38-
value = aws_iam_role.execution.create_date
38+
value = join("", aws_iam_role.execution.*.create_date)
3939
}
4040

4141
output "execution_role_id" {
4242
description = "The ID of the execution role."
43-
value = aws_iam_role.execution.id
43+
value = join("", aws_iam_role.execution.*.id)
4444
}
4545

4646
output "execution_role_unique_id" {
4747
description = "The stable and unique string identifying the role."
48-
value = aws_iam_role.execution.unique_id
48+
value = join("", aws_iam_role.execution.*.unique_id)
4949
}
5050

5151
output "task_definition_arn" {
5252
description = "Full ARN of the Task Definition (including both family and revision)."
53-
value = aws_ecs_task_definition.task.arn
53+
value = join("", aws_ecs_task_definition.task.*.arn)
5454
}
5555

5656
output "task_definition_family" {
5757
description = "The family of the Task Definition."
58-
value = aws_ecs_task_definition.task.family
58+
value = join("", aws_ecs_task_definition.task.*.family)
5959
}
6060

61-
output "task_definition_td_revision" {
61+
output "task_definition_revision" {
6262
description = "The revision of the task in a particular family."
63-
value = aws_ecs_task_definition.task.revision
63+
value = join("", aws_ecs_task_definition.task.*.revision)
6464
}
6565

6666
output "container_port" {

variables.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "enabled" {
2+
type = bool
3+
description = "Whether to create the resources. Set to `false` to prevent the module from creating any resources"
4+
default = true
5+
}
16

27
variable "name_prefix" {
38
description = "A prefix used for naming resources."
@@ -52,7 +57,8 @@ variable "task_container_environment" {
5257

5358
variable "cloudwatch_log_group_name" {
5459
description = "CloudWatch log group name required to enabled logDriver in container definitions for ecs task."
55-
type = map(string)
60+
type = string
61+
default = ""
5662
}
5763

5864
variable "tags" {
@@ -92,3 +98,8 @@ variable "volume" {
9298
default = []
9399
}
94100

101+
variable "docker_volume_configuration" {
102+
type = list
103+
description = "(Optional) Used to configure a docker volume option \"docker_volume_configuration\". Full set of options can be found at https://www.terraform.io/docs/providers/aws/r/ecs_task_definition.html"
104+
default = []
105+
}

0 commit comments

Comments
 (0)