Skip to content

Commit af29da3

Browse files
authoredMay 28, 2021
fix: property lookup in ecs_target block (terraform-aws-modules#8)
1 parent 9131d34 commit af29da3

File tree

5 files changed

+70
-14
lines changed

5 files changed

+70
-14
lines changed
 

‎.github/workflows/pre-commit.yml

+5-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: actions/setup-python@v2
3939
- name: Terraform min/max versions
4040
id: minMax
41-
uses: clowdhaus/terraform-min-max@v1.0.1
41+
uses: clowdhaus/terraform-min-max@v1.0.2
4242
with:
4343
directory: ${{ matrix.directory }}
4444
- name: Install Terraform v${{ steps.minMax.outputs.minVersion }}
@@ -50,14 +50,11 @@ jobs:
5050
- name: Execute pre-commit
5151
# Run only validate pre-commit check on min version supported
5252
if: ${{ matrix.directory != '.' }}
53-
run:
54-
pre-commit run terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*
53+
run: pre-commit run terraform_validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*
5554
- name: Execute pre-commit
5655
# Run only validate pre-commit check on min version supported
5756
if: ${{ matrix.directory == '.' }}
58-
run:
59-
pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)
60-
57+
run: pre-commit run terraform_validate --color=always --show-diff-on-failure --files $(ls *.tf)
6158

6259
# Max Terraform version
6360
getBaseVersion:
@@ -68,7 +65,7 @@ jobs:
6865
uses: actions/checkout@v2
6966
- name: Terraform min/max versions
7067
id: minMax
71-
uses: clowdhaus/terraform-min-max@v1.0.1
68+
uses: clowdhaus/terraform-min-max@v1.0.2
7269
outputs:
7370
minVersion: ${{ steps.minMax.outputs.minVersion }}
7471
maxVersion: ${{ steps.minMax.outputs.maxVersion }}
@@ -94,7 +91,7 @@ jobs:
9491
- name: Install pre-commit dependencies
9592
run: |
9693
pip install pre-commit
97-
curl -L "$(curl -s https://api.github.com/repos/terraform-docs/terraform-docs/releases/latest | grep -o -E "https://.+?-v0.12\..+?-linux-amd64" | head -n1)" > terraform-docs && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
94+
curl -Lo ./terraform-docs.tar.gz https://github.com/terraform-docs/terraform-docs/releases/download/v0.13.0/terraform-docs-v0.13.0-$(uname)-amd64.tar.gz && tar -xzf terraform-docs.tar.gz && chmod +x terraform-docs && sudo mv terraform-docs /usr/bin/
9895
curl -L "$(curl -s https://api.github.com/repos/terraform-linters/tflint/releases/latest | grep -o -E "https://.+?_linux_amd64.zip")" > tflint.zip && unzip tflint.zip && rm tflint.zip && sudo mv tflint /usr/bin/
9996
- name: Execute pre-commit
10097
# Run all pre-commit checks on max version supported

‎examples/complete/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Note that this example may create resources which cost money. Run `terraform des
3434

3535
| Name | Source | Version |
3636
|------|--------|---------|
37+
| <a name="module_ecs"></a> [ecs](#module\_ecs) | terraform-aws-modules/ecs/aws | ~> 3.0 |
3738
| <a name="module_eventbridge"></a> [eventbridge](#module\_eventbridge) | ../../ | |
3839
| <a name="module_step_function"></a> [step\_function](#module\_step\_function) | terraform-aws-modules/step-functions/aws | ~> 2.0 |
3940

@@ -42,6 +43,8 @@ Note that this example may create resources which cost money. Run `terraform des
4243
| Name | Type |
4344
|------|------|
4445
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
46+
| [aws_ecs_service.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |
47+
| [aws_ecs_task_definition.hello_world](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_task_definition) | resource |
4548
| [aws_kinesis_stream.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kinesis_stream) | resource |
4649
| [aws_sqs_queue.dlq](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
4750
| [aws_sqs_queue.fifo](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |

‎examples/complete/main.tf

+52
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module "eventbridge" {
3232
attach_cloudwatch_policy = true
3333
cloudwatch_target_arns = [aws_cloudwatch_log_group.this.arn]
3434

35+
attach_ecs_policy = true
36+
ecs_target_arns = [aws_ecs_task_definition.hello_world.arn]
37+
3538
rules = {
3639
orders = {
3740
description = "Capture all order data"
@@ -90,6 +93,15 @@ module "eventbridge" {
9093
dead_letter_arn = aws_sqs_queue.dlq.arn
9194
input_transformer = local.order_input_transformer
9295
attach_role_arn = true
96+
},
97+
{
98+
name = "process-email-with-ecs-task",
99+
arn = module.ecs.ecs_cluster_arn,
100+
attach_role_arn = true
101+
ecs_target = {
102+
task_count = 1
103+
task_definition_arn = aws_ecs_task_definition.hello_world.arn
104+
}
93105
}
94106
]
95107
}
@@ -248,3 +260,43 @@ module "step_function" {
248260
}
249261
}
250262
}
263+
264+
######
265+
# ECS
266+
######
267+
268+
module "ecs" {
269+
source = "terraform-aws-modules/ecs/aws"
270+
version = "~> 3.0"
271+
272+
name = random_pet.this.id
273+
274+
capacity_providers = ["FARGATE", "FARGATE_SPOT"]
275+
}
276+
277+
resource "aws_ecs_service" "hello_world" {
278+
name = "hello_world-${random_pet.this.id}"
279+
cluster = module.ecs.ecs_cluster_id
280+
task_definition = aws_ecs_task_definition.hello_world.arn
281+
launch_type = "FARGATE"
282+
283+
desired_count = 1
284+
285+
deployment_maximum_percent = 100
286+
deployment_minimum_healthy_percent = 0
287+
}
288+
289+
resource "aws_ecs_task_definition" "hello_world" {
290+
family = "hello_world-${random_pet.this.id}"
291+
292+
container_definitions = <<EOF
293+
[
294+
{
295+
"name": "hello_world-${random_pet.this.id}",
296+
"image": "hello-world",
297+
"cpu": 0,
298+
"memory": 128
299+
}
300+
]
301+
EOF
302+
}

‎iam.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ data "aws_iam_policy_document" "ecs" {
171171
sid = "ECSAccess"
172172
effect = "Allow"
173173
actions = ["ecs:RunTask"]
174-
resources = var.ecs_target_arns
174+
resources = [for arn in var.ecs_target_arns : replace(arn, "/:\\d+$/", ":*")]
175175
}
176176

177177
statement {
178178
sid = "PassRole"
179179
effect = "Allow"
180180
actions = ["iam:PassRole"]
181-
resources = [aws_iam_role.eventbridge[0].arn]
181+
resources = ["*"]
182182
}
183183
}
184184

‎main.tf

+8-4
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,24 @@ resource "aws_cloudwatch_event_target" "this" {
7070
}
7171

7272
dynamic "ecs_target" {
73-
for_each = lookup(each.value, "ecs_target", null) != null ? [true] : []
73+
for_each = lookup(each.value, "ecs_target", null) != null ? [
74+
each.value.ecs_target
75+
] : []
7476

7577
content {
7678
group = lookup(ecs_target.value, "group", null)
7779
launch_type = lookup(ecs_target.value, "launch_type", null)
7880
platform_version = lookup(ecs_target.value, "platform_version", null)
7981
task_count = lookup(ecs_target.value, "task_count", null)
80-
task_definition_arn = ecs_target.value.task_definition_arn
82+
task_definition_arn = lookup(ecs_target.value, "task_definition_arn", null)
8183

8284
dynamic "network_configuration" {
83-
for_each = lookup(ecs_target.value, "network_configuration", null) != null ? [true] : []
85+
for_each = lookup(each.value.ecs_target, "network_configuration", null) != null ? [
86+
each.value.ecs_target.network_configuration
87+
] : []
8488

8589
content {
86-
subnets = network_configuration.value.subnets
90+
subnets = lookup(network_configuration.value, "subnets", null)
8791
security_groups = lookup(network_configuration.value, "security_groups", null)
8892
assign_public_ip = lookup(network_configuration.value, "assign_public_ip", null)
8993
}

0 commit comments

Comments
 (0)
Please sign in to comment.