An end-to-end example of using AWS stored key-value pairs as docker environment variables and consuming them in applications therein.
Note: If running locally, you can skip these steps and jump to "Usage"
Create your AWS resources:
- A Secrets Manager secret, EG:
your/aws/secret/id
- A role for the ECS task (Or use the default AWS ECS role)
- IAM policy granting the ECS task role permission to decrypt the secret (This is important!)
- Resource policy granting ECS permission to create CloudWatch events (If using custom ECS roles)
- A task definition, with container definitions as specified in "Usage", mapping the id of the secret you created to the
env
environment variable
- Implement the secrets/parameters and relevant IAM policies in "Pre-requisites"
- Choose a method from "Image options" below to run the image
- Either:
- View the console log output to see the vars at the O/S level
- View the web app output on http/80 to see them passed to nginx/php
- Profit!
Build and run it locally (Mapping port 8080 on the host to 80 in the container):
$ git clone https://github.com/davidsuart/awesome-envvars.git
$ docker build --no-cache --tag awesome-envvars github.com/davidsuart/awesome-envvars
$ docker run --env-file=env_file --publish 8080:80 awesome-envvars env
AWS Fargate it (Including these parameters within your task definition):
container_definitions = [
{
"image": "davidsuart/awesome-envvars:latest",
"portMappings": [{ "containerPort": 80, "hostPort": null, "protocol": "tcp" }],
"logConfiguration": { "logDriver": "awslogs" },
"secrets": [{ "name": "env", "valueFrom": "<your/aws/secret/id>" }]
}
]
- If you are not using AWS defaults for ECS task role and task execution role, ensure that your roles have the appropriate privilege assignments to run tasks, access secrets and write to cloudwatch logs
- Ensure that the subnet you place the ECS cluster/service in has internet access so it can download the image
- Ensure that the security group you assign to the task permits traffic to/from it to access the web application
Spotted an error? Something functional to add value? Send me a pull request!
- Fork it (https://github.com/yourname/yourproject/fork)
- Create your feature branch (
git checkout -b feature/foo
) - Commit your changes (
git commit -am 'Add some foo'
) - Push to the branch (
git push origin feature/foo
) - Create a new Pull Request
MIT license. See LICENSE for full details.