Chapter 12

CodeCommit作成

ShigeruOda
ShigeruOda
2022.10.25に更新

■CodeCommitの作成

  • git互換のソースリポジトリであるCodeCommitを作成

img

cmd

aws codecommit create-repository \
  --repository-name ContainerHandsOn \
  --repository-description "ContainerHandsOn" \
  --tags "key=Name,value=ContainerHandsOn"

result

{
    "repositoryMetadata": {
        "accountId": "123456789012",
        "repositoryId": "45fe4e7e-d7bb-4e06-9ee4-9d959888be4b",
        "repositoryName": "ContainerHandsOn",
        "repositoryDescription": "ContainerHandsOn",
        "lastModifiedDate": 1663244011.08,
        "creationDate": 1663244011.08,
        "cloneUrlHttp": "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/ContainerHandsOn",
        "cloneUrlSsh": "ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/ContainerHandsOn",
        "Arn": "arn:aws:codecommit:ap-northeast-1:123456789012:ContainerHandsOn"
    }
}

■CodeCommitリポジトリのクローン

  • git cloneでリポジトリをcloneします、中身は空です

cmd

cd ~/environment/
git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/ContainerHandsOn

result

Cloning into 'ContainerHandsOn'...
warning: You appear to have cloned an empty repository.

■資材の準備

  • 事前に作成した資材をgit管理ディレクトリにcopyします

cmd

cd ~/environment/ContainerHandsOn
cp -p ../Dockerfile ./
cp -pr ../src ./
ls -lR

result

.:
total 8
-rw-rw-r-- 1 ec2-user ec2-user   47 Sep 15 12:01 Dockerfile
drwxrwxr-x 2 ec2-user ec2-user 4096 Sep 15 12:01 src

./src:
total 4
-rw-rw-r-- 1 ec2-user ec2-user 190 Sep 15 12:01 index.php

■buildspec.ymlの新規作成

  • CodeBuildの仕様を記述したファイルを作成します

cmd

cd ~/environment/ContainerHandsOn
cat << EOF > buildspec.yml
version: 0.2
phases:
  install:
    runtime-versions:
        docker: 20
        
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - docker version
      - aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin ${AccountID}.dkr.ecr.ap-northeast-1.amazonaws.com
      - RepositoryUri=${AccountID}.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on
      - ImageTag=\$(echo \$CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)

  build:
    commands:
      - echo Build started on \`date\`
      - echo Building the Docker image...          
      - docker build -t jaws-days-2022/container-hands-on .
      - docker tag jaws-days-2022/container-hands-on:latest \${RepositoryUri}:latest
      - docker tag jaws-days-2022/container-hands-on:latest \${RepositoryUri}:\${ImageTag}
      - printf '{"Version":"1.0","ImageURI":"%s"}' \${RepositoryUri}:\${ImageTag} > imageDetail.json

  post_build:
    commands:
      - echo Build completed on \`date\`
      - echo Pushing the Docker image...
      - docker push \${RepositoryUri}:latest
      - docker push \${RepositoryUri}:\${ImageTag}

artifacts:
  files: imageDetail.json
EOF

result

(なし)

■buildspec.ymlの確認

cmd

cd ~/environment/ContainerHandsOn
ls -l buildspec.yml

result

-rw-rw-r-- 1 ec2-user ec2-user 1193 Sep 15 12:14 buildspec.yml

■appspec.ymlの新規作成

  • CodeDeployの仕様を記述したファイルを作成します
  • <TASK_DEFINITION>と記載されている箇所は後ほどCodePipelineで更新します

cmd

cd ~/environment/ContainerHandsOn
cat << EOF > appspec.yml
version: 0.0
Resources:
  - TargetService:
      Type: AWS::ECS::Service
      Properties:
        TaskDefinition: "<TASK_DEFINITION>"
        LoadBalancerInfo:
            ContainerName: "ContainerHandsOn" 
            ContainerPort: "80"
EOF

result

(なし)

■appspec.ymlの確認

cmd

cd ~/environment/ContainerHandsOn
ls -l appspec.yml

result

-rw-rw-r-- 1 ec2-user ec2-user 240 Sep 15 12:14 appspec.yml

■taskdef.jsonの新規作成

  • ECS Taskの仕様を記述したファイルを作成します
  • 前半のハンズオンで作成した内容を出力しています

cmd

aws ecs describe-task-definition \
  --task-definition ContainerHandsOn:${RevisionNo} \
  --query taskDefinition > taskdef.json

result

(なし)

■taskdef.jsonの変更

以下の変更を行います
<変更前>

"image": "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on:latest"  

<変更後>

"image": "<IMAGE_NAME>"  

<IMAGE_NAME>は後述のCodePipelineにて、最新のImage URIに置換する処理が組み込まれます

cmd(変更前確認)

cd ~/environment/ContainerHandsOn
grep '"image":' taskdef.json

result(変更前確認)

            "image": "123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/jaws-days-2022/container-hands-on:latest",

cmd(変更処理&変更後確認)

cd ~/environment/ContainerHandsOn
sed -i -e 's/\"image\".*/\"image\": \"<IMAGE_NAME>\",/g' taskdef.json
grep '"image":' taskdef.json

result

            "image": "<IMAGE_NAME>",

■CodeCommitへのPush

  • 作成したファイルをCodeCommitへPushし格納します

cmd

cd ~/environment/ContainerHandsOn
git config --global user.name "Your Name"
git config --global user.email you@example.com

git add -A
git commit -m "first commit"
git push origin master

result

[master (root-commit) 15052c6] first commit
 5 files changed, 131 insertions(+)
 create mode 100644 Dockerfile
 create mode 100644 appspec.yml
 create mode 100644 buildspec.yml
 create mode 100644 src/index.php
 create mode 100644 taskdef.json

Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 1.93 KiB | 1.93 MiB/s, done.
Total 8 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/ContainerHandsOn
 * [new branch]      master -> master