In this blog , Iโll be sharing how i implemented the CI/CD on AWS for the Flask Application.
The Following repository consists of the code for the Flask application.
https://github.com/Joshna454/AWS-CI-CD
Continuous Integration:
Continuous Integration is a Software development practice where developers frequently integrate their code changes into a shared repository, often multiple times a day. Each integration is automatically verified by running tests and builds, which helps detect errors early and ensures that the codebase remains in a healthy state.
Starting with Code Build, the AWS Code Build is a fully managed Continuous Integration Service that Compiles the Source Code, runs tests, and produces software packages that are ready to deploy.
AWS Code Build Phases:
-
Code Checkout: Fetches the code from GitHub.
-
Static Code Analysis: CodeBuild can run static code analysis tools to check code quality, such as checking for bugs.
-
Security Vulnerabilities: scan to detect potential issues in the code or dependencies.
-
Build and Test: responsible for running tests, and building Docker images (if applicable).
Implementation of Continuous Integration:
- Create a Build Project and Configure the
buildspec.yml
file . The AWS CodeBuild YAML configuration, the pipeline is divided into multiple phases.
Parameter Store:
Install Phase:
- In this Phase the Build Environment is Setup where the required version of tools or languages are specified.
Pre-Build Phase:
- In this phase Install the project dependencies that are specified in requirements.txt file.
Build Phase:
- The build phase is where the main build steps occur, such as logging into the Docker registry, building the Docker image, and pushing it to the registry.
Post-Build Phase:
- The post-build phase runs after the build process has finished. It typically handles notifications or cleanup tasks.
With this the CodeBuild build, tests and deploy the Application securely on to the Docker Registry. These Phases Ensure that the Code Build is Build successfully.
Assign an IAM role to the Code Build and give necessary Permissions to access the the SSM parameter Store.
Now Click on the Start Build and Check if it is succeeded.
Code Pipeline:
AWS CodePipeline is a fully managed continuous integration and continuous delivery (CI/CD) service that automates the process of building, testing, and deploying your application. It allows you to set up a workflow that automatically executes each step of the release process whenever there are changes to your code. This helps to speed up releases, ensure quality, and reduce manual intervention.
Implementation of Code Pipeline:
- Create a pipeline with a service Role assigned to it.
-
Link to the pipeline to the GitHub and Configure the Repository name and Default branch. I have Specified the Trigger as No Filter (it starts the pipeline on any action performed)
-
Configure the build stage in the pipeline skip the deployment stage for now and create the pipeline.
-
Now the checkout stage and build stage is Successful.
-
The pipeline will Get triggered with the slight changes in the code stored in the GitHub Repository.
With this the Continuous Integration Part is Completed. Letโs move on the Continuous Deployment.
Continuous Deployment:
CD extends the concept of Continuous Integration (CI) by automating the steps required to release software into production. It ensures that every change made in the codebase is automatically built, tested, and prepared for deployment.
Implementation of Continuous Deployment:
- Create a Application in AWS Code Deploy. The application is a container for the resources that Code Deploy will manage during the deployment process. It Contains the Deployment Groups, Deployments etc..
Deployment Groups: A deployment group is used to define the environment where the application will be deployed.
Creating EC2 Instance:
Creating IAM roles:
-
Create a Deployment Group and assign an IAM role to the Deployment Group. The IAM role should have the permission to access the EC2 instances.
-
The Role should be attached to the EC2 instance that has Code Deploy Agent Installed.
Configure the Deployment Group:
Configure Deployment Settings:
-
Choose the deployment settings that best fit your needs, such as deployment configuration .
-
Click on “Create deployment group” to save.
Configure Deployment:
-
Now Click on Create Deployment.
-
Select the deployment Group.
-
The AppSpec file defines the deployment steps. Make sure that the file is present in the root of the directory.
-
Click “Create deployment” to initiate the deployment.
-
After the Deployment is Succeeded, Navigate to code pipeline and create the deployment stage.
Creating deployment Stage:
-
Navigate to CodePipeline and click on Edit to modify the Pipeline.
-
Click on Add Stage and provide the Action name , Action provider and the name of the Deployment that was created and Click on Create.
Trigger the Pipeline:
-
After Creating the Deployment Stage, Trigger the Pipeline by Committing Changes to the Code in the GitHub Repository.
-
Ensure that the deployment stage runs successfully and that the application is deployed to the specified EC2 instances.
By following these steps, you will have successfully configured a deployment group and automated deployments using AWS CodeDeploy, while linking it with AWS CodePipeline. This setup ensures that your Flask application is automatically built, tested, and seamlessly deployed to your EC2 instances whenever new code is pushed to the GitHub repository. This fully automated CI/CD process not only streamlines your application delivery but also reduces manual intervention, enhances code quality, and accelerates the development lifecycle, enabling faster and more reliable releases.
[fluentform id="8"]