Creating a CI/CD Pipeline with AWS CodePipeline, CodeBuild, and CodeDeploy

Creating a CI/CD Pipeline with AWS CodePipeline, CodeBuild, and CodeDeploy

Maryam Mairaj's photo
·

5 min read

Table of contents

In modern DevOps workflows, Continuous Integration and Continuous Deployment (CI/CD) pipelines are essential for automating software build, test, and deployment processes. AWS offers several fully managed services that streamline CI/CD workflows: AWS CodePipeline for orchestrating the process, AWS CodeBuild for building and testing code, and AWS CodeDeploy for automating the deployment across various compute services like EC2, ECS, and Lambda.

This blog post will create a complete CI/CD pipeline using AWS CodePipeline, CodeBuild, and CodeDeploy. This tutorial will help you automate the process of building, testing, and deploying an application to an EC2 instance.

Step 1: Setting Up the Source Repository

To begin, let’s set up a source repository on GitHub.

1. Create a Repository:

Go to GitHub and create a new repository for your application. Add the sample application code "https://github.com/MaryamMairaj123/AWS-DevOps/aws-devops-main/day-14" 2. Create a Personal Access Token: In GitHub, navigate to Settings > Developer Settings > Personal Access Tokens and generate a token with the necessary permissions. This will allow AWS to connect to your GitHub repository. 3. Configure GitHub in AWS CodePipeline: Next, you’ll authorize AWS CodePipeline to connect to your GitHub repository.

Step 2: Setting Up with the AWS System Manager

Go to the systems manager to secure sensitive information for docker registering. In the systems manager go to the parameters stores and click on Create parameters.

Image description

Image description

I have created three parameters.

Image description

Step 3: Setting Up AWS CodeBuild

AWS CodeBuild compiles your code, runs unit tests, and produces deployable artifacts.

1. Create a CodeBuild Project Go to the AWS Management Console and navigate to CodeBuild. Click Create Build Project. Enter a name for your project (e.g., MyApp-Build).

Image description

2. Configure Source Select GitHub as the source provider and choose the repository you created earlier.

Image description

3. Buildspec File Define a buildspec.yml version of the build spec that can be used in your CodeBuild project:

version: 0.2
env:
  parameter-store:
    DOCKER_REGISTRY_USERNAME: "/myapp/docker-credentials/username"
    DOCKER_REGISTRY_PASSWORD: "/myapp/docker-credentials/password"
    DOCKER_REGISTRY_URL: "/myapp/docker-registry/url"

phases:
  install:
    runtime-versions:
      python: 3.11
    commands:
      - pip install -r aws-devops-main/day-14/simple-python-app/requirements.txt

  pre_build:
    commands:
      - echo "Logging in to Docker registry..."
      - echo "$DOCKER_REGISTRY_PASSWORD" | docker login "$DOCKER_REGISTRY_URL" --username "$DOCKER_REGISTRY_USERNAME" --password-stdin

  build:
    commands:
      - cd aws-devops-main/day-14/simple-python-app
      - echo "Building Docker Image"
      - docker build -t "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/python-flask-app-service:latest" .
      - echo "Pushing Docker Image to Registry"
      - docker push "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/python-flask-app-service:latest"

  post_build:
    commands:
      - echo "Build is Successful"

4. IAM Role Ensure that your CodeBuild project has an IAM role with permissions to access the required services (CodeCommit, S3, CodeDeploy, etc.).

Image description

Step 4: Attach IAM policies with SSM

Now go to the IAM Roles and attach the policies of SSM

Image description

Step 5: Enable privileged

Go to the project edit and enable privileged and update project.

Image description

Image description

Step 6: Configure Deployment with AWS CodePipeline

1. Go to the AWS code pipeline and create a new code pipeline

Image description

Image description

2. Configure Source

Image description

3. Add Build Stage

Image description

4. Skip the deployment stage for now and create the pipeline.

Image description

5. Here is the Docker image

Image description

The CI part is done here.

Now Deploying CD part.

Step 7: Automating Deployment with AWS CodeDeploy

AWS CodeDeploy automates the deployment of your code to EC2 instances.

1. Set Up EC2 Instances Launch EC2 instances and install the CodeDeploy agent:

Image description

Add Tags

Image description

sudo yum install ruby
sudo yum install wget
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install 
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
systemctl status codedeploy-agent

2. Create a CodeDeploy Application In the AWS Management Console, navigate to CodeDeploy and click Create Application. Name the application and select EC2/On-Premises as the compute platform.

Image description

3. create a role To grant permissions to any service to access another service in AWS, we use IAM roles. Roles are used for services. Create a role and select code deploy then click next and then next

Image description

Select the role name and create the role.

Image description

Now go to the modified IAM roles.

Image description

Image description

After updating the IAM role we need to restart the service and check the status.

Image description

Now go to the code deploy and create a deployment group.

Image description

Image description

Image description

Now we have to create the deployment, go to the application, and create a deployment.

Image description

Image description

Copy the latest commit ID and paste it into the commit ID

Image description

Click on Create Deployment now

Image description

Build is successful

Image description

Now go to the code pipeline and edit tab Go to the build stage and add a stage. Set the name as code-deploy then add an action group

Image description

After done click on the save button

Image description

Finally, the application is running :)

Image description

Conclusion

Following this guide, you've successfully set up a CI/CD pipeline using AWS CodePipeline, CodeBuild, and CodeDeploy. This automated pipeline allows you to integrate, build, and deploy your application efficiently. As you refine your skills, consider adding additional stages for testing or integrating security checks.

Happy automating!