Skip to content

Instantly share code, notes, and snippets.

@kokuyouwind
Last active May 7, 2021 04:09
Show Gist options
  • Save kokuyouwind/28cc4c1a93307f6c7a48388edd4ff420 to your computer and use it in GitHub Desktop.
Save kokuyouwind/28cc4c1a93307f6c7a48388edd4ff420 to your computer and use it in GitHub Desktop.
ECSで任意タスクを実行するスクリプト(クラスタとタスク定義名が揃っている前提、subnet, securityGroupは適切なものに書き換えてください)
#!/bin/bash -e
# ECS環境でコマンドを実行するためのタスク
#
# 以下の設定が事前に必要
# * aws-cliのインストール
# * ecs-cliのインストール
# * jqのインストール
#
# usage: run_task env command...
if [ $# -lt 2 ]; then
echo "usage: run_task env command..."
exit 1
fi
CLUSTER="$1"
TASK_DEF="$1"
CMD="bundle exec ${@:2}"
cat <<EOF
Task Settings:
Cluster: $CLUSTER
Task Definition: $TASK_DEF
Command: $CMD
EOF
echo -n "Continue? (yes to continue, other to exit): "
read IN
if [ "$IN" != "yes" ]; then
echo "Exit."
exit;
fi
echo "Task Creating..."
TASK_ARN=`echo $CMD | xargs -I {} aws ecs run-task --cluster $CLUSTER --task-definition $TASK_DEF --overrides '{"containerOverrides": [{"name":"app","command": ["bash","-c","'{}'"]}]}' --launch-type FARGATE --network-configuration 'awsvpcConfiguration={subnets=[subnet-xxx],securityGroups=[sg-xxx],assignPublicIp=DISABLED}' | jq -r '.tasks[0].taskArn'`
TASK_ID=`echo $TASK_ARN | cut -f 2 -d/`
echo "Task ${TASK_ID} Created."
echo "Wait for Task Starting..."
aws ecs wait tasks-running --cluster $CLUSTER --tasks $TASK_ARN
echo "Task Started."
echo "Following Logs...(Ctrl-C to Close)"
ecs-cli logs --task-id $TASK_ID --region ap-northeast-1 --cluster $CLUSTER --follow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment