-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1774e0f0e3285f02ed4e88baf1576136bb8458ef | ||
a4b4cf4c78bcf201717d11eb6bf5a2ce51124795 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Environment variables and secrets | ||
|
||
Applications follow [12-factor app](https://12factor.net/) principles to [store configuration as environment variables](https://12factor.net/config). The infrastructure provides some of these environment variables automatically, such as environment variables to authenticate as the ECS task role, environment variables for database access, and environment variables for accessing document storage. However, many applications require extra custom environment variables for application configuration and for access to secrets. This document describes how to configure application-specific environment variables and secrets. It also describes how to override those environment variables for a specific environment. | ||
|
||
## Application-specific extra environment variables | ||
|
||
Applications may need application specific configuration as environment variables. Examples may includes things like `WORKER_THREADS_COUNT`, `LOG_LEVEL`, `DB_CONNECTION_POOL_SIZE`, or `SERVER_TIMEOUT`. This section describes how to define extra environment variables for your application that are then made accessible to the ECS task by defining the environment variables in the task definition (see AWS docs on [using task definition parameters to pass environment variables to a container](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/taskdef-envfiles.html)). | ||
|
||
> ⚠️ Note: Do not put sensitive information such as credentials as regular environment variables. The method described in this section will embed the environment variables and their values in the ECS task definition's container definitions, so anyone with access to view the task definition will be able to see the values of the environment variables. For configuring secrets, see the section below on [Secrets](#secrets) | ||
Environment variables are defined in the `app-config` module in the [environment-variables.tf file](/infra/app/app-config/env-config/environment-variables.tf). Modify the `default_extra_environment_variables` map to define extra environment variables specific to the application. Map keys define the environment variable name, and values define the default value for the variable across application environments. For example: | ||
|
||
```terraform | ||
# environment-variables.tf | ||
locals { | ||
default_extra_environment_variables = { | ||
WORKER_THREADS_COUNT = 4 | ||
LOG_LEVEL = "info" | ||
} | ||
} | ||
``` | ||
|
||
To override the default values for a particular environment, modify the `app-config/[environment].tf file` for the environment, and pass overrides to the `env-config` module using the `service_override_extra_environment_variables` variable. For example: | ||
|
||
```terraform | ||
# dev.tf | ||
module "dev_config" { | ||
source = "./env-config" | ||
service_override_extra_environment_variables = { | ||
WORKER_THREADS_COUNT = 1 | ||
LOG_LEVEL = "debug" | ||
} | ||
... | ||
} | ||
``` | ||
|
||
## Secrets | ||
|
||
Secrets are a specific category of environment variables that need to be handled sensitively. Examples of secrets are authentication credentials such as API keys for external services. Secrets first need to be stored in AWS SSM Parameter Store as a `SecureString`. This section then describes how to make those secrets accessible to the ECS task as environment variables through the `secrets` configuration in the container definition (see AWS documentation on [retrieving Secrets Manager secrets through environment variables](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/secrets-envvar-secrets-manager.html)). | ||
|
||
Secrets are defined in the same file that non-sensitive environment variables are defined, in the `app-config` module in the [environment-variables.tf file](/infra/app/app-config/env-config/environment-variables.tf). Modify the `secrets` list to define the secrets that the application will have access to. For each secret, `name` defines the environment variable name, and `ssm_param_name` defines the SSM parameter name that stores the secret value. For example: | ||
|
||
```terraform | ||
# environment-variables.tf | ||
locals { | ||
secrets = [ | ||
{ | ||
name = "SOME_API_KEY" | ||
ssm_param_name = "/${var.app_name}-${var.environment}/secret-sauce" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
> ⚠️ Make sure you store the secret in SSM Parameter Store before you try to add secrets to your application service, or else the service won't be able to start since the ECS Task Executor won't be able to fetch the configured secret. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
locals { | ||
# Map from environment variable name to environment variable value | ||
# This is a map rather than a list so that variables can be easily | ||
# overridden per environment using terraform's `merge` function | ||
default_extra_environment_variables = { | ||
# Example environment variables | ||
# WORKER_THREADS_COUNT = 4 | ||
# LOG_LEVEL = "info" | ||
# DB_CONNECTION_POOL_SIZE = 5 | ||
} | ||
|
||
# Configuration for secrets | ||
# List of configurations for defining environment variables that pull from SSM parameter | ||
# store. Configurations are of the format | ||
# { name = "ENV_VAR_NAME", ssm_param_name = "/ssm/param/name" } | ||
secrets = [{ | ||
name = "API_AUTH_TOKEN" | ||
ssm_param_name = "/${var.app_name}-${var.environment}/api-auth-token" | ||
}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
data "aws_ssm_parameter" "secret" { | ||
for_each = { for secret in var.secrets : secret.name => secret } | ||
name = each.value.ssm_param_name | ||
} | ||
|
||
locals { | ||
secrets = [ | ||
for secret in var.secrets : | ||
{ | ||
name = secret.name, | ||
valueFrom = data.aws_ssm_parameter.secret[secret.name].arn | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters