Amazon ECS デプロイサーキットブレーカーを設定して監視する方法を教えてください。
Amazon Elastic Container Service (Amazon ECS) のデプロイが失敗したときに、ロールバックを自動で行い、通知を受信したいです。
簡単な説明
Amazon ECS デプロイサーキットブレーカーを使用してロールバックを自動化し、デプロイを監視するには、次の手順を実行します。
- デプロイサーキットブレーカーを設定します。
- Amazon EventBridge を設定し、デプロイサーキットブレーカーを監視します。
- デプロイが失敗するシナリオをテストします。
解決策
注: AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、「AWS CLI で発生したエラーのトラブルシューティング」を参照してください。また、AWS CLI の最新バージョンを使用していることを確認してください。
デプロイサーキットブレーカーを設定する
次の手順を実行します。
- 次の例のようなタスク定義を含む JSON ファイルを作成します。
注: 123456789876 を AWS アカウント ID に置き換えてください。ecsTaskExecutionRole がない場合は、タスク実行ロールを作成します。{ "family": "my-task", "containerDefinitions": [ { "name": "sample-container", "image": "nginx:alpine", "essential": true } ], "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" } - タスク定義を登録するには、次の register-task-definition コマンドを実行します。
注: taskdef-success.json は、実際のタスク定義 JSON ファイルに置き換えます。aws ecs register-task-definition \ --cli-input-json file://taskdef-success.json - デプロイサーキットブレーカーとロールバックが有効化された Amazon ECS サービスを作成するには、次の create-service コマンドを実行します。
注: subnet-12345 をサブネットに、sg-12345 をセキュリティグループに置き換えてください。デプロイサーキットブレーカーはローリングアップデートのデプロイでのみ使用できるため、deployment-controller を type=ECS に設定する必要があります。aws ecs create-service \ --cluster default \ --service-name my-sample-service \ --deployment-controller type=ECS \ --desired-count 1 \ --deployment-configuration "deploymentCircuitBreaker={enable=true,rollback=true}" \ --task-definition my-task:1 \ --launch-type FARGATE \ --network-configuration "awsvpcConfiguration={subnets=[subnet-12345],securityGroups=[sg-12345],assignPublicIp=ENABLED}"
デフォルトのクラスターがない場合は、次の create-cluster コマンドを実行してクラスターを作成します。
注: example-cluster は、実際のクラスター名に置き換えます。aws ecs create-cluster \ --cluster-name example-cluster - 次の describe-services コマンドを実行し、Amazon ECS サービスの状態が Steady であることを確認します。
次の例のような出力が表示されます。aws ecs describe-services \ --cluster default \ --services my-sample-service | jq '.services[0].events[] | {message}'{ "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)." }
EventBridge を設定し、デプロイサーキットブレーカーを監視する
次の手順を実行します。
-
次の create-topic コマンドを実行し、EventBridge ルールのターゲットとして使用する Amazon Simple Notification Service (Amazon SNS) トピックを作成します。
aws sns create-topic \ --name my-topic注: my-topic は、実際の SNS トピック名に置き換えます。
-
トピックの属性を更新し、必要な API を呼び出せるようにするには、次の set-topic-attributes コマンドを実行します。
aws sns set-topic-attributes \ --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \ --attribute-name Policy \ --attribute-value '{ "Version": "2008-10-17", "Id": "my_topic_policy", "Statement": [ { "Sid": "my_topic_default", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": [ "SNS:GetTopicAttributes", "SNS:SetTopicAttributes", "SNS:AddPermission", "SNS:RemovePermission", "SNS:DeleteTopic", "SNS:Subscribe", "SNS:ListSubscriptionsByTopic", "SNS:Publish" ], "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic", "Condition": { "StringEquals": { "AWS:SourceOwner": "123456789876" } } }, { "Sid": "my_topic_for_sns_Publish", "Effect": "Allow", "Principal": { "Service": "events.amazonaws.com" }, "Action": "sns:Publish", "Resource": "arn:aws:sns:eu-west-1:123456789876:my-topic" } ] }'注: お使いのものでそれぞれ、eu-west-1を AWS リージョンに、123456789876 をアカウント ID に、my-topic をトピック名に置き換えます。
-
メールアドレスを使用して SNS トピックにサブスクライブするには、次の subscribe コマンドを実行します。
aws sns subscribe \ --topic-arn arn:aws:sns:eu-west-1:123456789876:my-topic \ --protocol email \ --notification-endpoint example@example.com注: お使いのものでそれぞれ、eu-west-1 をリージョンに、123456789876 をアカウント ID に、my-topic をトピック名に、example@example.com をメールアドレスをに置き換えます。
-
受信したサブスクリプション確認メールに記載されている [サブスクリプションを確認] を選択します。
-
サービスのデプロイに失敗したイベントに対する EventBridge ルールを作成するには、次の put-rule コマンドを実行します。
aws events put-rule \ --name "EcsServiceDeploymentFailed" \ --event-pattern "{\"source\":[\"aws.ecs\"],\"detail-type\":[\"ECS Deployment State Change\"],\"detail\":{\"eventName\":[\"SERVICE_DEPLOYMENT_FAILED\"]}}" -
SNS トピックを EventBridge ルールのターゲットとして追加するには、次の put-targets コマンドを実行します。
aws events put-targets \ --rule EcsServiceDeploymentFailed --targets "Id"="1","Arn"="arn:aws:sns:eu-west-1:123456789876:my-topic"注: お使いのものでそれぞれ、eu-west-1をリージョンに、123456789876 をアカウント ID に、my-topic をトピック名に置き換えます。
デプロイ失敗シナリオをテストする
次の手順を実行します。
-
次に示すように、タスク定義に誤ったイメージタグを含む JSON ファイルを作成します。
{ "family": "my-task", "containerDefinitions": [ { "name": "sample-container", "image": "nginx:wrong-image-tag", "essential": true } ], "executionRoleArn": "arn:aws:iam::123456789876:role/ecsTaskExecutionRole", "networkMode": "awsvpc", "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" }注: お使いのものでそれぞれ、sample-container をコンテナインスタンスに、nginx:wrong-image-tag を誤ったイメージタグに、123456789876 をアカウント ID に置き換えます。イメージタグが誤っているため、デプロイに失敗します。
-
タスク定義を登録するには、次の register-task-definition コマンドを実行します。
aws ecs register-task-definition --cli-input-json file://taskdef-failure.json注: taskdef-failure.json は、お使いのタスク定義 JSON ファイルのタイトルに置き換えます。
-
新しいタスク定義でサービスを更新し、新しいデプロイを開始するには、次の update-service コマンドを実行します。
aws ecs update-service --service my-sample-service --task-definition my-task:2注: お使いのものでそれぞれ、my-sample-service をサービスに、my-task:2 をタスクに置き換えます。タスクがイメージをプルできないため、新しいデプロイは失敗します。次の例のような出力が表示されます。
{ "version": "0", "id": "12345abc-2f7c-f86a-e544-a69218eb1446", "detail-type": "ECS Deployment State Change", "source": "aws.ecs", "account": "123456789876", "time": "2024-11-19T17:42:41Z", "region": "eu-west-1", "resources": [ "arn:aws:ecs:eu-west-1:123456789876:service/default/my-sample-service" ], "detail": { "eventType": "ERROR", "eventName": "SERVICE_DEPLOYMENT_FAILED", "clusterArn": "arn:aws:ecs:eu-west-1:123456789876:cluster/default", "deploymentId": "ecs-svc/9876543210987654321", "updatedAt": "2024-11-19T17:42:40.73Z", "reason": "ECS deployment circuit breaker: tasks failed to start." } } -
Amazon ECS サービスがロールバックされたことを確認するには、次の describe-services コマンドを実行します。
aws ecs describe-services \ --cluster default \ --services my-sample-service | jq '.services[0].events[] | {message}'次の例のような出力が表示されます。
{ "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) rolling back to deployment ecs-svc/1234567890123456789." } { "message": "(service my-sample-service) (deployment ecs-svc/9876543210987654321) deployment failed: tasks failed to start." } { "message": "(service my-sample-service) has started 1 tasks: (task b808c60616134ec1ac0c656a2bff1ef2)." } { "message": "(service my-sample-service) has started 1 tasks: (task 846c9aebd9224c2b832a38942cae5ea6)." } { "message": "(service my-sample-service) has started 1 tasks: (task 7143d03444574f2db2b567d75df3fe72)." } { "message": "(service my-sample-service) has started 1 tasks: (task 9a6a399770d940a2b442560c02a6a4c0)." } { "message": "(service my-sample-service) has reached a steady state." } { "message": "(service my-sample-service) (deployment ecs-svc/1234567890123456789) deployment completed." } { "message": "(service my-sample-service) has started 1 tasks: (task 2918eb15dd0f4d42affc2a3a07818abf)." }
関連情報
- トピック
- Containers
- 言語
- 日本語
