Day 69: CI/CD pipeline on AWS: Part-2

Day 69: CI/CD pipeline on AWS: Part-2

AWS CodeBuild

What is CodeBuild & what are its features?

AWS CodeBuild is a fully managed CI/CD service provided by AWS. It is designed to automate the build, test, and deployment phases of your application development workflow. CodeBuild is highly customizable and can be integrated with various AWS services and third-party tools, making it a versatile choice for building and deploying applications.

Here are the key features and characteristics of AWS CodeBuild:

  1. Build Automation: CodeBuild automates the build, test, and deployment phases of your application development.

  2. Managed Build Environments: It offers managed build environments for various programming languages and tools.

  3. Scalability: CodeBuild can scale horizontally to handle multiple builds in parallel.

  4. Integration: It seamlessly integrates with other AWS services like CodePipeline and CodeCommit.

  5. Custom Build Scripts: You can use custom build scripts and commands to define your build process.

  6. Artifact Management: CodeBuild generates and stores build artifacts, which can be stored in Amazon S3 or other storage solutions.

  7. Logs and Monitoring: Provides detailed build logs and metrics for troubleshooting and monitoring.

  8. Security: Offers VPC support, IAM integration, and secure build environments.

  9. Build Triggers: You can trigger builds based on events such as code commits or pull requests.

  10. Environment Variables: Allows you to define environment variables for secure configuration.


Task-01 :

About Buildspec file for Codebuild.

A Buildspec file in AWS CodeBuild is a configuration file written in YAML or JSON that defines how your build project should run. It includes build commands, environment variables, and other settings needed to build and test your code. It provides instructions for CodeBuild on how to build your application and can be customized to suit your specific project requirements.

Create a simple index.html file in CodeCommit Repository

In our previous blog, we took a deep dive into AWS CodeCommit, an essential component in the AWS DevOps toolkit.

We explored how CodeCommit works, created an HTML file and also pushed the changes to the Repository.

In this blog, we will explore AWS CodeBuild and also make a few changes to the HTML file that we created earlier.

Create a new branch by using the command:

git checkout -b dev

Add a few changes to the HTML file

Commit the changes to the new branch- dev and push to the CodeCommit Repository.

Create a Pull Request on CodeCommit by merging between dev and master.

Scroll down and you can review the changes. Click on "Create pull request".

Click on "Merge"

There will be different types of merge. In this case, we select "Fast Forward"

The merge request is now successful from dev to master

This can also be reviewed on the file on the Master branch. The dev branch has been deleted.

Note
We can set the desired number of Approvals if a merge needs to be done from branch to branch using "Approval rule templates"

Build the index.html using nginx server

Go to Build--> Build projects--> Create Build project

Enter name and description

Select the source from where the code will be retrieved (e.g. S3, CodeCommit, GitHub, Bitbucket, etc). In this case, CodeCommit.

Proceed further to select the Repository and the desired branch.

Environment image is an environment/ place where the build will take place such as Linux environment or Select Docker image depending upon the requirement.

The service role is basically a type of role/ policy that grants CodeBuild the necessary permissions to access AWS resources and services during the build and deployment process. It defines what actions CodeBuild can perform within your AWS environment, ensuring secure and controlled access to resources needed for your builds.

Coming to the important aspect of CodeBuild, as mentioned above, buildspec is a type of configuration file that defines how the project should be executed.

To be continued in Task-02.....


Task-02 :

Add buildspec.yml file to the CodeCommit Repository and complete the build process.

Create a YAML file because as mentioned in the above screenshot, By default, CodeBuild looks for a file named buildspec.yml in the source code root directory.

#buildspec.yml

version: 0.2

phases:
  install:
    commands:
      - echo Installing NGINX
      - sudo apt-get update
      - sudo apt-get install nginx -y

  build:
    commands:
      - echo Build started on 'date'
      - cp index.html /var/www/html/

  post_build:
    commands:
      - echo Building Nginx

artifacts:
  files: 
    - '**/*' # this pattern is used to include all files and directories 
             # within the entire build environment recursively, 
             # starting from the root directory of the build environment.

commit the changes to the local master branch & push them to CodeCommit Repository.

You can review the changes on CodeCommit.

Coming back to the CodeBuild page, scroll down and select Artifact and Logs depending upon the requirements.

Create build project

We have created the project blueprint.

Complete the build process by clicking on "Start build"

After some time the build will complete.

Status: In progress

Status: Succeeded

Complete Phase details.

We can also store the files in artifacts.

Go to the build in CodeBuild--> Edit--> Artifacts

Select the S3 bucket that was created earlier in AWS S3.

Enter the name of the folder - (in this case-amin.zip)

Scroll down, select Artifacts packaging as ZIP and update artifacts

We can now rebuild on CodeBuild

At the same time, the files will be stored in the S3 bucket- "aminchivilkar-s3" as mentioned below.


Conclusion:

AWS CodeBuild is a handy cloud service that helps us build and test our code without the hassle of managing our servers. We learned about the Buildspec file, which guides CodeBuild on what to do. We also saw how to use CodeCommit to store our code and used an Nginx server to build a basic web page. This makes our code-building process easy and efficient.


Reference:

CI/CD pipeline on AWS: Part-3

aminchivilkar.hashnode.dev/day-70-cicd-pipeline-on-aws-part-3

Hope you like my post. Don't forget to like, comment, and share.