ecs-cliに慣れるため、とりあえずhello-worldしてみた。

チュートリアル

AWS公式チュートリアルサイト

手順

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が止まっている模様

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にログが流れているはず。

CloudWatch > ロググループ > tutorial > hello-world/hello_world/45409364-d632-4ffa-8b39-276ec24773d7

CloudWatchLogs

よしよし、想定通り!!