Create new Elastic Container Service (ECS) task definition revision
A bash script example showing how to create a new revision of an ECS task definition, with variables for image URI, and other values.
About
This script demonstrates the use of a Bash here document to embed a task definition template in a deploy script. You can interpolate variable values from the bash script into the task definition template, and then pass the entire JSON structure to the
aws ecs create-task-definition CLI command using the --cli-input-json flag.Example deploy script
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
47
48
49
# File: script.sh
IMAGE_URL="httpd:2.4"
CPU=256
MEMORY=512
export TASK_DEFINITION=$(cat << EOF
{
"containerDefinitions": [
{
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
],
"entryPoint": [
"sh",
"-c"
],
"essential": true,
"image": "$IMAGE_URL",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "/ecs/fargate-task-definition",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"name": "sample-fargate-app",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
],
"cpu": "$CPU",
"executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole",
"family": "fargate-task-definition",
"memory": "$MEMORY",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
]
}
EOF
)
aws ecs register-task-definition --cli-input-json "$TASK_DEFINITION"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
47
48
49
# File: script.sh
IMAGE_URL="httpd:2.4"
CPU=256
MEMORY=512
export TASK_DEFINITION=$(cat << EOF
{
"containerDefinitions": [
{
"command": [
"/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\""
],
"entryPoint": [
"sh",
"-c"
],
"essential": true,
"image": "$IMAGE_URL",
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group" : "/ecs/fargate-task-definition",
"awslogs-region": "us-east-1",
"awslogs-stream-prefix": "ecs"
}
},
"name": "sample-fargate-app",
"portMappings": [
{
"containerPort": 80,
"hostPort": 80,
"protocol": "tcp"
}
]
}
],
"cpu": "$CPU",
"executionRoleArn": "arn:aws:iam::012345678910:role/ecsTaskExecutionRole",
"family": "fargate-task-definition",
"memory": "$MEMORY",
"networkMode": "awsvpc",
"requiresCompatibilities": [
"FARGATE"
]
}
EOF
)
aws ecs register-task-definition --cli-input-json "$TASK_DEFINITION"Installation
Download the script and use
chmod on the command line to make it executable:1
chmod +x script.shYou can then run the script on the command line with
1
./script.shNext steps
- Customize the embedded task definition with your own application's details
- Add some
dockercommands to the top of the script to build and push a new version of your container image - Add the script to your Git repository and use a CI/CD solution to automatically run the deploy script whenever you merge code into your repository.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article