ブルー/グリーンデプロイを使用した Amazon ECS サービスのデプロイ
AWS CLI でブルー/グリーンデプロイタイプを使用する Fargate タスクを含む、Amazon ECS サービスを作成する方法について説明します。
注記
ブルー/グリーンデプロイを実行するためのサポートが AWS CloudFormation に追加されています。詳細については、[AWS CloudFormationユーザーガイド]の[AWS CloudFormationを使用した CodeDeploy による Perform Amazon ECS ブルー/グリーンデプロイの実行]を参照してください。
前提条件
このチュートリアルでは、以下の前提条件をすでに満たしているものとします。
-
AWS CLI の最新バージョンがインストールされ、設定されていること。AWS CLI のインストールまたはアップグレードについては、「AWS CLI の最新バージョンをインストールまたは更新」を参照してください。
-
「Amazon ECS を使用するようにセットアップする」のステップを完了していること。
-
AWS ユーザーに AmazonECS_FullAccess IAMポリシー例で指定されている必要なアクセス権限があること。
-
VPC およびセキュリティグループが使用できるように作成されていること。詳細については、「仮想プライベートクラウドを作成する」を参照してください。
-
Amazon ECS CodeDeploy IAM ロールが作成されます。詳細については、「Amazon ECS CodeDeploy IAM ロール」を参照してください。
ステップ 1: Application Load Balancer の作成
ブルー/グリーンデプロイタイプを使用する Amazon ECS サービスは、Application Load Balancer または Network Load Balancer のいずれかを使用する必要があります。このチュートリアルでは、Application Load Balancer を使用します。
Application Load Balancer を作成するには
-
[ロードバランサーの作成]コマンドを使用して、Application Load Balancer を作成します。異なるアベイラビリティーゾーンにある 2 つのサブネット、およびセキュリティグループを指定します。
aws elbv2 create-load-balancer \ --name
bluegreen-alb
\ --subnetssubnet-abcd1234
subnet-abcd5678
\ --security-groupssg-abcd1234
\ --regionus-east-1
出力には、次の形式でロードバランサーの Amazon リソースネーム (ARN) が含まれます。
arn:aws:elasticloadbalancing:
region
:aws_account_id
:loadbalancer/app/bluegreen-alb/e5ba62739c16e642
-
ターゲットグループを作成するには、create-target-group コマンドを使用します。このターゲットグループは、サービスで設定されている元のタスクにトラフィックをルーティングします。
aws elbv2 create-target-group \ --name
bluegreentarget1
\ --protocolHTTP
\ --port80
\ --target-type ip \ --vpc-idvpc-abcd1234
\ --regionus-east-1
出力には、以下の形式でターゲットグループの ARN が含まれます。
arn:aws:elasticloadbalancing:
region
:aws_account_id
:targetgroup/bluegreentarget1/209a844cd01825a4 -
ターゲットグループにリクエストを転送するデフォルトルールが関連付けられた、ロードバランサーのリスナーを作成するには、create-listener コマンドを使用します。
aws elbv2 create-listener \ --load-balancer-arn arn:aws:elasticloadbalancing:
region
:aws_account_id
:loadbalancer/app/bluegreen-alb/e5ba62739c16e642
\ --protocol HTTP \ --port 80 \ --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region
:aws_account_id
:targetgroup/bluegreentarget1/209a844cd01825a4
\ --regionus-east-1
出力には、以下の形式でリスナーの ARN が含まれます。
arn:aws:elasticloadbalancing:
region
:aws_account_id
:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4
ステップ 2: Amazon ECS クラスターを作成する
[create-cluster]コマンドを使用して、使用するtutorial-bluegreen-cluster
という名前のクラスターを作成します。
aws ecs create-cluster \ --cluster-name
tutorial-bluegreen-cluster
\ --regionus-east-1
出力には、以下の形式でクラスターの ARN が含まれます。
arn:aws:ecs:region
:aws_account_id
:cluster/tutorial-bluegreen-cluster
ステップ 3: タスク定義を登録する
登録-タスク-定義コマンドを使用して、Fargate と互換性のあるタスク定義を登録します。これには、awsvpc
ネットワークモードを使用する必要があります。このチュートリアルで使用するタスク定義の例を以下に示します。
まず、以下の内容で fargate-task.json
というファイルを作成します。タスク実行ロールの ARN を使用していることを確認します。詳細については、「Amazon ECS タスク実行IAM ロール」を参照してください。
{ "family": "sample-fargate", "networkMode": "awsvpc", "containerDefinitions": [ { "name": "sample-app", "image": "public.ecr.aws/docker/library/httpd:latest", "portMappings": [ { "containerPort": 80, "hostPort": 80, "protocol": "tcp" } ], "essential": true, "entryPoint": [ "sh", "-c" ], "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #333;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ] } ], "requiresCompatibilities": [ "FARGATE" ], "cpu": "256", "memory": "512" }
次に、作成した fargate-task.json
ファイルを使用して、タスク定義を登録します。
aws ecs register-task-definition \ --cli-input-json file://
fargate-task.json
\ --regionus-east-1
ステップ 4: Amazon ECS サービスを作成する
サービスを作成するには、create-service コマンドを使用します。
まず、以下の内容で service-bluegreen.json
というファイルを作成します。
{ "cluster": "
tutorial-bluegreen-cluster
", "serviceName": "service-bluegreen
", "taskDefinition": "tutorial-task-def
", "loadBalancers": [ { "targetGroupArn": "arn:aws:elasticloadbalancing:region
:aws_account_id
:targetgroup/bluegreentarget1/209a844cd01825a4
", "containerName": "sample-app", "containerPort": 80 } ], "launchType": "FARGATE", "schedulingStrategy": "REPLICA", "deploymentController": { "type": "CODE_DEPLOY" }, "platformVersion": "LATEST
", "networkConfiguration": { "awsvpcConfiguration": { "assignPublicIp": "ENABLED", "securityGroups": [ "sg-abcd1234
" ], "subnets": [ "subnet-abcd1234
", "subnet-abcd5678
" ] } }, "desiredCount": 1 }
次に、作成した service-bluegreen.json
ファイルを使用してサービスを作成します。
aws ecs create-service \ --cli-input-json file://
service-bluegreen.json
\ --regionus-east-1
出力には、以下の形式でサービスの ARN が含まれます。
arn:aws:ecs:region
:aws_account_id
:service/service-bluegreen
次のコマンドを使用し、ロードバランサーの DNS 名を取得します。
aws elbv2 describe-load-balancers --name bluegreen-alb --query 'LoadBalancers[*].DNSName'
Web ブラウザに DNS 名を入力すると、サンプルアプリが青色の背景で表示された Web ページが表示されます。
ステップ 5: AWS CodeDeploy リソースを作成する
次のステップに従って、CodeDeploy アプリケーション、CodeDeploy デプロイグループのApplication Load Balancer ターゲットグループ、および CodeDeploy デプロイグループを作成します。
CodeDeploy リソースを作成するには
-
[アプリケーションの作成]コマンドを使用して、CodeDeploy アプリケーションを作成します。
ECS
コンピューティングプラットフォームを指定します。aws deploy create-application \ --application-name
tutorial-bluegreen-app
\ --compute-platformECS
\ --regionus-east-1
出力には、以下の形式でアプリケーションの ID が含まれます。
{ "applicationId": "b8e9c1ef-3048-424e-9174-885d7dc9dc11" }
-
[ターゲットグループの作成]コマンドを使用して、CodeDeploy デプロイ グループの作成時に使用される 2 つ目のApplication Load Balancer ターゲットグループを作成します。
aws elbv2 create-target-group \ --name
bluegreentarget2
\ --protocolHTTP
\ --port80
\ --target-type ip \ --vpc-id "vpc-0b6dd82c67d8012a1
" \ --regionus-east-1
出力には、以下の形式でターゲットグループの ARN が含まれます。
arn:aws:elasticloadbalancing:
region
:aws_account_id
:targetgroup/bluegreentarget2/708d384187a3cfdc -
[デプロイ-グループの作成]コマンドを使用して CodeDeploy デプロイ グループを作成します。
まず、以下の内容で
tutorial-deployment-group.json
というファイルを作成します。この例では、作成したリソースを使用します。serviceRoleArn
には、Amazon ECS CodeDeploy IAM ロールの ARN を指定します。詳細については、「Amazon ECS CodeDeploy IAM ロール」を参照してください。{ "applicationName": "
tutorial-bluegreen-app
", "autoRollbackConfiguration": { "enabled": true, "events": [ "DEPLOYMENT_FAILURE" ] }, "blueGreenDeploymentConfiguration": { "deploymentReadyOption": { "actionOnTimeout": "CONTINUE_DEPLOYMENT", "waitTimeInMinutes": 0 }, "terminateBlueInstancesOnDeploymentSuccess": { "action": "TERMINATE", "terminationWaitTimeInMinutes": 5 } }, "deploymentGroupName": "tutorial-bluegreen-dg
", "deploymentStyle": { "deploymentOption": "WITH_TRAFFIC_CONTROL", "deploymentType": "BLUE_GREEN" }, "loadBalancerInfo": { "targetGroupPairInfoList": [ { "targetGroups": [ { "name": "bluegreentarget1
" }, { "name": "bluegreentarget2
" } ], "prodTrafficRoute": { "listenerArns": [ "arn:aws:elasticloadbalancing:region
:aws_account_id
:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4
" ] } } ] }, "serviceRoleArn": "arn:aws:iam::aws_account_id
:role/ecsCodeDeployRole
", "ecsServices": [ { "serviceName": "service-bluegreen
", "clusterName": "tutorial-bluegreen-cluster
" } ] }次に、CodeDeploy デプロイ グループを作成します。
aws deploy create-deployment-group \ --cli-input-json file://
tutorial-deployment-group.json
\ --regionus-east-1
出力には、以下の形式でデプロイグループの ID が含まれます。
{ "deploymentGroupId": "6fd9bdc6-dc51-4af5-ba5a-0a4a72431c88" }
ステップ 6: CodeDeploy デプロイを作成およびモニタリングする
CodeDeploy デプロイを作成する前に、次のように fargate-task.json
のタスク定義 command
を更新し、サンプルアプリの背景色を緑色に変更します。
{ ... "containerDefinitions": [ { ... "command": [ "/bin/sh -c \"echo '<html> <head> <title>Amazon ECS Sample App</title> <style>body {margin-top: 40px; background-color: #097969;} </style> </head><body> <div style=color:white;text-align:center> <h1>Amazon ECS Sample App</h1> <h2>Congratulations!</h2> <p>Your application is now running on a container in Amazon ECS.</p> </div></body></html>' > /usr/local/apache2/htdocs/index.html && httpd-foreground\"" ] } ], ... }
次のコマンドを使用し、更新されたタスク定義を登録します。
aws ecs register-task-definition \ --cli-input-json file://
fargate-task.json
\ --regionus-east-1
以下のステップを従い、アプリケーション仕様ファイル (AppSpec ファイル) と CodeDeploy デプロイを作成してアップロードします。
CodeDeploy デプロイを作成してモニタリングするには
-
以下の手順を使用して、AppSpec ファイルを作成してアップロードします。
-
CodeDeploy デプロイグループの内容で
appspec.yaml
というファイルを作成します。この例では、更新されたタスク定義を使用しています。version: 0.0 Resources: - TargetService: Type: AWS::ECS::Service Properties: TaskDefinition: "arn:aws:ecs:
region
:aws_account_id
:task-definition/tutorial-task-def:2
" LoadBalancerInfo: ContainerName: "sample-app
" ContainerPort: 80 PlatformVersion: "LATEST" -
「s3 mb」コマンドを使用して、AppSpec ファイルの Amazon S3 バケットを作成します。
aws s3 mb s3://
tutorial-bluegreen-bucket
-
「s3 cp」コマンドを使用して、AppSpec ファイルを Amazon S3 バケットにアップロードします。
aws s3 cp ./appspec.yaml s3://
tutorial-bluegreen-bucket
/appspec.yaml
-
-
以下のステップを使用して、CodeDeploy デプロイを作成します。
-
CodeDeploy デプロイの内容で
create-deployment.json
というファイルを作成します。この例では、チュートリアルの前半で作成したリソースを使用します。{ "applicationName": "
tutorial-bluegreen-app
", "deploymentGroupName": "tutorial-bluegreen-dg
", "revision": { "revisionType": "S3", "s3Location": { "bucket": "tutorial-bluegreen-bucket
", "key": "appspec.yaml
", "bundleType": "YAML" } } } -
デプロイを作成するには、create-deployment コマンドを使用します。
aws deploy create-deployment \ --cli-input-json file://
create-deployment.json
\ --regionus-east-1
出力には、以下の形式でデプロイの ID が含まれます。
{ "deploymentId": "d-RPCR1U3TW" }
-
-
デプロイの詳細を取得するには、get-deployment-target コマンドで、前の出力からの
deploymentId
を指定します。aws deploy get-deployment-target \ --deployment-id "
d-IMJU3A8TW
" \ --target-idtutorial-bluegreen-cluster:service-bluegreen
\ --regionus-east-1
初期では、デプロイの状態は
InProgress
です。トラフィックは、BLUE
のtaskSetLabel
、PRIMARY
の状態、100.0
のtrafficWeight
である元のタスクセットに送信されます。置換タスクセットには、GREEN
のtaskSetLabel
、ACTIVE
の状態、0.0
のtrafficWeight
があります。DNS 名を入力した Web ブラウザには、引き続き青い背景のサンプルアプリが表示されます。{ "deploymentTarget": { "deploymentTargetType": "ECSTarget", "ecsTarget": { "deploymentId": "d-RPCR1U3TW", "targetId": "tutorial-bluegreen-cluster:service-bluegreen", "targetArn": "arn:aws:ecs:
region
:aws_account_id
:service/service-bluegreen", "lastUpdatedAt": "2023-08-10T12:07:24.797000-05:00", "lifecycleEvents": [ { "lifecycleEventName": "BeforeInstall", "startTime": "2023-08-10T12:06:22.493000-05:00", "endTime": "2023-08-10T12:06:22.790000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "Install", "startTime": "2023-08-10T12:06:22.936000-05:00", "status": "InProgress" }, { "lifecycleEventName": "AfterInstall", "status": "Pending" }, { "lifecycleEventName": "BeforeAllowTraffic", "status": "Pending" }, { "lifecycleEventName": "AllowTraffic", "status": "Pending" }, { "lifecycleEventName": "AfterAllowTraffic", "status": "Pending" } ], "status": "InProgress", "taskSetsInfo": [ { "identifer": "ecs-svc/9223370493423413672", "desiredCount": 1, "pendingCount": 0, "runningCount": 1, "status": "ACTIVE", "trafficWeight": 0.0, "targetGroup": { "name": "bluegreentarget2" }, "taskSetLabel": "Green" }, { "identifer": "ecs-svc/9223370493425779968", "desiredCount": 1, "pendingCount": 0, "runningCount": 1, "status": "PRIMARY", "trafficWeight": 100.0, "targetGroup": { "name": "bluegreentarget1" }, "taskSetLabel": "Blue" } ] } } }以下の出力に示すように、デプロイメントのステータスが
Succeeded
になるまで、コマンドを使用してデプロイメントの詳細の取得を続けます。これで、トラフィックは置換タスクセットにリダイレクトされ、PRIMARY
の状態および100.0
のtrafficWeight
になりました。ロードバランサーの DNS 名を入力したウェブブラウザを更新すると、背景が緑色のサンプルアプリが表示されます。{ "deploymentTarget": { "deploymentTargetType": "ECSTarget", "ecsTarget": { "deploymentId": "d-RPCR1U3TW", "targetId": "tutorial-bluegreen-cluster:service-bluegreen", "targetArn": "arn:aws:ecs:
region
:aws_account_id
:service/service-bluegreen", "lastUpdatedAt": "2023-08-10T12:07:24.797000-05:00", "lifecycleEvents": [ { "lifecycleEventName": "BeforeInstall", "startTime": "2023-08-10T12:06:22.493000-05:00", "endTime": "2023-08-10T12:06:22.790000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "Install", "startTime": "2023-08-10T12:06:22.936000-05:00", "endTime": "2023-08-10T12:08:25.939000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "AfterInstall", "startTime": "2023-08-10T12:08:26.089000-05:00", "endTime": "2023-08-10T12:08:26.403000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "BeforeAllowTraffic", "startTime": "2023-08-10T12:08:26.926000-05:00", "endTime": "2023-08-10T12:08:27.256000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "AllowTraffic", "startTime": "2023-08-10T12:08:27.416000-05:00", "endTime": "2023-08-10T12:08:28.195000-05:00", "status": "Succeeded" }, { "lifecycleEventName": "AfterAllowTraffic", "startTime": "2023-08-10T12:08:28.715000-05:00", "endTime": "2023-08-10T12:08:28.994000-05:00", "status": "Succeeded" } ], "status": "Succeeded", "taskSetsInfo": [ { "identifer": "ecs-svc/9223370493425779968", "desiredCount": 1, "pendingCount": 0, "runningCount": 1, "status": "ACTIVE", "trafficWeight": 0.0, "targetGroup": { "name": "bluegreentarget1" }, "taskSetLabel": "Blue" }, { "identifer": "ecs-svc/9223370493423413672", "desiredCount": 1, "pendingCount": 0, "runningCount": 1, "status": "PRIMARY", "trafficWeight": 100.0, "targetGroup": { "name": "bluegreentarget2" }, "taskSetLabel": "Green" } ] } } }
ステップ 7: クリーンアップする
このチュートリアルが終了したら、使用していないリソースに対する料金が発生しないように、チュートリアルに関連付けられたリソースをクリーンアップします。
チュートリアルに関連付けられたリソースのクリーンアップ
-
[デプロイグループの削除]コマンドを使用して、CodeDeploy デプロイグループを削除します。
aws deploy delete-deployment-group \ --application-name
tutorial-bluegreen-app
\ --deployment-group-nametutorial-bluegreen-dg
\ --regionus-east-1
-
[アプリケーションの削除]コマンドを使用して、 CodeDeploy アプリケーションを削除します。
aws deploy delete-application \ --application-name
tutorial-bluegreen-app
\ --regionus-east-1
-
[削除サービス]コマンドを使用して、Amazon ECS サービスを削除します。
--force
フラグを使用すると、タスクがゼロになっていなくてもサービスを削除できます。aws ecs delete-service \ --service arn:aws:ecs:
region
:aws_account_id
:service/service-bluegreen
\ --force \ --regionus-east-1
-
[delete-cluster] コマンドを使用して、Amazon ECS クラスターを削除します。
aws ecs delete-cluster \ --cluster
tutorial-bluegreen-cluster
\ --regionus-east-1
-
[s3 rm]コマンドを使用して、Amazon S3 バケットから AppSpec ファイルを削除します。
aws s3 rm s3://
tutorial-bluegreen-bucket/appspec.yaml
-
[s3 rb]コマンドを使用して、 Amazon S3 バケットを削除します。
aws s3 rb s3://
tutorial-bluegreen-bucket
-
[delete-load-balancer] コマンドを使用して、Application Load Balancerを削除します。
aws elbv2 delete-load-balancer \ --load-balancer-arn arn:aws:elasticloadbalancing:
region
:aws_account_id
:loadbalancer/app/bluegreen-alb/e5ba62739c16e642
\ --regionus-east-1
-
[delete-target-group]コマンドを使用して、2 つのApplication Load Balancer ターゲットグループを削除します。
aws elbv2 delete-target-group \ --target-group-arn arn:aws:elasticloadbalancing:
region
:aws_account_id
:targetgroup/bluegreentarget1/209a844cd01825a4
\ --regionus-east-1
aws elbv2 delete-target-group \ --target-group-arn arn:aws:elasticloadbalancing:
region
:aws_account_id
:targetgroup/bluegreentarget2/708d384187a3cfdc
\ --regionus-east-1