django-step-by-step
django-step-by-step copied to clipboard
Refactor application upgrade scripts
Both the frontend and backend applications are upgraded via AWS CLI calls.
The frontend application upgrade script looks like this:
Script
TASK_FAMILY=$WORKSPACE-$TASK
# save the task definition JSON to a variable
TASK_DESCRIPTION=$(aws ecs describe-task-definition \
--task-definition $TASK_FAMILY \
)
echo $TASK_DESCRIPTION | jq -r \
.taskDefinition.containerDefinitions \
> /tmp/$TASK_FAMILY.json
# write new container definition JSON with updated image
echo "Writing new $TASK_FAMILY container definitions JSON..."
# replace old image URI with new image URI in a new container definitions JSON
cat /tmp/$TASK_FAMILY.json \
| jq \
--arg IMAGE "$NEW_FRONTEND_IMAGE_URI" '.[0].image |= $IMAGE' \
> /tmp/$TASK_FAMILY-new.json
# Get the existing configuration for the task definition (memory, cpu, etc.)
# from the variable that we saved the task definition JSON to earlier
echo "Getting existing configuration for $TASK_FAMILY..."
MEMORY=$( echo $TASK_DESCRIPTION | jq -r \
.taskDefinition.memory \
)
CPU=$( echo $TASK_DESCRIPTION | jq -r \
.taskDefinition.cpu \
)
ECS_EXECUTION_ROLE_ARN=$( echo $TASK_DESCRIPTION | jq -r \
.taskDefinition.executionRoleArn \
)
ECS_TASK_ROLE_ARN=$( echo $TASK_DESCRIPTION | jq -r \
.taskDefinition.taskRoleArn \
)
# check the content of the new container definition JSON
cat /tmp/$TASK_FAMILY-new.json
# register new task definition using the new container definitions
# and the values that we read off of the existing task definitions
echo "Registering new $TASK_FAMILY task definition..."
aws ecs register-task-definition \
--family $TASK_FAMILY \
--container-definitions file:///tmp/$TASK_FAMILY-new.json \
--memory $MEMORY \
--cpu $CPU \
--network-mode awsvpc \
--execution-role-arn $ECS_EXECUTION_ROLE_ARN \
--task-role-arn $ECS_TASK_ROLE_ARN \
--requires-compatibilities "FARGATE"
While this works, there is an easier way to write this using the --cli-input-json
flag from the register-task-definition
AWS CLI command.
Both the frontend and backend application update scripts should be re-written to use this argument instead of reading out memory, CPU, execution-role-arn, task-role-arn individually.