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
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
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
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
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:
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
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
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
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
- The file is successfully pushed to the master branch, Confirm by checking the file in the Codecommit repository.
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
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.
- Check the runtime logs in the Build logs tab.
- On to the ECR repository to confirm the newly created image.
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
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)