Skip to content

Commit be397ec

Browse files
authored
Allow Task and Execution roles as inputs (#38)
* feat: Allow Task and Execution roles as inputs * fix: Error: Inconsistent conditional result types
1 parent 617e0fe commit be397ec

File tree

9 files changed

+57
-46
lines changed

9 files changed

+57
-46
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ Temporary Items
233233
# Local .terraform directories
234234
**/.terraform/*
235235

236+
# Terraform lock file
237+
.terraform.lock.hcl
238+
236239
# .tfstate files
237240
*.tfstate
238241
*.tfstate.*
@@ -646,4 +649,4 @@ healthchecksdb
646649
# Backup folder for Package Reference Convert tool in Visual Studio 2017
647650
MigrationBackup/
648651

649-
# End of https://www.gitignore.io/api/linux,macos,windows,terraform,jetbrains,visualstudio,jetbrains+iml,jetbrains+all,visualstudiocode
652+
# End of https://www.gitignore.io/api/linux,macos,windows,terraform,jetbrains,visualstudio,jetbrains+iml,jetbrains+all,visualstudiocode

.pre-commit-config.yaml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
1+
fail_fast: true
2+
13
repos:
24
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.71.0
5+
rev: v1.77.1 # Get the latest from: https://github.com/antonbabenko/pre-commit-terraform/releases
46
hooks:
5-
- id: terraform_docs
67
- id: terraform_fmt
8+
- id: terraform_docs
9+
args: ["--args=--lockfile=false"]
710
- id: terraform_validate
11+
- repo: https://github.com/pre-commit/pre-commit-hooks
12+
rev: v4.4.0 # Get the latest from: https://github.com/pre-commit/pre-commit-hooks/releases
13+
hooks:
14+
- id: check-merge-conflict
15+
- id: end-of-file-fixer
16+
- id: trailing-whitespace
17+
- id: check-added-large-files
18+
- id: check-case-conflict
19+
- id: detect-private-key
20+
- id: check-yaml
21+
files: ^(.github/workflows).*$

CODE_OF_CONDUCT.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Additionally, community organizers are available to help community members engag
6565

6666
## 8. Addressing Grievances
6767

68-
If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify CN Services with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies.
68+
If you feel you have been falsely or unfairly accused of violating this Code of Conduct, you should notify CN Services with a concise description of your grievance. Your grievance will be handled in accordance with our existing governing policies.
6969

7070

7171

@@ -81,7 +81,7 @@ [email protected]
8181

8282
## 11. License and attribution
8383

84-
The Citizen Code of Conduct is distributed by [Stumptown Syndicate](http://stumptownsyndicate.org) under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/).
84+
The Citizen Code of Conduct is distributed by [Stumptown Syndicate](http://stumptownsyndicate.org) under a [Creative Commons Attribution-ShareAlike license](http://creativecommons.org/licenses/by-sa/3.0/).
8585

8686
Portions of text derived from the [Django Code of Conduct](https://www.djangoproject.com/conduct/) and the [Geek Feminism Anti-Harassment Policy](http://geekfeminism.wikia.com/wiki/Conference_anti-harassment/Policy).
8787

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ Pleas run this command right after cloning the repository.
2121

2222
pre-commit install
2323

24-
For that you may need to install the folowwing tools:
25-
* [Pre-commit](https://pre-commit.com/)
24+
For that you may need to install the following tools:
25+
* [Pre-commit](https://pre-commit.com/)
2626
* [Terraform Docs](https://terraform-docs.io/)
2727

2828
In order to run all checks at any point run the following command:
@@ -35,19 +35,19 @@ In order to run all checks at any point run the following command:
3535
| Name | Version |
3636
|------|---------|
3737
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13 |
38-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4 |
38+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
3939

4040
## Providers
4141

4242
| Name | Version |
4343
|------|---------|
44-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.0.0 |
44+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 |
4545

4646
## Modules
4747

4848
| Name | Source | Version |
4949
|------|--------|---------|
50-
| <a name="module_container_definition"></a> [container\_definition](#module\_container\_definition) | cloudposse/ecs-container-definition/aws | 0.58.1 |
50+
| <a name="module_container_definition"></a> [container\_definition](#module\_container\_definition) | cloudposse/ecs-container-definition/aws | 0.58.2 |
5151

5252
## Resources
5353

@@ -83,6 +83,7 @@ In order to run all checks at any point run the following command:
8383
| <a name="input_environment_files"></a> [environment\_files](#input\_environment\_files) | One or more files containing the environment variables to pass to the container. This maps to the --env-file option to docker run. The file must be hosted in Amazon S3. This option is only available to tasks using the EC2 launch type. This is a list of maps | <pre>list(object({<br> value = string<br> type = string<br> }))</pre> | `[]` | no |
8484
| <a name="input_ephemeral_storage_size"></a> [ephemeral\_storage\_size](#input\_ephemeral\_storage\_size) | The number of GBs to provision for ephemeral storage on Fargate tasks. Must be greater than or equal to 21 and less than or equal to 200 | `number` | `0` | no |
8585
| <a name="input_essential"></a> [essential](#input\_essential) | Determines whether all other containers in a task are stopped, if this container fails or stops for any reason. Due to how Terraform type casts booleans in json it is required to double quote this value | `bool` | `true` | no |
86+
| <a name="input_execution_role_arn"></a> [execution\_role\_arn](#input\_execution\_role\_arn) | (Optional) The ARN of IAM role that grants permissions to start the containers defined in a task (e.g populate environment variables from AWS Secrets Manager). If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used | `string` | `null` | no |
8687
| <a name="input_extra_hosts"></a> [extra\_hosts](#input\_extra\_hosts) | A list of hostnames and IP address mappings to append to the /etc/hosts file on the container. This is a list of maps | <pre>list(object({<br> ipAddress = string<br> hostname = string<br> }))</pre> | `null` | no |
8788
| <a name="input_firelens_configuration"></a> [firelens\_configuration](#input\_firelens\_configuration) | The FireLens configuration for the container. This is used to specify and configure a log router for container logs. For more details, see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_FirelensConfiguration.html | <pre>object({<br> type = string<br> options = map(string)<br> })</pre> | `null` | no |
8889
| <a name="input_healthcheck"></a> [healthcheck](#input\_healthcheck) | (Optional) A map containing command (string), timeout, interval (duration in seconds), retries (1-10, number of times to retry before marking container unhealthy), and startPeriod (0-300, optional grace period to wait, in seconds, before failed healthchecks count toward retries) | <pre>object({<br> command = list(string)<br> retries = number<br> timeout = number<br> interval = number<br> startPeriod = number<br> })</pre> | `null` | no |
@@ -115,7 +116,7 @@ In order to run all checks at any point run the following command:
115116
| <a name="input_stop_timeout"></a> [stop\_timeout](#input\_stop\_timeout) | Time duration (in seconds) to wait before the container is forcefully killed if it doesn't exit normally on its own | `number` | `null` | no |
116117
| <a name="input_system_controls"></a> [system\_controls](#input\_system\_controls) | A list of namespaced kernel parameters to set in the container, mapping to the --sysctl option to docker run. This is a list of maps: { namespace = "", value = ""} | `list(map(string))` | `[]` | no |
117118
| <a name="input_tags"></a> [tags](#input\_tags) | Resource tags | `map(string)` | `{}` | no |
118-
| <a name="input_task_role_arn"></a> [task\_role\_arn](#input\_task\_role\_arn) | (Optional) The ARN of IAM role that allows your Amazon ECS container task to make calls to other AWS services. If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used | `string` | `null` | no |
119+
| <a name="input_task_role_arn"></a> [task\_role\_arn](#input\_task\_role\_arn) | (Optional) The ARN of IAM role that grants permissions to the actual application once the container is started (e.g access an S3 bucket or DynamoDB database). If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used | `string` | `null` | no |
119120
| <a name="input_ulimits"></a> [ulimits](#input\_ulimits) | Container ulimit settings. This is a list of maps, where each map should contain "name", "hardLimit" and "softLimit" | <pre>list(object({<br> name = string<br> hardLimit = number<br> softLimit = number<br> }))</pre> | `null` | no |
120121
| <a name="input_user"></a> [user](#input\_user) | The user to run as inside the container. Can be any of these formats: user, user:group, uid, uid:gid, user:gid, uid:group. The default (null) will use the container's configured `USER` directive or root if not set. | `string` | `null` | no |
121122
| <a name="input_volumes"></a> [volumes](#input\_volumes) | (Optional) A set of volume blocks that containers in your task may use | <pre>list(object({<br> host_path = string<br> name = string<br> docker_volume_configuration = list(object({<br> autoprovision = bool<br> driver = string<br> driver_opts = map(string)<br> labels = map(string)<br> scope = string<br> }))<br> efs_volume_configuration = list(object({<br> file_system_id = string<br> root_directory = string<br> transit_encryption = string<br> transit_encryption_port = string<br> authorization_config = list(object({<br> access_point_id = string<br> iam = string<br> }))<br> }))<br> }))</pre> | `[]` | no |

examples/test/.terraform.lock.hcl

Lines changed: 1 addition & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
# AWS ECS Task Execution Role
33
#------------------------------------------------------------------------------
44
resource "aws_iam_role" "ecs_task_execution_role" {
5+
count = var.execution_role_arn == null ? 1 : 0
56
name = "${var.name_prefix}-ecs-task-execution-role"
67
assume_role_policy = file("${path.module}/files/iam/ecs_task_execution_iam_role.json")
78
permissions_boundary = var.permissions_boundary
89
tags = var.tags
910
}
1011

1112
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_policy_attach" {
12-
role = aws_iam_role.ecs_task_execution_role.name
13+
count = var.execution_role_arn == null ? 1 : 0
14+
role = aws_iam_role.ecs_task_execution_role[0].name
1315
policy_arn = "arn:${var.iam_partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
1416
}
1517

1618
resource "aws_iam_policy" "ecs_task_execution_role_custom_policy" {
17-
count = length(var.ecs_task_execution_role_custom_policies)
19+
count = var.execution_role_arn == null ? length(var.ecs_task_execution_role_custom_policies) : 0
1820
name = "${var.name_prefix}-ecs-task-execution-role-custom-policy-${count.index}"
1921
description = "A custom policy for ${var.name_prefix}-ecs-task-execution-role IAM Role"
2022
policy = var.ecs_task_execution_role_custom_policies[count.index]
2123
tags = var.tags
2224
}
2325

2426
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_custom_policy" {
25-
count = length(var.ecs_task_execution_role_custom_policies)
26-
role = aws_iam_role.ecs_task_execution_role.name
27+
count = var.execution_role_arn == null ? length(var.ecs_task_execution_role_custom_policies) : 0
28+
role = aws_iam_role.ecs_task_execution_role[0].name
2729
policy_arn = aws_iam_policy.ecs_task_execution_role_custom_policy[count.index].arn
2830
}
2931

@@ -85,7 +87,8 @@ resource "aws_ecs_task_definition" "td" {
8587
family = var.name_prefix
8688

8789
cpu = var.container_cpu
88-
execution_role_arn = aws_iam_role.ecs_task_execution_role.arn
90+
task_role_arn = var.task_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].arn : var.task_role_arn
91+
execution_role_arn = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].arn : var.execution_role_arn
8992
ipc_mode = var.ipc_mode
9093
memory = var.container_memory
9194
network_mode = "awsvpc" # awsvpc required for Fargate tasks
@@ -123,7 +126,6 @@ resource "aws_ecs_task_definition" "td" {
123126

124127
requires_compatibilities = ["FARGATE"]
125128
skip_destroy = var.skip_destroy
126-
task_role_arn = var.task_role_arn == null ? aws_iam_role.ecs_task_execution_role.arn : var.task_role_arn
127129

128130
dynamic "volume" {
129131
for_each = var.volumes

outputs.tf

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@
33
#------------------------------------------------------------------------------
44
output "aws_iam_role_ecs_task_execution_role_arn" {
55
description = "The Amazon Resource Name (ARN) specifying the role."
6-
value = aws_iam_role.ecs_task_execution_role.arn
6+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].arn : var.execution_role_arn
77
}
8+
89
output "aws_iam_role_ecs_task_execution_role_create_date" {
910
description = "The creation date of the IAM role."
10-
value = aws_iam_role.ecs_task_execution_role.create_date
11+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].create_date : null
1112
}
13+
1214
output "aws_iam_role_ecs_task_execution_role_description" {
1315
description = "The description of the role."
14-
value = aws_iam_role.ecs_task_execution_role.description
16+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].description : null
1517
}
18+
1619
output "aws_iam_role_ecs_task_execution_role_id" {
1720
description = "The ID of the role."
18-
value = aws_iam_role.ecs_task_execution_role.id
21+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].id : null
1922
}
23+
2024
output "aws_iam_role_ecs_task_execution_role_name" {
2125
description = "The name of the role."
22-
value = aws_iam_role.ecs_task_execution_role.name
26+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].name : null
2327
}
28+
2429
output "aws_iam_role_ecs_task_execution_role_unique_id" {
2530
description = "The stable and unique string identifying the role."
26-
value = aws_iam_role.ecs_task_execution_role.unique_id
31+
value = var.execution_role_arn == null ? aws_iam_role.ecs_task_execution_role[0].unique_id : null
2732
}
2833

2934
#------------------------------------------------------------------------------

variables.tf

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,13 @@ variable "skip_destroy" {
433433
}
434434

435435
variable "task_role_arn" {
436-
description = "(Optional) The ARN of IAM role that allows your Amazon ECS container task to make calls to other AWS services. If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used"
436+
description = "(Optional) The ARN of IAM role that grants permissions to the actual application once the container is started (e.g access an S3 bucket or DynamoDB database). If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used"
437+
type = string
438+
default = null
439+
}
440+
441+
variable "execution_role_arn" {
442+
description = "(Optional) The ARN of IAM role that grants permissions to start the containers defined in a task (e.g populate environment variables from AWS Secrets Manager). If not specified, `aws_iam_role.ecs_task_execution_role.arn` is used"
437443
type = string
438444
default = null
439445
}

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ terraform {
33
required_providers {
44
aws = {
55
source = "hashicorp/aws"
6-
version = ">= 4"
6+
version = ">= 4.0.0"
77
}
88
}
99
}

0 commit comments

Comments
 (0)