6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Amazon ECS - Blue/Green デプロイでALBのターゲットグループを変更

Last updated at Posted at 2020-02-13

ALBのターゲットグループ bluegreen-target1 でタスクを起動させる。
CodeDeploy で bluegreen-target2 に変更させる。

#1.ALB#

ロードバランサー

aws elbv2 create-load-balancer \
     --name bluegreen-alb \
     --subnets subnet-00000000000000000 subnet-00000000000000000 \
     --security-groups sg-00000000000000000 \

ターゲットグループ

aws elbv2 create-target-group \
     --name bluegreen-target1 \
     --protocol HTTP \
     --port 80 \
     --target-type ip \
     --vpc-id vpc-00000000000000000

リスナー

aws elbv2 create-listener \
     --load-balancer-arn arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:loadbalancer/app/bluegreen-alb/ae66a1b7b0611f6c \
     --protocol HTTP \
     --port 80 \
     --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:targetgroup/bluegreen-target1/443d84907a4e35e3

#2.ECSクラスター#

aws ecs create-cluster \
     --cluster-name bluegreen-cluster

#3.タスク定義#

task-definitions.json
{
    "family": "task-definitions",
        "networkMode": "awsvpc",
        "containerDefinitions": [
            {
                "name": "sample-app",
                "image": "httpd:2.4",
                "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",
        "executionRoleArn": "arn:aws:iam::268546037544:role/ecsTaskExecutionRole"
}
aws ecs register-task-definition \
     --cli-input-json file://task-definitions.json

#4.サービス#

service.json
{
    "cluster": "bluegreen-cluster",
    "serviceName": "service-bluegreen",
    "taskDefinition": "task-definitions",
    "loadBalancers": [
        {
            "targetGroupArn": "arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:targetgroup/bluegreen-target1/443d84907a4e35e3",
            "containerName": "sample-app",
            "containerPort": 80
        }
    ],
    "launchType": "FARGATE",
    "schedulingStrategy": "REPLICA",
    "deploymentController": {
        "type": "CODE_DEPLOY"
    },
    "platformVersion": "LATEST",
    "networkConfiguration": {
       "awsvpcConfiguration": {
          "assignPublicIp": "ENABLED",
          "securityGroups": [ "sg-00000000000000000" ],
          "subnets": [ "subnet-00000000000000000", "subnet-00000000000000000" ]
       }
    },
    "desiredCount": 1
}
aws ecs create-service \
     --cli-input-json file://service.json

#5. CodeDeploy#

aws deploy create-application \
     --application-name bluegreen-app \
     --compute-platform ECS
aws elbv2 create-target-group \
     --name bluegreen-target2 \
     --protocol HTTP \
     --port 80 \
     --target-type ip \
     --vpc-id "vpc-00000000000000000"
deployment-group.json
{
   "applicationName": "bluegreen-app",
   "autoRollbackConfiguration": {
      "enabled": true,
      "events": [ "DEPLOYMENT_FAILURE" ]
   },
   "blueGreenDeploymentConfiguration": {
      "deploymentReadyOption": {
         "actionOnTimeout": "CONTINUE_DEPLOYMENT",
         "waitTimeInMinutes": 0
      },
      "terminateBlueInstancesOnDeploymentSuccess": {
         "action": "TERMINATE",
         "terminationWaitTimeInMinutes": 5
      }
   },
   "deploymentGroupName": "bluegreen-deploymentgroup",
   "deploymentStyle": {
      "deploymentOption": "WITH_TRAFFIC_CONTROL",
      "deploymentType": "BLUE_GREEN"
   },
   "loadBalancerInfo": {
      "targetGroupPairInfoList": [
        {
          "targetGroups": [
             {
                 "name": "bluegreen-target1"
             },
             {
                 "name": "bluegreen-target2"
             }
          ],
          "prodTrafficRoute": {
              "listenerArns": [
                  "arn:aws:elasticloadbalancing:ap-northeast-1:000000000000:listener/app/bluegreen-alb/ae66a1b7b0611f6c/020fcdaa33680028"
              ]
          }
        }
      ]
   },
   "serviceRoleArn": "arn:aws:iam::000000000000:role/ecsCodeDeployRole",
   "ecsServices": [
       {
           "serviceName": "service-bluegreen",
           "clusterName": "bluegreen-cluster"
       }
   ]
}
aws deploy create-deployment-group \
     --cli-input-json file://deployment-group.json
appspec.yaml
version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "arn:aws:ecs:ap-northeast-1:000000000000:task-definition/task-definitions:3"
        LoadBalancerInfo:
          ContainerName: "sample-app"
          ContainerPort: 80
        PlatformVersion: "LATEST"
aws s3 mb s3://naata-bluegreen-bucket
aws s3 cp ./appspec.yaml s3://naata-bluegreen-bucket/appspec.yaml
create-deployment.json
{
    "applicationName": "bluegreen-app",
    "deploymentGroupName": "bluegreen-deploymentgroup",
    "revision": {
        "revisionType": "S3",
        "s3Location": {
            "bucket": "naata-bluegreen-bucket",
            "key": "appspec.yaml",
            "bundleType": "YAML"
        }
    }
}
aws deploy create-deployment \
     --cli-input-json file://create-deployment.json

#■リンク#
Amazon ECS

6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?