ハンズオン: コンテナ定義ファイルの作成

参考: https://github.com/awslabs/ecs-nginx-reverse-proxy/blob/master/reverse-proxy/task-definition.json

パラメータの指定

変数の設定:

export AWS_DEFAULT_REGION='ap-northeast-1'

変数の設定:

PRJ_NAME='handson-cli'

APP

変数の設定:

ECR_REPOSITORY_NAME='handson-cli-app-repository'

コマンド:

CONTAINER_IMAGE_TAG="0.0.1"

コマンド:

ECR_REPOSITORY_URI=$( \
  aws ecr describe-repositories \
    --query "repositories[?repositoryName == \`${ECR_REPOSITORY_NAME}\`].repositoryUri" \
    --output text  \
) \
  && echo ${ECR_REPOSITORY_URI}

結果(例):

XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/handson-cli-app-repository

コマンド:

aws ecr describe-images \
  --repository-name ${ECR_REPOSITORY_NAME} \
  --query "imageDetails[?contains(imageTags[], \`${CONTAINER_IMAGE_TAG}\`)].imageDigest" \
  --output text

結果(例):

sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

課題

1.15.x〜1.16.12(2018年9月時点)では、IDが数字1桁の場合にマッチしない(バグ?)

(2桁以上の数字ではためしていない。)

コマンド:

ECR_REPOSITORY_IMAGE_APP="${ECR_REPOSITORY_URI}:${CONTAINER_IMAGE_TAG}"

Reverse Proxy

変数の設定:

ECR_REPOSITORY_NAME='handson-cli-reverse-proxy-repository'

コマンド:

CONTAINER_IMAGE_TAG="0.0.1"

コマンド:

ECR_REPOSITORY_URI=$( \
  aws ecr describe-repositories \
    --query "repositories[?repositoryName == \`${ECR_REPOSITORY_NAME}\`].repositoryUri" \
    --output text  \
) \
  && echo ${ECR_REPOSITORY_URI}

結果(例):

XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/handson-cli-app-repository

コマンド:

aws ecr describe-images \
  --repository-name ${ECR_REPOSITORY_NAME} \
  --query "imageDetails[?contains(imageTags[], \`${CONTAINER_IMAGE_TAG}\`)].imageDigest" \
  --output text

結果(例):

sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

課題

1.15.x〜1.16.12(2018年9月時点)では、IDが数字1桁の場合にマッチしない(バグ?)

(2桁以上の数字ではためしていない。)

コマンド:

ECR_REPOSITORY_IMAGE_PROXY="${ECR_REPOSITORY_URI}:${CONTAINER_IMAGE_TAG}"

コンテナ定義ファイルの作成

コマンド:

mkdir -p ${HOME}/tmp/conf-${PRJ_NAME}/

変数の設定:

FILE_ECS_CONTAINER_DEFINITION="${HOME}/tmp/conf-${PRJ_NAME}/${PRJ_NAME}-container-definition.json" \
  && echo ${FILE_ECS_CONTAINER_DEFINITION}

結果(例):

${HOME}/tmp/handson-cli/handson-cli-container-definition.json

変数の確認:

cat << ECHO

  FILE_ECS_CONTAINER_DEFINITION: ${FILE_ECS_CONTAINER_DEFINITION}
  ECR_REPOSITORY_IMAGE_APP:   ${ECR_REPOSITORY_IMAGE_APP}
  ECR_REPOSITORY_IMAGE_PROXY: ${ECR_REPOSITORY_IMAGE_PROXY}

ECHO

コマンド:

cat << EOF > ${FILE_ECS_CONTAINER_DEFINITION}
[
  {
    "name": "nginx",
    "image": "${ECR_REPOSITORY_IMAGE_PROXY}",
    "memory": 256,
    "cpu": 256,
    "essential": true,
    "portMappings": [
      {
        "containerPort": 80,
        "hostPort": ${HOST_PORT},
        "protocol": "tcp"
      }
    ],
    "links": [
      "app"
    ]
  },
  {
    "name": "app",
    "image": "${ECR_REPOSITORY_IMAGE_APP}",
    "memory": 256,
    "cpu": 256,
    "essential": true
  }
]
EOF

cat ${FILE_ECS_CONTAINER_DEFINITION}

結果(例):

[
  {
    "name": "nginx",
    "image": "XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/handson-cli-reverse-proxy-repository:0.0.1",
    "memory": 256,
    "cpu": 256,
    "essential": true,
    "portMappings": [
      {
        "containerPort": 80,
        "hostPort": 80,
        "protocol": "tcp"
      }
    ],
    "links": [
      "app"
    ]
  },
  {
    "name": "app",
    "image": "XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/handson-cli-app-repository:0.0.1",
    "memory": 256,
    "cpu": 256,
    "essential": true
  }
]

完了