Monday, 5 February 2024

AWS CodeBuild Made Easy: Hands-On Demos for Building & Testing Code

 

Introduction

In this blog, we will see the Hands-on practice of AWS CodeBuild in detail.

So, in this blog, we will continue with all the steps of getting started with the AWS CodeBuild in detail.

In the world of DevOps continuous integration and continuous deployment (CI/CD), automation is the key to success. AWS (Amazon Web Services) provides a wide range of tools and services to streamline your development and deployment processes.

Enter AWS CodeBuild, your trusty helper in this journey. It’s like having a magic wand that can do the hard work of building, testing, and packaging your code for you.

In this blog, we are going to take you on a ride into the world of AWS CodeBuild. Through some practical examples, we will show you how it can simplify your software development and make your life easier. 

 AWS CodeBuild is one such service that plays a crucial role in automating your build and test workflows. So, get ready as we explore the path to making your work automatic, efficient, and awesome!

As I said above we have already discussed the theory of AWS CodeBuild but for we will dive deep into AWS CodeBuild, exploring its features and capabilities through a series of hands-on demos.

Table of Contents

  1. Introduction to AWS CodeBuild
  2. Setting Up AWS CodeBuild
  3. Creating a CodeBuild Project
  4. Configuring Build Specifications
  5. Building and Testing Code
  6. Using AWS CodePipeline with CodeBuild
  7. Advanced Features and Best Practices
  8. Conclusion

1. Introduction to AWS CodeBuild

In the ever-evolving landscape of software development and deployment, automation is the beacon that guides us toward efficiency, reliability, and speed. AWS CodeBuild, a fundamental service offered by Amazon Web Services (AWS), stands at the forefront of this automation revolution. It plays a central role in simplifying and accelerating the process of building and testing code, a fundamental element of the modern software development life cycle.

In the world of software development, where the demands for faster and more reliable delivery are ever-increasing, manual and error-prone processes can no longer keep pace. Automation is the answer to this challenge, as it allows developers to focus on the creative aspects of their work while leaving the repetitive and time-consuming tasks to specialized tools.

AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces ready-to-deploy software packages. It’s designed to be highly scalable, customizable, and tightly integrated with other AWS services, making it an ideal choice for automating the build and test phases of your software development process.

This is the short introduction of the AWS CodeBuild.

Are you ready? Let’s begin!

2. Setting Up AWS CodeBuild

At this point, we will set up an AWS CodeBuild.

In this image, we have shown the exact process of AWS CodeBuild working.

Let us explain the diagram, so you will get a clear idea about it.

In the above diagram, there are two terms used i.e. Inputs, and Outputs.
Now first we will explain what inputs have.
In the Inputs, it has source code, it has a “build.yaml” file as input to the AWS CodeBuild.
Now, source code is a code provided to the AWS CodeBuild as an input.
Then “build.yaml” file which defines —

1. It defines how to run code.
2. It defines which command to use.
3. It defines which environment to use for your code.

This is the input term provided as an input to the AWS CodeBuild.

Now, we will see what the output is.

In the output, it has Artifact ( deployable source code file contains) and CloudWatch logs. 

The word output itself defines the meaning of the output word.
It is the output generated by AWS CodeBuild.

This is the explanation of the above image.

Before diving into the hands-on demos, you will need an AWS account. This is an AWS-related blog, so I am considering that you have an account on AWS. Once you have an AWS account, navigate to the AWS Management Console.

2.1. Steps to Set Up AWS CodeBuild:

Setting up AWS CodeBuild is a crucial step in automating your build and test workflows.

Here, we’ll walk you through the essential steps to set up AWS CodeBuild:

  1. Open the AWS Management Console:


To begin, log in to your AWS account and access the AWS Management Console.

  1. Navigate to CodeBuild:

In the AWS Management Console, you can locate the AWS CodeBuild service by searching for “CodeBuild” in the search bar or by navigating to it through the “Developer Tools” section.

Now, we have successfully logged into the AWS Management Console. 

3. Creating a CodeBuild Project

Let us create a CodeBuild project from the very beginning:

Demo 1: Creating a CodeBuild Project

Once you are inside the AWS CodeBuild service, click on the “Create build project” button as shown below.


This action initiates the process of setting up a new build project from the very beginning.

  1. Project Configuration:

    Here, you will need to provide the following information:
  1. Project Name:

    Give your project a descriptive name. This is how you will identify it later in the CodeBuild console.

For this example, let’s name it “Mydemoproj.”

  1. Source Provider:

    In the source provider,  Choose the source code repository you are using for your Node.js application. 


In our example, we are selecting “GitHub.”

  1. Repository:

    Select the specific repository and branch you want to build. For instance, choose your Node.js application repository on GitHub and specify the branch you want to use, e.g., “main.”

  1. Environment:

    In the “Environment” section, you will define the build environment:

Environment Image:

Select an environment image that matches the requirements of your Node.js application. For Node.js, you can choose an environment image like “Ubuntu” since Node.js can run on Ubuntu.

Environment Type:

Choose “standard” because you will be using the environment and tools provided by AWS for this example. If you had specific tools to bring to the environment, you could choose “custom.”

  1. Buildspec:

    The “Buildspec” is where you define the instructions for the build and test phases. You can either write a “buildspec.yml” file manually or use the AWS CodeBuild console to create a template. 

For our Node.js application, the buildspec.yml could look like this:

  • Build Specifications: Use a buildspec.yml file to define your build commands and test steps.
  1. Artifacts:
    • Artefacts Packaging: Define where to store build artefacts (e.g., Amazon S3).
  2. Service Role:
    • Create a service role or use an existing one to allow CodeBuild to interact with other AWS services.
  3. Logs:
    • Define where build logs should be stored (e.g., CloudWatch Logs).
  4. Advanced Settings:
    • Configure environment variables, cache settings, and timeouts.
  5. Review and Create:
    • Review your project settings and create the project.

4. Configuring Build Specifications

The heart of your CodeBuild project is the buildspec.yml file. This YAML file defines what happens during the build and test phases.

After creating an AWS CodeBuild project, the next crucial step is to configure the build specifications. These build specifications, defined in a buildspec.yml file, outline the exact steps and commands that AWS CodeBuild will follow to build, test, and package your application. In this step, we’ll dive into the details of configuring build specifications for your CodeBuild project.

Demo 2: Creating a buildspec.yml File

To get started with build specifications, you need to create a buildspec.yml file. This file will be included in your source code repository and serves as a blueprint for CodeBuild to follow. Here’s an example buildspec.yml for a Node.js application:

Here’s a sample buildspec.yml file for a Node.js application:




In this example:

  • version: 0.2 specifies the format version of the buildspec file.
  • phases define the different phases of your build process. In this example, we have two phases: install and build.
  • commands under each phase list the specific commands to execute. For the install phase, we run npm install to install project dependencies. In the build phase, we execute npm test to run tests.
  • artifacts specify what files or directories should be included in the build artifacts. The files: ‘**/*’ means that all files and directories in your project should be included.


Phases: Define the build phases, such as installing dependencies and running tests.

  • Commands: Specify the commands to be executed in each phase.
  • Artifacts: Specify what files should be included in the build artefacts.

Step 2: Include Buildspec in Your Repository

Save the buildspec.yml file in the root directory of your source code repository, and commit it to your version control system (e.g., Git). This allows CodeBuild to access the buildspec and execute the defined commands when a build is triggered.

Step 3: Project Configuration

As a reminder, during the AWS CodeBuild project configuration (which we discussed in a previous step), you specify the source provider, the environment image, and other settings. You also indicate where the build artifacts should be stored.

Make sure that the source provider you selected (e.g., GitHub) is correctly configured to allow CodeBuild to access your repository.

Step 4: Triggering Builds

After configuring your build specifications, you can trigger a build in several ways:

  • Manual Start: In the AWS CodeBuild console, you can manually start a build for your project. This is useful for testing and debugging your buildspec.
  • Integration with CI/CD: You can integrate CodeBuild into your Continuous Integration/Continuous Deployment (CI/CD) pipeline. For example, you can configure AWS CodePipeline to automatically trigger CodeBuild whenever changes are pushed to your repository.

Step 5: Monitoring and Logs

As your build runs, you can monitor its progress and access logs in the AWS CodeBuild console. This includes details on the commands executed and any errors encountered during the build.

Step 6: Review and Iterate

Review your build logs and the outcome of your build process. If there are issues or improvements to be made, you can iterate on your buildspec.yml file to refine the build process. Testing and refining the buildspec is a common practice to ensure your code is built correctly and your tests pass consistently.

End of Configuring Build Specifications

Configuring build specifications is a fundamental part of using AWS CodeBuild. It allows you to define a repeatable and automated process for building and testing your application. Your buildspec.yml serves as the recipe for AWS CodeBuild, guiding it through the build phases and producing reliable, consistent results.

5. Building and Testing Code

After configuring build specifications in AWS CodeBuild, the next step is to initiate a build, during which CodeBuild follows the instructions specified in the buildspec file (buildspec.yml) to build and test your code. In this step, we’ll delve into the process of building and testing code using AWS CodeBuild.

Step 1: Triggering a Build

Before your code can be built and tested, you need to initiate a build. There are several methods to trigger a build:

  • Manual Start: In the AWS CodeBuild console, you can manually start a build for your project. This is useful for testing and debugging your build process. Here’s how you can manually start a build:
    • In the AWS CodeBuild console, locate your project and click on it to access the project details.
    • Click the “Start build” button to initiate a manual build. This will trigger CodeBuild to follow the instructions specified in your buildspec.yml file.
  • Integration with CI/CD: In a Continuous Integration/Continuous Deployment (CI/CD) pipeline, AWS CodeBuild can be automatically triggered whenever changes are pushed to your source code repository. This is a common and powerful way to ensure that code is automatically built and tested upon changes.

Step 2: CodeBuild Execution

Once a build is triggered, AWS CodeBuild will execute the following steps as defined in your buildspec.yml:

  • Install Phase: In this phase, CodeBuild will execute the commands specified in the install section of your buildspec.yml. It’s common to use this phase for tasks such as installing project dependencies or setting up the build environment.
  • Build Phase: The build section of the buildspec.yml contains commands that are executed in the build phase. This is where your application’s build and test processes occur. For example, you might compile your code, run unit tests, and generate artifacts.
  • Artifacts Generation: As specified in the artifacts section of your buildspec.yml, CodeBuild will package and store the build artifacts. These artifacts can include compiled code, test reports, or other files that are generated during the build.

Step 3: Monitoring Progress and Logs

During the build process, you can monitor its progress in the AWS CodeBuild console. Here, you can track the status of your build, view build logs, and access detailed information about the build execution.

  • Logs: AWS CodeBuild provides logs that include detailed information about the build execution, such as the commands that were run, any errors encountered, and the duration of each phase.
  • CloudWatch Logs: Build logs are typically stored in Amazon CloudWatch Logs. You can access and analyze these logs for troubleshooting and auditing purposes.

Step 4: Review and Iterate

After a build is completed, it’s important to review the build logs and the outcome of your build process. This allows you to ensure that your code was built correctly and that your tests passed consistently.

  • If there are issues or areas for improvement, you can iterate on your buildspec.yml file to refine the build process. This iterative approach helps you maintain a reliable and efficient build and test workflow.

End of Building and Testing Code

The process of building and testing code in AWS CodeBuild follows the instructions specified in your buildspec.yml. By triggering builds manually or integrating CodeBuild into a CI/CD pipeline, you can automate the build and test workflows, leading to efficient and reliable software development and deployment.

With your CodeBuild project and buildspec.yml file in place, it’s time to trigger a build and test your code.

Demo 3: Triggering a CodeBuild Build

  • Manual Build:
    • Navigate to your CodeBuild project in the AWS Management Console.
    • Click “Start build.”
  • Automated Build:
    • Integrate CodeBuild with AWS CodePipeline for automatic builds on code changes.

6. Using AWS CodePipeline with CodeBuild

AWS CodePipeline allows you to automate your entire CI/CD workflow, including building, testing, and deploying your code using AWS CodeBuild as a crucial step.

AWS CodePipeline is a service that allows you to build, test, and deploy your code changes continuously. By integrating AWS CodeBuild into your CodePipeline, you can automate and streamline your entire software release process. In this step, we will explore how to use AWS CodePipeline with AWS CodeBuild.

Step 1: Create a CodePipeline

  • Navigate to AWS CodePipeline:
    • Use the search bar at the top and type “CodePipeline.”
    • Click on the “CodePipeline” result to enter the AWS CodePipeline service.
  • Create a New Pipeline:
    • Click the “Create pipeline” button to initiate the process of setting up a new pipeline.
  • Pipeline Settings:
    • Define a pipeline name and choose the service role. The service role should have permissions to interact with AWS services and resources, including AWS CodeBuild.
  • Source Stage:
    • Configure the source stage by choosing the source provider where your code is stored, such as GitHub, CodeCommit, or an S3 bucket. You will also specify the repository and branch.
  • Build Stage:
    • In this stage, you will add an action to build your code. Choose “AWS CodeBuild” as the build provider, and select the project you created earlier in AWS CodeBuild.
  • Deploy Stage:
    • If your pipeline includes a deployment stage, you can configure it based on your deployment targets. This could be an AWS Elastic Beanstalk environment, an Amazon S3 bucket, or an AWS Lambda function, depending on your application.
  • Review and Create:
    • Review your pipeline configuration to ensure everything is set up correctly, and then click “Create pipeline.”

Step 2: Configure Your Pipeline Stages

  • The source stage monitors changes in your source code repository and triggers the pipeline automatically when new code is pushed.
  • The build stage uses AWS CodeBuild to build and test your code according to the specifications defined in your buildspec.yml.
  • If you have a deployment stage, it will take the output of the build stage and deploy it to your target environment.

Step 3: Monitor and Automate

Once your pipeline is set up, it will continuously monitor changes in your source code repository. When changes are detected, it automatically triggers the defined stages, which can include building and testing with AWS CodeBuild.

  • You can monitor the progress and status of your pipeline in the AWS CodePipeline console. This allows you to track the status of builds, deployments, and any errors that may occur.

Step 4: Review and Iterate

Just like when using AWS CodeBuild on its own, it’s important to regularly review your pipeline’s logs and the outcomes of your builds and deployments.

If issues or improvements are identified, you can update your pipeline’s configurations and the buildspec.yml file to refine your entire software release process.

End of Using AWS CodePipeline with CodeBuild

By integrating AWS CodeBuild into AWS CodePipeline, you automate the entire software development and deployment process. Changes in your source code repository trigger a sequence of actions, from building and testing with CodeBuild to deploying your application to various environments. This automation results in faster, more reliable, and efficient software delivery.

Demo 4: Setting Up CodePipeline with CodeBuild

  • Create a CodePipeline:
    • Navigate to AWS CodePipeline in the AWS Management Console.
    • Create a new pipeline and configure your source, build, and deployment stages.
  • Add CodeBuild as a Stage:
    • Add a build stage to your pipeline and select your CodeBuild project.
  • Configure Triggers:
    • Set up triggers to automatically start the pipeline when code changes are detected.

Conclusion

AWS CodeBuild is a powerful service that enables you to automate your build and test workflows in a scalable and customizable manner. By following the hands-on demos and best practices outlined in this blog, you can streamline your development process, increase code quality, and accelerate your software delivery pipeline. Start building with AWS CodeBuild today and experience the benefits of automated CI/CD.

Remember, automation is key in the DevOps world, and AWS CodeBuild is here to make your development life easier, one build at a time.