DEV Community

Revathi Joshi for AWS Community Builders

Posted on

6-part series - (5) Create a CodeBuild Project, upload the code and push it to the master repository

In this 6-part series on configuring a CI/CD pipeline using Customized Docker image on an Apache Web Server, Application Load Balancer, ECS, ECR, CodeCommit, CodeBuild, CodeDeply services -

In this 5th article, We will create a CodeBuild Project, upload the code and push it to the master repository

1st article

2nd article

3rd article

4th article

Let’s get started!

Please visit my GitHub Repository for Docker/ECS/ECR articles on various topics being updated on constant basis.

Objectives:

1. Create a CodeBuild Project

2. Create a buildspec.yaml file

3. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

4. Provide the ECR permissions to the Codebuild role

5. Start build project

6. Create a imagedefinitions.json file

7. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • AWS CLI.

Resources Used:

CodeCommit

CodeBuild

AWS CodePipeline

Steps for implementation to this project

1. Create a CodeBuild Project

  • On the Codecommit dashboard, Build, Build projects, Create build project, my_codebuild_project, Source provider : Select AWS CodeCommit, Repository - my-codecommit-repo, Branch - master, Environment image - Managed image, Operating system - Amazon Linux 2, Runtime(s) - Standard, image : Choose aws/codebuild/amazonlinux2-x86_64-standard:3.0

Check the privileged, as it will build the docker image.[Important]

  • In Service role, New service role - codebuild-my-service-role (Do not change), Buildspec, Use a buildspec file selected automatically, all defaults

  • Create build project

Image description

2. Create a buildspec.yaml file

  • On to the created ECR repository, click my-ecr, View push commands

  • Copy the first command, i.e the login command.

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode
  • Close the push commands page, Now copy the repository URI beside the repo name.
<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr
Enter fullscreen mode Exit fullscreen mode

Image description

In the following code, change only the URI

version: 0.2
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t my-ecr:latest .
      - docker tag my-ecr:latest <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode

create the buildspec.yaml file

  • SSH into the EC2 Instance and navigate to the repo folder
cd /opt/docker/my-codecommit-repo
Enter fullscreen mode Exit fullscreen mode
  • Create a buildspec.yaml file

  • vi buildspec.yaml

  • copy this code

version: 0.2
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t my-ecr:latest .
      - docker tag my-ecr:latest <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode

Image description

3. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

git add .

git status

git commit -m "Adding buildspec.yaml File"

git push
Enter fullscreen mode Exit fullscreen mode

Image description

  • The file is successfully pushed to the master branch, Confirm by checking the file in the Codecommit repository.

Image description

4. Provide the ECR permissions to the Codebuild role

  • On the IAM console, Roles and search for the role starting with codebuild- which was created with the codebuild project, codebuild-my-service-role, click codebuild-my-service-role, On the Permissions tab, Add permissions, Attach policies, Search and select AmazonEC2ContainerRegistryFullAccess, Attach policies

Image description

5. Start build project

  • CodeBuild under Developer Tools, click Build projects, my_codebuild_project, the Start build

  • Wait for 5-6 minutes to build the project.

Image description

  • Check the runtime logs in the Build logs tab.

Image description

  • On to the ECR repository to confirm the newly created image.

Image description

6. Create a imagedefinitions.json file

  • When we upload any code and push it to the master repository, it should automatically create a build and the build should be pushed to the ECR. From the ECR it should be pushed to the ECS container.
  • To do so, we need to create a CodePipeline.

  • Before creating CodePipeline, let us create imagedefinitions.json file.

  • SSH into the EC2 Instance and go to the repo folder

sudo su
cd /opt/docker/my-codecommit-repo
Enter fullscreen mode Exit fullscreen mode
  • Create the file with the name - imagedefinitions.json

  • vi imagedefinitions.json

[
    {
        "name": "CONTAINER-NAME",
        "imageUri": "ECR-REPO-URI:latest"
    }
]
Enter fullscreen mode Exit fullscreen mode
  • To get the Container name, Go to ECS, Task definition, my_task, Task definition: revision, my_task:2, Scroll down and find the container name, my_container

  • To get the ECS-Repo-URI, Go to ECR, Copy the URI for latest image

<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode
  • change
[
    {
        "name": "my_container",
        "imageUri": "<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest"
    }
]
Enter fullscreen mode Exit fullscreen mode

7. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

git add .
git commit -m "Adding imagedefinitions File"
git push
Enter fullscreen mode Exit fullscreen mode

Image description

What we have done so far

  • We have successfully created a CodeBuild Project, uploaded the code and pushed it to the master repository

Top comments (0)