From c1a4e6e7a93d8221fe21aecd86333baa1ec79123 Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Tue, 19 Dec 2023 09:41:30 +0000 Subject: [PATCH] Helper script for executing commands on ECS app containers (#2085) * Add exec.sh script to execute command on app containers * Update docs --- docs/monitoring.md | 18 ++++++++++++------ exec.sh | 10 ++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) create mode 100755 exec.sh diff --git a/docs/monitoring.md b/docs/monitoring.md index dd2234743..abd1a3dca 100644 --- a/docs/monitoring.md +++ b/docs/monitoring.md @@ -33,17 +33,23 @@ To do this, you will need to “exec” into the container. - AWS access #### Accessing the rails console -1. Find the cluster name of the relevant cluster -2. Find the task arn of a relevant task -3. In a shell using suitable AWS credentials for the relevant account (e.g. the development, staging, or production account), run `aws ecs execute-command --cluster cluster-name --task task-arn --interactive --command "rails c"` - -N.B. You can run other commands on the container similarly. +In a shell using suitable AWS credentials for the relevant account (e.g. the development, staging, or production account), run `./exec.sh env command` + +E.g. `./exec.sh staging "rails c"` - this will open the rails console on an app container in the staging environment, when authenticated for the staging aws account. + +You can use this for other commands, e.g. to get a bash shell. + +For production, use `prod` as the environment. For a review app, use `review-` + +Alternatively, if you care about which container you're accessing, you can view a table of container details with e.g. ``` env=staging taskArns=$(aws ecs list-tasks --cluster "core-$env-app" --query "taskArns[*]") aws ecs describe-tasks --cluster "core-$env-app" --tasks "${taskArns[@]}" --query "tasks[*].{arn:taskArn, status:lastStatus, startedAt:startedAt, group:group, image:containers[0].image}" --output text ``` +You can then use `aws ecs execute-command --cluster "core-$env-app" --task --interactive --command ` to run the relevant command on a specific task. + ### Database -In order to investigate or look more closely at the database, you can exec into a container as above, and use the rails console to query the database. \ No newline at end of file +In order to investigate or look more closely at the database, you can exec into a container as above, and use the rails console to query the database. diff --git a/exec.sh b/exec.sh new file mode 100755 index 000000000..9d8dd6697 --- /dev/null +++ b/exec.sh @@ -0,0 +1,10 @@ +if [ $# -ne 2 ]; + then echo "Expected 2 arguments: exec.sh env command" +fi + +env=$1 +command=$2 +cluster="core-$env-app" + +taskId=$(aws ecs list-tasks --cluster $cluster --service-name "core-$env-app" --query "taskArns[0]" | grep -o "/$cluster/\w*" | sed "s@/$cluster/@@g") +aws ecs execute-command --cluster "core-$env-app" --task $taskId --interactive --command "$command"