Browse Source

Helper script for executing commands on ECS app containers (#2085)

* Add exec.sh script to execute command on app containers

* Update docs
pull/2101/head
Rachael Booth 1 year ago committed by GitHub
parent
commit
c1a4e6e7a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 18
      docs/monitoring.md
  2. 10
      exec.sh

18
docs/monitoring.md

@ -33,17 +33,23 @@ To do this, you will need to “exec” into the container.
- AWS access - AWS access
#### Accessing the rails console #### Accessing the rails console
1. Find the cluster name of the relevant cluster In a shell using suitable AWS credentials for the relevant account (e.g. the development, staging, or production account), run `./exec.sh env command`
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"` 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.
N.B. You can run other commands on the container similarly. 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-<PR-NUM>`
Alternatively, if you care about which container you're accessing, you can view a table of container details with e.g.
``` ```
env=staging env=staging
taskArns=$(aws ecs list-tasks --cluster "core-$env-app" --query "taskArns[*]") 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 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 <taskid> --interactive --command <command>` to run the relevant command on a specific task.
### Database ### 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. 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.

10
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"
Loading…
Cancel
Save