Add buildspec

A build spec is a collection of build commands and related settings, in YAML format, that AWS CodeBuild uses to run a build.

You are going to include a build specification as part of the source code.

You are going to add buildspec.yml to Northwind solution, not CDK project.

Create buildspec.yml file in the root of the solution (next to Northwind.sln file) that describes how to build and publish Docker container to the Amazon ECR.

version: 0.2

phases:
  install:
    runtime-versions:
      docker: 18
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

This build spec simply does the following:

  • Login to Amazon ECR
  • Build image (docker build)
  • Tag image (docker tag)
  • Push image to Amazon ECR (docker push)

Please note the following environment variables that you will setup in next module:

  • AWS_DEFAULT_REGION with a value of region ID
  • AWS_ACCOUNT_ID with a value of account ID
  • IMAGE_TAG with a value of Latest
  • IMAGE_REPO_NAME with a value of Amazon ECR repository name

Add this new file to source control:

Using provided remote VM

git add buildspec.yml
git commit -m "Add buildspec file"
git push aws main

Using your own computer

git add buildspec.yml
git commit -m "Add buildspec file"
git push origin main