Skip to content

Commit 01aed41

Browse files
committed
Change way of filtering out null and empty arrays
1 parent b9231c9 commit 01aed41

File tree

3 files changed

+16
-33
lines changed

3 files changed

+16
-33
lines changed

main.tf

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
11
/**
22
* # Terraform AWS ECS Container Definition
33
*
4-
* Introducing the AWS ECS Container Definitions Terraform Module, a highly
5-
* optimized solution for creating and managing your container definitions
6-
* within Amazon Web Services. This module has been expertly crafted by our
7-
* team, who have years of experience working with AWS and Terraform.
8-
*
9-
* We have taken the time to fine-tune the settings and configurations to
10-
* provide you with the best possible experience when using this module. Our
11-
* team is comprised of experts in AWS and Terraform, and we are proud to share
12-
* our knowledge and expertise with you.
13-
*
14-
* This Terraform module offers a preconfigured solution for managing your
15-
* container definitions, allowing you to focus on developing your applications
16-
* and not on the infrastructure setup. By using this module, you can be
17-
* confident that your container definitions are created and managed in a
18-
* secure, scalable, and efficient manner.
19-
*
20-
* So, whether you're a seasoned AWS user or just starting out, the AWS ECS
21-
* Container Definitions Terraform Module is the perfect solution for managing
22-
* your container definitions. Give it a try and see the difference it can make
23-
* in your workflow!
4+
* This module is used to generate a container definition for use in an AWS ECS task definition.
245
*/
256
locals {
26-
# TODO: Filter out null values on the container definition.
277
container_definition = {
288
name = var.name
299
image = var.image
@@ -127,15 +107,11 @@ locals {
127107
credentialsParameter : var.repository_credentials
128108
} : null
129109
}
110+
}
130111

131-
filtered_container_definition = { for k, v in local.container_definition : k => v if v != null && v != [] }
132-
filtered_port_mappings = [for pm in local.container_definition.portMappings : { for k, v in pm : k => v if v != null && v != [] }]
133-
134-
# Merge all filtered values into one definition
135-
final_container_definition = merge(
136-
local.filtered_container_definition,
137-
{
138-
portMappings = local.filtered_port_mappings
139-
}
140-
)
112+
# AWS will complain if we send any optional values with a null value. A simple way to get around this is to use jq
113+
# to remove any null values and empty arrays from the JSON before sending it to AWS.
114+
data "jq_query" "main" {
115+
query = "del(.. | nulls) | del(.. | select(. == []))"
116+
data = jsonencode(local.container_definition)
141117
}

outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
output "json" {
22
description = "Rendered container definition as JSON output."
3-
value = jsonencode(local.final_container_definition)
3+
value = data.jq_query.main.result
44
}
55

66
output "hcl" {
77
description = "Rendered container definition as HCL object."
8-
value = local.final_container_definition
8+
value = jsondecode(data.jq_query.main.result)
99
}

versions.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
terraform {
22
required_version = ">= 1.3"
3+
4+
required_providers {
5+
jq = {
6+
source = "massdriver-cloud/jq"
7+
version = "0.2.1"
8+
}
9+
}
310
}

0 commit comments

Comments
 (0)