Skip to content

Commit

Permalink
Add map_environment variable (#63)
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph <[email protected]>
Co-authored-by: nitro <[email protected]>
Co-authored-by: actions-bot <[email protected]>
Co-authored-by: Andriy Knysh <[email protected]>
  • Loading branch information
5 people authored Jun 23, 2020
1 parent 2155507 commit e9787a7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | <pre>object({<br> capabilities = object({<br> add = list(string)<br> drop = list(string)<br> })<br> devices = list(object({<br> containerPath = string<br> hostPath = string<br> permissions = list(string)<br> }))<br> initProcessEnabled = bool<br> maxSwap = number<br> sharedMemorySize = number<br> swappiness = number<br> tmpfs = list(object({<br> containerPath = string<br> mountOptions = list(string)<br> size = number<br> }))<br> })</pre> | `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 | <pre>list(object({<br> containerPort = number<br> hostPort = number<br> protocol = string<br> }))</pre> | `[]` | 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 |
Expand Down
1 change: 1 addition & 0 deletions docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | <pre>object({<br> capabilities = object({<br> add = list(string)<br> drop = list(string)<br> })<br> devices = list(object({<br> containerPath = string<br> hostPath = string<br> permissions = list(string)<br> }))<br> initProcessEnabled = bool<br> maxSwap = number<br> sharedMemorySize = number<br> swappiness = number<br> tmpfs = list(object({<br> containerPath = string<br> mountOptions = list(string)<br> size = number<br> }))<br> })</pre> | `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 | <pre>list(object({<br> containerPort = number<br> hostPort = number<br> protocol = string<br> }))</pre> | `[]` | 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 |
Expand Down
17 changes: 17 additions & 0 deletions examples/map_environment/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions examples/map_environment/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
terraform {
required_version = "~> 0.12.0"

required_providers {
local = "~> 1.2"
}
}
18 changes: 9 additions & 9 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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"))
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit e9787a7

Please sign in to comment.