Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor application upgrade scripts #31

Open
briancaffey opened this issue Jan 15, 2023 · 0 comments
Open

Refactor application upgrade scripts #31

briancaffey opened this issue Jan 15, 2023 · 0 comments
Assignees

Comments

@briancaffey
Copy link
Owner

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.

@briancaffey briancaffey self-assigned this Jan 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant