Complete CI/CD pipeline on AWS using CodeBuild, CodePipeline and CodeDeploy - 1

·

4 min read

Complete CI/CD pipeline on AWS using CodeBuild, CodePipeline and CodeDeploy - 1

In this blog, I'll be sharing how I implemented a complete CI/CD on AWS for my Flask application.

https://github.com/RachanaVenkat/flask-app-aws-codebuild - This repository contains of a simple flask application which I deployed on an EC2 instance as the final result of the CI/CD process. I chose GitHub for source code management instead of AWS CodeCommit due to the numerous advantages it offers.

Continuous Integration -

Imagine you're running a movie studio. Your filmmakers (developers) are constantly creating new movie scenes (code) to add to your blockbuster film. To ensure each scene is perfect before it hits the big screen, you have an automated editing and review process (CI/CD pipeline) that checks and approves every scene.

Here's How It Works:

  1. Creating New Scenes:

    • Each filmmaker completes a new movie scene and submits it to the studio's main server (commits code to GitHub).
  2. Automated Editing and Review (Continuous Integration):

    • The studio's main server triggers the automated editing and review process (AWS CodePipeline) as soon as a new scene is submitted.

    • The first step is to check the script and footage for errors (code checkout and static code analysis).

    • The second step scans for any issues that might affect the film’s quality, such as audio problems or visual glitches (checking for security vulnerabilities).

    • The third step assembles the scene, adds special effects, and runs a preview to ensure it looks great (building and running Docker images).

      This entire process, from submitting the scene to passing all checks, is called Continuous Integration (CI).

      It ensures that every new scene added to the film is perfect and doesn't disrupt the overall quality of the movie.

Technical Breakdown

  • Code Committed to GitHub:

    • Filmmakers (developers) push their code (scenes) to the GitHub repository.
  • AWS CodePipeline Orchestration:

    • AWS CodePipeline automatically triggers and starts the CI process.
  • AWS CodeBuild Tasks: (varies depending on the application)

    • Code Checkout: Fetches the latest code from GitHub.

    • Static Code Analysis: Runs tools to check the quality of the code.

    • Security Vulnerabilities Check: Scans the code for potential security issues.

    • Build and Test: Builds the Docker image and runs tests to ensure everything works.

Practical Implementation:

  1. Setting up CodeBuild:

    buildspec.yml is the file you'll need to perform all tasks on CodeBuild -

    1. Environment Setup:

      • Utilizes AWS CodeBuild to automate the build process.

      • Fetches Docker registry credentials securely from AWS Systems Manager Parameter Store.

    2. Phases:

      • Install Phase:

        • Specifies Python 3.11 as the runtime version.
      • Pre_Build Phase:

        • Installs project dependencies(which is just Flask here) defined in requirements.txt.
      • Build Phase:

        • Builds and tests a simple Python Flask application.

        • Logs in to the Docker registry using credentials from AWS Parameter Store.

        • Builds a Docker image from the application and pushes it to the Docker registry.

    3. Post_Build Phase:

      • Displays a success message indicating the completion of the build process.

This configuration automates the build, test, and deployment process of a Python Flask application using AWS CodeBuild and securely manages Docker registry credentials using AWS Systems Manager Parameter Store.

Do not forget to assign an IAM role for CodeBuild to be able to access SSM parameter store.

Additionally, check a box called 'Privileged' to be able to build docker images.

Now, check if it works by 'start build' which should work - (check the logs for de-bugging)

We can now move on to setting up CodePipeline for orchestrating the process discussed above.

  1. Setting up CodePipeline:

    Link the GitHub repository to CodePipeline as below-

    Now, link it with CodeBuild as well to automate its operation-

    The final step is to add the deployment stage, which I will be writing in the next blog. Except for that, the Continuous Integration(Ci) is complete.

This can be checked by committing a code change in the connected repository, the build process will automatically be triggered and the docker image is built

This can be further verified in DockerHub-

Now, the image of our application is on DockerHub, which using AWS CodeDeploy is finally deployed in an EC2 instance.

See you in the next blog, have a good day :)