AWS CI-CD Project using Github, CodePipeline, CodeBuild, and CodeDeploy. Days 14 & 15

Introduction

In the journey of learning AWS CI-CD practices for a couple of days, I learned about the code build, code pipeline, and code deploy and created the project following the AWS CI-CD best practices. Here is the step I followed to build the project. You can check out the theory part of Code Commit here and Code Pipeline here.

Overview

As in the fig above we will use AWS code build for the continuous integration where we build the docker image and push it to our docker hub. But we will use GitHub instead of Code Commit as it is used widely in the industry. We will then create the Code Pipeline which will invoke both the Code Build and Code Deploy.

Code Build

We will start by creating the new Build Project.

We will provide the Project Name and Descriptions and then

We will provide GitHub as our Source provider and connect the GitHub.

We will use managed image and Ubuntu as our operating system and choose standard runtime.

We need to create the buildspec file for the code build where we will tell the code build what to do.

We can provide it both on the source provider or in an editor on the aws code build itself.

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
  pre_build:
    commands:
      - echo "Installing dependencies..."
      - pip install -r requirements.txt
  build:
    commands:
      - echo "Running tests..."
      - echo "Building Docker image..."
      - echo "$DOCKER_REGISTRY_PASSWORD" | docker login -u "$DOCKER_REGISTRY_USERNAME" --password-stdin "$DOCKER_REGISTRY_URL"
      - docker build -t "$DOCKER_REGISTRY_URL/$DOCKER_REGISTRY_USERNAME/simple-python-app:latest" .
      - docker push "docker.io/$DOCKER_REGISTRY_USERNAME/simple-python-app:latest"
  post_build:
    commands:
      - echo "Build completed successfully!"

We have stored our Docker credentials in AWS SSM for security purposes.

fig: AWS System manager.

Now our Build Process is Done and now we will start our Build and wait for it to succeed.

After the Code Build is successful we will create the code pipeline and integrate it with the Pipeline.

Code Pipeline

We will create the new Code Pipeline.

We provide the name for the pipeline and choose the Service Role.

In the next stage, we will provide the source Provider which is GitHub in our case and we establish our connection with GitHub.

In the third Stage, we will provide the detail about the Build Provider where we choose AWS Code Build as our Build Provider and chose the CodeBuild Project.

This is our Code Pipeline which we have integrated our source Provider and Code Build.

We look into it by changing the code in GitHub and it automatically detect the change in the Source Provider and invoked the Code Build immediately.

This Sums up the CI Part and We Will look after the CD part Now.

Code Deploy

After the CI part, we will implement the CD part.

First, we will create the EC2 instance where we will host our App and install the agent in the ec2 server. The agent plays a crucial role in the deployment process and facilitates the seamless coordination between your deployment configurations and the instances.

We First Create the application on EC2.

In the given Application we will create the deployment group and deployment.

Application is created now we will create the deployment group.

We then created the deployment group as shown in the above figure.

After that, we create the deployment by selecting the created deployment group.

After creating the deployment we then run the Deployment and wait for it.

And then finally we link the Code Deploy in our code pipeline to integrate together.

let's break down the key components in AWS CodeDeploy: Application, Deployment Group, and Deployment.

  1. Application:

    In AWS CodeDeploy, an Application is the highest-level logical entity. It represents the application or service that we want to deploy. The Application serves as a container for our deployment revisions and deployment configurations.

  2. Deployment Group:

    A Deployment Group is a set of EC2 instances, Lambda functions, or ECS tasks that we want to deploy our application. By creating Deployment Groups, you can target specific instances or groups of instances for deployment.

  3. Deployment:

    A Deployment in AWS CodeDeploy is an instance of deploying a specific revision of our application to a Deployment Group. Deployments are the actual process of updating our application on the target instances