#123 & #124 祝日スペシャル ECR + CodePipeline + CodeDeploy + ECS(Fargate) ハンズオン

ハンズオンの概要

前編: 本編 (構築: ECR)

ECR環境の構築 (handson-cli-repository)

前編: 本編 (構築: ECS)

1. ECS実行用IAMロールの作成

コンテナ実行用のIAMロールを作成します。

2. ECSタスク用IAMロールの作成

3. ECSクラスタ環境の構築

4. ロードバランサの構築

6. ECSサービスの構築

6.1. ECSコンテナ定義ファイル作成

5.2. ECSタスク定義の作成 (Fargate)

7. アクセス確認 (green)

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

変数の設定:

ELBV2_LB_NAME='handson-cli-alb'

コマンド:

ELBV2_LB_DNS_NAME=$( \
  aws elbv2 describe-load-balancers \
    --name ${ELBV2_LB_NAME} \
    --query 'LoadBalancers[].DNSName' \
    --output text \
) \
  && echo ${ELBV2_LB_DNS_NAME}

結果(例):

handson-cli-alb-xxxxxxxxxxxx.ap-northeast-1.elb.amazonaws.com

変数の設定:

URL_CHECK="http://${ELBV2_LB_DNS_NAME}/handson-cli-container/" \
  && echo ${URL_CHECK}

コマンド:

curl ${URL_CHECK}

nginxの handson-cli-container が存在しないので404が表示されます。

結果(例):

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.8</center>
</body>
</html>

後編: 本編 (構築: ECS blue環境)

1. 8080/tcp用ECSサービスの構築

2. アクセス確認 (8080/tcp)

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

変数の設定:

ELBV2_LB_NAME='handson-cli-alb'

コマンド:

ELBV2_LB_DNS_NAME=$( \
  aws elbv2 describe-load-balancers \
    --name ${ELBV2_LB_NAME} \
    --query 'LoadBalancers[].DNSName' \
    --output text \
) \
  && echo ${ELBV2_LB_DNS_NAME}

結果(例):

handson-cli-alb-xxxxxxxxxxxx.ap-northeast-1.elb.amazonaws.com

変数の設定:

URL_CHECK="http://${ELBV2_LB_DNS_NAME}:8080/handson-cli-container/" \
  && echo ${URL_CHECK}

コマンド:

curl ${URL_CHECK}

nginxの handson-cli-container が存在しないので404が表示されます。

結果(例):

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.15.8</center>
</body>
</html>

後編:

1. 本編 (構築: CodeCommit)

課題

  • タスク定義ファイルの作成
  • AppSpec ファイルの作成

taskdef.json:

{
  "executionRoleArn": "arn:aws:iam::216399753842:role/handson-cli-ecs-execution-role",
  "containerDefinitions": [
      {
          "name": "handson-cli-container",
          "image": "<IMAGE1_NAME>",
          "essential": true,
          "portMappings": [
              {
                  "hostPort": 80,
                  "protocol": "tcp",
                  "containerPort": 80
              }
          ]
      }
  ],
  "requiresCompatibilities": [
      "FARGATE"
  ],
  "networkMode": "awsvpc",
  "cpu": "256",
  "memory": "512",
  "family": "handson-cli-task-definition"
}

appspec.yaml:

version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: <TASK_DEFINITION>
        LoadBalancerInfo:
          ContainerName: "handson-cli-container"
          ContainerPort: 80

2. CodeDeployアプリケーションの構築

課題

  • CodeDeploy用IAMロールの作成
  • CodeDeployデプロイメントグループの構築 (ECS)

3. CodePipelinパイプラインの構築

課題

  • パイプライン用IAMポリシの作成
  • パイプライン用IAMロールの作成
  • パイプラインの作成

4. パイプラインの動作確認

課題

本編 (破棄)

1. ECSサービスの停止 (green)

1.1. ECSサービス desired count更新

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

変数の設定:

ECS_CLUSTER_NAME='handson-cli-cluster'

変数の設定:

ECS_SERVICE_NAME='handson-cli-service'

コマンド:

ECS_SERVICE_ARN=$( \
  aws ecs list-services \
    --cluster ${ECS_CLUSTER_NAME} \
    --query "serviceArns[?contains(@,\`${ECS_SERVICE_NAME}\`)]" \
    --output text \
) \
  && echo ${ECS_SERVICE_ARN}

結果(例):

arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:service/handson-cli-service

コマンド:

aws ecs update-service \
  --desired-count 0 \
  --service ${ECS_SERVICE_ARN} \
  --cluster ${ECS_CLUSTER_NAME}

2. ECSサービスの停止 (blue)

1.1. ECSサービス desired count更新

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

変数の設定:

ECS_CLUSTER_NAME='handson-cli-cluster'

変数の設定:

ECS_SERVICE_NAME='handson-cli-blue-service'

コマンド:

ECS_SERVICE_ARN=$( \
  aws ecs list-services \
    --cluster ${ECS_CLUSTER_NAME} \
    --query "serviceArns[?contains(@,\`${ECS_SERVICE_NAME}\`)]" \
    --output text \
) \
  && echo ${ECS_SERVICE_ARN}

結果(例):

arn:aws:ecs:ap-northeast-1:XXXXXXXXXXXX:service/handson-cli-blue-service

コマンド:

aws ecs update-service \
  --desired-count 0 \
  --service ${ECS_SERVICE_ARN} \
  --cluster ${ECS_CLUSTER_NAME}