6-part series - (5) Create a CodeBuild Project, upload the code and push it to the master repository
Tags: codepipeline, codebuild, ecr, ecs
Published: 2023-02-17 00:06:22

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**
- As a reference, please read my **[1st article - 6-part series - (1) Deploy a Custom Docker Image with Centos on an Apache web server and Save it to AWS ECR Repository](https://dev.to/aws-builders/6-part-series-1-deploy-a-custom-docker-image-with-centos-on-an-apache-web-server-and-save-it-to-aws-ecr-repository-186)**

**2nd article**
- As a reference, please read my **[2nd article - 6-part series - (2) Create Task Definition in ECS and Application Load balancer for the Task to be run on Fargate Cluster](https://dev.to/aws-builders/6-part-series-2-create-task-definition-in-ecs-and-application-load-balancer-for-the-task-to-be-run-on-fargate-cluster-1ohm)**

**3rd article**
- As a reference, please read my **[3rd article - 6-part series - (3) Create Fargate Cluster, and a Service which can access customized Docker image ](https://dev.to/aws-builders/6-part-series-3-create-fargate-cluster-and-a-service-which-can-access-customized-docker-image-1dl7)**

**4th article**
- As a reference, please read my **[4th article - 6-part series - (4) Create, clone a CodeCommit Repo, move-push the Dockerfile and index.html to remote repository master branch](https://dev.to/aws-builders/6-part-series-4-create-clone-a-codecommit-repo-move-push-the-dockerfile-and-indexhtml-to-remote-repository-master-branch-37ng)**

_**Let’s get started!**_

Please visit my **[GitHub Repository for Docker/ECS/ECR articles](https://github.com/awsmine/Docker_projects)** 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](https://docs.aws.amazon.com/codecommit/latest/userguide/welcome.html)**

**[CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html)**

**[AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html)**


# 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thydwtkjpk6d0atpl5p2.png)


## 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
```

- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/noz6d7av4imghlau6qb6.png)


**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
```

**create the buildspec.yaml file**

- SSH into the EC2 Instance and navigate to the repo folder 

```
cd /opt/docker/my-codecommit-repo
```

- 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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eezca25egn6wacaqr3pn.png)



## 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
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e7zcrkwbdd64em7d24jo.png)


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



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cjp04svoif9dvhf5u7rh.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sbyxaxbjcg4jix6vj4e5.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5uuljbqmftxbiwx4xi3.png)


- Check the runtime logs in the Build logs tab.



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u3qepykitir61nbrdga4.png)




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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x2hksnnjl3g7qd6q2g62.png)


## 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
```

- Create the file with the name - imagedefinitions.json

- **vi imagedefinitions.json**

```
[
    {
        "name": "CONTAINER-NAME",
        "imageUri": "ECR-REPO-URI:latest"
    }
]
```

- **`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
```

- change

```
[
    {
        "name": "my_container",
        "imageUri": "<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest"
    }
]
```


## 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
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/muc4hvehr5360uhbdq4r.png)

# What we have done so far

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