Skip to content

Commit

Permalink
[#103] Improve the way to manage environment variables and secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangmirs committed Sep 21, 2022
1 parent d4e2252 commit cdc6e4d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
10 changes: 10 additions & 0 deletions skeleton/aws/modules/ecs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ data "aws_ecr_repository" "repo" {
}

locals {
# Environment variables from other variables
environment_variables = toset([
{
name = "AWS_REGION"
value = var.region
}
])

container_vars = {
namespace = var.namespace
region = var.region
Expand All @@ -15,6 +23,8 @@ locals {
aws_ecr_repository = data.aws_ecr_repository.repo.repository_url
aws_ecr_tag = var.ecr_tag
aws_cloudwatch_log_group_name = var.aws_cloudwatch_log_group_name

environment_variables = setunion(local.environment_variables, var.environment_variables)
}

container_definitions = templatefile("${path.module}/service.json.tftpl", merge(local.container_vars, var.aws_parameter_store))
Expand Down
3 changes: 1 addition & 2 deletions skeleton/aws/modules/ecs/service.json.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"awslogs-group": "${aws_cloudwatch_log_group_name}"
}
},
"environment": [
],
"environment": ${jsonencode(environment_variables)},
"secrets": [
],
"ulimits": [
Expand Down
8 changes: 8 additions & 0 deletions skeleton/aws/modules/ecs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,11 @@ variable "aws_parameter_store" {
description = "AWS parameter store"
type = map(any)
}

variable "environment_variables" {
description = "List of [{name = \"\", value = \"\"}] pairs of environment variables"
type = set(object({
name = string
value = string
}))
}
34 changes: 33 additions & 1 deletion src/templates/aws/addons/ecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,38 @@ const ecsVariablesContent = dedent`
deployment_minimum_healthy_percent = number
})
}
variable "environment_variables" {
description = "List of [{name = \"\", value = \"\"}] pairs of environment variables"
type = set(object({
name = string
value = string
}))
default = [
{
name = "AVAILABLE_LOCALES"
value = "en"
},
{
name = "DEFAULT_LOCALE"
value = "en"
},
{
name = "FALLBACK_LOCALES"
value = "en"
},
{
name = "MAILER_DEFAULT_HOST"
value = "localhost"
},
{
name = "MAILER_DEFAULT_PORT"
value = "80"
},
]
}
\n`;

const ecsModuleContent = dedent`
module "ecs" {
source = "./modules/ecs"
Expand All @@ -47,7 +78,8 @@ const ecsModuleContent = dedent`
deployment_minimum_healthy_percent = var.ecs.deployment_minimum_healthy_percent
container_memory = var.ecs.task_container_memory
aws_parameter_store = module.ssm.parameter_store
aws_parameter_store = module.ssm.parameter_store
environment_variables = var.environment_variables
}
\n`;

Expand Down

0 comments on commit cdc6e4d

Please sign in to comment.