LoginSignup
4
6

More than 3 years have passed since last update.

ecs-cliでHelloWorldしてみた

Posted at

背景

あるバッチ処理をクラウド上で定期実行させたかった。
色々調べてた所、ECS-Fargateで定期実行が出来るみたい。
とりあえず雰囲気掴むためにHelloWorldしてみた。

チュートリアル

公式チュートリアル: Amazon ECS CLI を使用して Fargate タスクのクラスターを作成する

手順

1. config編集

$ ecs-cli configure --cluster hello-world --default-launch-type FARGATE --region ap-northeast-1 --config-name hello-world

$ cat ~/.ecs/config
version: v1
default: ecs-cli-test-config
clusters:
 hello-world:
   cluster: hello-world
   region: ap-northeast-1
   default_launch_type: FARGATE

2. credential編集

$ export AWS_ACCESS_KEY_ID=xxxxxxxxx
$ export AWS_SECRET_ACCESS_KEY=xxxxxx

上記を実行した状態で、

$ ecs-cli configure profile --profile-name ecs-profile --access-key $AWS_ACCESS_KEY_ID --secret-key $AWS_SECRET_ACCESS_KEY

$ cat ~/.ecs/credentials
version: v1
default: ecs-profile
ecs_profiles:
 ecs-profile:
   aws_access_key_id: 【AWS_ACCESS_KEY】
   aws_secret_access_key: 【AWS_SECRET_KEY】

3. クラスター作成

$ ecs-cli up --cluster-config hello-world --ecs-profile ecs-profile --force

INFO[0001] Created cluster                               cluster=hello-world region=ap-northeast-1
INFO[0002] Waiting for your cluster resources to be created...
INFO[0003] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0064] Cloudformation stack status                   stackStatus=ROLLBACK_FAILED
INFO[0125] Cloudformation stack status                   stackStatus=ROLLBACK_FAILED
INFO[0186] Cloudformation stack status                   stackStatus=ROLLBACK_FAILED

→ CloudFormationの作成途中で止まっている模様

AWSコンソール上、
CloudFormation > スタック > amazon-ecs-cli-setup-hello-world
にてエラー内容を確認

イベントで各種権限が足りないよとのエラーが表示
ex. CloudFormationでのエラーの一部
Image from Gyazo


IAM編集(権限追加)

ec2:DeleteVpc
ec2:DescribeVpcs
ec2:CreateInternetGateway
ec2:DescribeInternetGateways
ec2:ModifyVpcAttribute
ec2:DeleteInternetGateway
ec2:DescribeAvailabilityZones
ec2:DescribeAccountAttributes
ec2:DescribeSubnets
ec2:CreateRouteTable
ec2:CreateSubnet
ec2:AttachInternetGateway
ec2:DeleteSubnet
ec2:DeleteRouteTable
ec2:DetachInternetGateway
ec2:CreateRoute
ec2:AssociateRouteTable
ec2:DeleteRoute
cloudformation:DescribeStackResources

$ ecs-cli up --cluster-config hello-world --ecs-profile ecs-profile --force

INFO[0001] Created cluster                               cluster=hello-world region=ap-northeast-1
INFO[0002] Waiting for your CloudFormation stack resources to be deleted...
INFO[0002] Cloudformation stack status                   stackStatus=DELETE_IN_PROGRESS
INFO[0034] Waiting for your cluster resources to be created...
INFO[0035] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
INFO[0096] Cloudformation stack status                   stackStatus=CREATE_IN_PROGRESS
VPC created: 【VPC_ID】
Subnet created: 【SUBNET_ID_A】
Subnet created: 【SUBNET_ID_B】
Cluster creation succeeded.

4. 作成したVPCのセキュリティグループIDを取得

$ aws2 ec2 describe-security-groups --filters Name=vpc-id,Values=【VPC_ID】 --region ap-northeast-1

An error occurred (UnauthorizedOperation) when calling the DescribeSecurityGroups operation: You are not authorized to perform this operation.

権限(ec2:DescribeSecurityGroups)が必要なので注意

5. docker-compose.yml/ecs-params.yml を用意する

ecs-params.yml
version: 1
task_definition:
 task_execution_role: ecsTaskExecutionRole
 ecs_network_mode: awsvpc
 task_size:
   mem_limit: 0.5GB
   cpu_limit: 256
run_params:
 network_configuration:
   awsvpc_configuration:
     subnets:
       "【SUBNET_ID_A】"
       "【SUBNET_ID_B】"
     security_groups:
       "【SECURITY_GROUP_ID】"
     assign_public_ip: ENABLED
docker-compose.yml
hello_world:
 cpu_shares: 135
 mem_limit: 131072000
 image: hello-world
 log_driver: awslogs
 log_opt:
   awslogs-group: "tutorial"
   awslogs-region: "ap-northeast-1"
   awslogs-stream-prefix: "hello-world"

6. 5で作成したファイルを元にデプロイ

# カレントディレクトリにあるファイルを自動で参照
$ ecs-cli compose up

# ファイルを指定する場合
$ ecs-cli compose -f docker-compose.yml --ecs-params ecs-params.yml up

7. CloudWatchLogsを確認

docker-compose.ymlにlogの設定をawslogsに設定しているので、CloudWatchLogsにログが流れているはず。
AWSコンソール上で、
CloudWatch > ロググループ > tutorial > hello-world/hello_world/45409364-d632-4ffa-8b39-276ec24773d7

CloudWatchLogs

よしよし、想定通り!!

まとめ

あとは、ECRに実行したいバッチ処理のイメージ載せて、タスク定義とタスク作って、スケジュール組むだけ。
割とすんなりいく印象。もう少しいじってみたいと思います。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6