Skip to content

Instantly share code, notes, and snippets.

@tanish-kr
Created November 14, 2024 06:55
Show Gist options
  • Save tanish-kr/ff9f9e63b9a693c8859486ba892a8e10 to your computer and use it in GitHub Desktop.
Save tanish-kr/ff9f9e63b9a693c8859486ba892a8e10 to your computer and use it in GitHub Desktop.
ECS execute script
#!/bin/bash -e
PROFILE=$1
CLUSTER_NAME=$2
# SERVICE_NAME=$3
CONTAINER_NAME=$3
if [ -z "$PROFILE" ]; then
echo "must profile name"
exit 1
fi
if [ -z "$CLUSTER_NAME" ]; then
echo "list clusters"
aws ecs list-clusters --profile ${PROFILE}
exit 1
fi
TASK_ARNS=$(aws ecs list-tasks --profile ${PROFILE} --cluster ${CLUSTER_NAME} --desired-status RUNNING --query 'taskArns[*]' --output text | awk '{print $1}')
if [ -n "$TASK_ARNS" ]; then
FIRST_TASK_ARN=$(echo "$TASK_ARNS" | head -n1)
else
echo "No tasks found"
exit 1
fi
TASK_ID=$(echo "${FIRST_TASK_ARN}" | cut -d / -f 3)
echo "Task ID: ${TASK_ID}"
if [ -z "$CONTAINER_NAME" ]; then
echo "container name lists"
aws ecs describe-tasks --profile ${PROFILE} --cluster ${CLUSTER_NAME} --task ${TASK_ID} --query 'tasks[].containers[].name'
else
echo "exec bash in ecs"
aws ecs execute-command --profile ${PROFILE} --cluster ${CLUSTER_NAME} --task ${TASK_ID} --container ${CONTAINER_NAME} --interactive --command "/bin/bash"
# aws ecs execute-command --profile ${PROFILE} --cluster ${CLUSTER_NAME} --task ${TASK_ID} --container ${CONTAINER_NAME} --interactive --command "bundle exec rails c"
fi
@tanish-kr
Copy link
Author

今だとパラメーターの有無でlist clusterとかで出力させて入力させるようにしてるけど、
ecsのexecute-commandをワンライナーで行えるようGoで書き換えたい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment