From e9787a7413f2ae6c28ad222cdef29bcb4ab03f07 Mon Sep 17 00:00:00 2001 From: Joseph Ligier Date: Tue, 23 Jun 2020 23:45:05 +0200 Subject: [PATCH] Add map_environment variable (#63) Co-authored-by: Joseph Co-authored-by: nitro Co-authored-by: actions-bot <58130806+actions-bot@users.noreply.github.com> Co-authored-by: Andriy Knysh --- README.md | 1 + docs/terraform.md | 1 + examples/map_environment/main.tf | 17 +++++++++++++++++ examples/map_environment/versions.tf | 7 +++++++ main.tf | 18 +++++++++--------- variables.tf | 6 ++++++ 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 examples/map_environment/main.tf create mode 100644 examples/map_environment/versions.tf diff --git a/README.md b/README.md index 19179af..03b178f 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,7 @@ No provider. | links | List of container names this container can communicate with without port mappings | `list(string)` | `null` | no | | linux\_parameters | Linux-specific modifications that are applied to the container, such as Linux kernel capabilities. For more details, see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LinuxParameters.html |
object({
capabilities = object({
add = list(string)
drop = list(string)
})
devices = list(object({
containerPath = string
hostPath = string
permissions = list(string)
}))
initProcessEnabled = bool
maxSwap = number
sharedMemorySize = number
swappiness = number
tmpfs = list(object({
containerPath = string
mountOptions = list(string)
size = number
}))
})
| `null` | no | | log\_configuration | Log configuration options to send to a custom log driver for the container. For more details, see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LogConfiguration.html | `any` | `null` | no | +| map\_environment | The environment variables to pass to the container. This is a map of string: {key: value}, environment override map\_environment | `map(string)` | `null` | no | | mount\_points | Container mount points. This is a list of maps, where each map should contain a `containerPath` and `sourceVolume`. The `readOnly` key is optional. | `list` | `[]` | no | | port\_mappings | The port mappings to configure for the container. This is a list of maps. Each map should contain "containerPort", "hostPort", and "protocol", where "protocol" is one of "tcp" or "udp". If using containers in a task with the awsvpc or host network mode, the hostPort can either be left blank or set to the same value as the containerPort |
list(object({
containerPort = number
hostPort = number
protocol = string
}))
| `[]` | no | | privileged | When this variable is `true`, the container is given elevated privileges on the host container instance (similar to the root user). This parameter is not supported for Windows containers or tasks using the Fargate launch type. | `bool` | `null` | no | diff --git a/docs/terraform.md b/docs/terraform.md index 9276586..e6a32bc 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -34,6 +34,7 @@ No provider. | links | List of container names this container can communicate with without port mappings | `list(string)` | `null` | no | | linux\_parameters | Linux-specific modifications that are applied to the container, such as Linux kernel capabilities. For more details, see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LinuxParameters.html |
object({
capabilities = object({
add = list(string)
drop = list(string)
})
devices = list(object({
containerPath = string
hostPath = string
permissions = list(string)
}))
initProcessEnabled = bool
maxSwap = number
sharedMemorySize = number
swappiness = number
tmpfs = list(object({
containerPath = string
mountOptions = list(string)
size = number
}))
})
| `null` | no | | log\_configuration | Log configuration options to send to a custom log driver for the container. For more details, see https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_LogConfiguration.html | `any` | `null` | no | +| map\_environment | The environment variables to pass to the container. This is a map of string: {key: value}, environment override map\_environment | `map(string)` | `null` | no | | mount\_points | Container mount points. This is a list of maps, where each map should contain a `containerPath` and `sourceVolume`. The `readOnly` key is optional. | `list` | `[]` | no | | port\_mappings | The port mappings to configure for the container. This is a list of maps. Each map should contain "containerPort", "hostPort", and "protocol", where "protocol" is one of "tcp" or "udp". If using containers in a task with the awsvpc or host network mode, the hostPort can either be left blank or set to the same value as the containerPort |
list(object({
containerPort = number
hostPort = number
protocol = string
}))
| `[]` | no | | privileged | When this variable is `true`, the container is given elevated privileges on the host container instance (similar to the root user). This parameter is not supported for Windows containers or tasks using the Fargate launch type. | `bool` | `null` | no | diff --git a/examples/map_environment/main.tf b/examples/map_environment/main.tf new file mode 100644 index 0000000..bfc1ecf --- /dev/null +++ b/examples/map_environment/main.tf @@ -0,0 +1,17 @@ +module "container" { + source = "../../" + container_name = "name" + container_image = "cloudposse/geodesic" + + map_environment = { + "string_var" = "I am a string" + "true_boolean_var" = true + "false_boolean_var" = false + "integer_var" = 42 + } +} + +output "json" { + description = "Container definition in JSON format" + value = module.container.json +} diff --git a/examples/map_environment/versions.tf b/examples/map_environment/versions.tf new file mode 100644 index 0000000..f4d5a1b --- /dev/null +++ b/examples/map_environment/versions.tf @@ -0,0 +1,7 @@ +terraform { + required_version = "~> 0.12.0" + + required_providers { + local = "~> 1.2" + } +} diff --git a/main.tf b/main.tf index 3ed635a..632701d 100644 --- a/main.tf +++ b/main.tf @@ -1,8 +1,8 @@ locals { # Sort environment variables so terraform will not try to recreate on each plan/apply env_vars = var.environment != null ? var.environment : [] - env_vars_keys = [for m in local.env_vars : lookup(m, "name")] - env_vars_values = [for m in local.env_vars : lookup(m, "value")] + env_vars_keys = var.map_environment != null ? keys(var.map_environment) : [for m in local.env_vars : lookup(m, "name")] + env_vars_values = var.map_environment != null ? values(var.map_environment) : [for m in local.env_vars : lookup(m, "value")] env_vars_as_map = zipmap(local.env_vars_keys, local.env_vars_values) sorted_env_vars_keys = sort(local.env_vars_keys) @@ -22,6 +22,13 @@ locals { } ] : var.mount_points + # This strange-looking variable is needed because terraform (currently) does not support explicit `null` in ternary operator, + # so this does not work: final_environment_vars = length(local.sorted_environment_vars) > 0 ? local.sorted_environment_vars : null + null_value = var.environment == null ? var.environment : null + + # https://www.terraform.io/docs/configuration/expressions.html#null + final_environment_vars = length(local.sorted_environment_vars) > 0 ? local.sorted_environment_vars : local.null_value + log_configuration_secret_options = var.log_configuration != null ? lookup(var.log_configuration, "secretOptions", null) : null log_configuration_with_null = var.log_configuration == null ? null : { logDriver = tostring(lookup(var.log_configuration, "logDriver")) @@ -39,13 +46,6 @@ locals { if v != null } - # This strange-looking variable is needed because terraform (currently) does not support explicit `null` in ternary operator, - # so this does not work: final_environment_vars = length(local.sorted_environment_vars) > 0 ? local.sorted_environment_vars : null - null_value = var.environment == null ? var.environment : null - - # https://www.terraform.io/docs/configuration/expressions.html#null - final_environment_vars = length(local.sorted_environment_vars) > 0 ? local.sorted_environment_vars : local.null_value - container_definition = { name = var.container_name image = var.container_image diff --git a/variables.tf b/variables.tf index 8249989..dea9263 100644 --- a/variables.tf +++ b/variables.tf @@ -99,6 +99,12 @@ variable "extra_hosts" { default = null } +variable "map_environment" { + type = map(string) + description = "The environment variables to pass to the container. This is a map of string: {key: value}, environment override map_environment" + default = null +} + # https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_EnvironmentFile.html variable "environment_files" { type = list(object({