AWS Builder Center

Inject configuration files in an Elastic Container Service (ECS) task definition

How to add custom config files to your container at runtime, by using a command override in the ECS task definition

The following snippet shows how to do runtime file generation and injection in an ECS task definition.
It launches an NGINX reverse proxy server, directly from Amazon Elastic Container Registry Public. This default container does not do anything except show a simple
"welcome to NGINX" message. However, we can use a command to generate the custom NGINX configuration at runtime, prior to launching the container. Because this command runs inside the container as it launches we can even use custom environment variables
from the task definition.
💡 Tip: You will find that the richer feature set of YAML makes the embedded config file in the CloudFormation YAML much easier to read than the single line string inside of the raw JSON task definition.
If you intend to create more advanced task definitions it is
highly recommended to use AWS CloudFormation. If you do not wish
to use CloudFormation
then consider still writing your task definitions in YAML, and use yq to convert the YAML
to JSON
 
for the ECS API.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File: task-definition.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: An example task definition which self generates and injects
a config file for the running application.

Resources:

InjectorTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: nginx-ingress
Cpu: 256
Memory: 128
ContainerDefinitions:
- Name: nginx
Image: public.ecr.aws/nginx/nginx:mainline
EntryPoint:
- '/bin/sh'
- '-c'
Environment:
- Name: MY_URL_ENV_VARIABLE
Value: http://myupstreamhost.com
Command:
- >
echo "
events {
worker_connections 1024;
}
http
{
upstream ingress
{
server $MY_URL_ENV_VARIABLE;
}
server {
listen 8080;
location /
{
proxy_pass http://ingress;
proxy_set_header Host \$host;
proxy_pass_request_headers on;
}
}
}
" > /etc/nginx/nginx.conf &&
exec nginx -g 'daemon off;'
Deploy the CloudFormation template above by using the AWS CloudFormation web console.
Alternatively you can deploy from the command line with:
1
2
3
aws cloudformation deploy \
--stack-name sample-task-definition \
--template-file task-definition.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File: task-definition.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: An example task definition which self generates and injects
a config file for the running application.

Resources:

InjectorTaskDefinition:
Type: AWS::ECS::TaskDefinition
Properties:
Family: nginx-ingress
Cpu: 256
Memory: 128
ContainerDefinitions:
- Name: nginx
Image: public.ecr.aws/nginx/nginx:mainline
EntryPoint:
- '/bin/sh'
- '-c'
Environment:
- Name: MY_URL_ENV_VARIABLE
Value: http://myupstreamhost.com
Command:
- >
echo "
events {
worker_connections 1024;
}
http
{
upstream ingress
{
server $MY_URL_ENV_VARIABLE;
}
server {
listen 8080;
location /
{
proxy_pass http://ingress;
proxy_set_header Host \$host;
proxy_pass_request_headers on;
}
}
}
" > /etc/nginx/nginx.conf &&
exec nginx -g 'daemon off;'
Deploy the CloudFormation template above by using the AWS CloudFormation web console.
Alternatively you can deploy from the command line with:
1
2
3
aws cloudformation deploy \
--stack-name sample-task-definition \
--template-file task-definition.yml

See Also

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article