https://github.com/kavithma-thushal/ci-cd-pipelines
https://github.com/kavithma-thushal/ci-cd-pipelines
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/kavithma-thushal/ci-cd-pipelines
- Owner: Kavithma-Thushal
- Created: 2025-03-02T03:20:10.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2025-03-02T03:28:40.000Z (about 2 months ago)
- Last Synced: 2025-03-02T04:23:24.125Z (about 2 months ago)
- Language: Python
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
CI/CD Pipeline using ECR, GitHub, ECS, CodePipeline, and Load Balancer
# Step 1: Create Required Repositories in ECR
Install AWS CLI: Download and install the AWS CLI from .

Configure AWS CLI: Create an IAM user or get the access key and secret key for the root user. Run the following command and enter your AWS credentials:
aws configureCreate ECR Repository: Navigate to the AWS Management Console. Open the Amazon ECR console. Create a new repository to store your Docker images.

Authenticate Docker to ECR: Get the login command from the ECR console and run it in your terminal:
aws configure

aws ecr get-login-password --region region | docker login --username AWS --password-stdin aws_account_id.dkr.ecr.region.amazonaws.com

# Step 2: Build and Push Docker Image
Build Docker Image: Build your Docker image using the Dockerfile.
docker build -t your-image-name .
Tag Docker Image: Tag the Docker image with your ECR repository URI.
docker tag your-image-name:latest aws_account_id.dkr.ecr.region.amazonaws.com/your-repo-name:latest
Push Docker Image to ECR: Push the tagged image to the ECR repository.
docker push aws_account_id.dkr.ecr.region.amazonaws.com/your-repo-name:latest# Step 3: Set Up GitHub Repository
Initialize GitHub Repository: Initialize a Git repository and add your remote GitHub repository.
git init
git remote add originCreate Branches: Create the following branches:
git checkout -b develop
git checkout -b pre-develop
git checkout -b release
Enable Branch Protection Rules: Go to your GitHub repository settings and set up branch protection rules.

# Step 4: Configure AWS CodePipelineCreate a Pipeline: Open the AWS CodePipeline console and create a new pipeline. Provide a pipeline name and select the service role.

Add Source Stage: Select GitHub (Version 2) as the source provider and connect your GitHub repository. Select the repository and branch (e.g., release).

Click connect to GitHub(Version 2) if you don’t have a project click Connect to GitHub

Provide a connection name and click on Connect to GitHub.

Provide the required Authorization

Select the required repositories and click install

Approve it your GitHub ID will be displayed click connect
When you complete the above task your github id will be displayed in the pipeline.

Select the Repository and Branch

Update the trigger

Output artifact format
Select code pipeline default

Add Build Stage: Select AWS CodeBuild as the build provider. Create a new build project and configure it to use the buildspec.yaml file from your repository.
select code build.
Create a new project by clicking the create project button.

Provide a project name

Provide a role name

Select buildspec file and click continue to code pipeline

Select the project and click next

Till we create ECS skip the deployment stage.

Click on Skip

The build will fail due to newly created IAM user doesn’t have the required permissions.

Click on view details to view.

To find the IAM role go to Build details and click on Service role

Click on add permissions

Add the EC2InstanceProfileForImageBuilderECRContainerBuilds. Add the required permission to the user

Go back to the release pipeline and click release change

Click on Release

The build will succeed.

# Step 5: Set Up CodeBuild
Create Build Project: Provide a project name and select the build environment. Specify the buildspec file:
\`\`\`yaml
version: 0.2
phases:
install:
runtime-versions:
docker: 18
pre_build:
commands:
\- $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)
build:
commands:
\- docker build -t your-image-name .
\- docker tag your-image-name:latest aws_account_id.dkr.ecr.region.amazonaws.com/your-repo-name:latest
post_build:
commands:
\- docker push aws_account_id.dkr.ecr.region.amazonaws.com/your-repo-name:latest
# Step 6: Create ECS Cluster and Task Definition
Create ECS Cluster: Open the ECS console and create a new cluster with Fargate (serverless) launch type.

Select Fargate (serverless) application.

Click Create button

Create Task Definition: Define the task definition with container details, environment variables, and resource requirements.

Container details

Add environment file and variables according to the requirement

Create Service

Fill the family and Task Definition revision version

Update the networking requirements.

# Step 7: Configure Load Balancer
Create Load Balancer: Open the EC2 console and create a new load balancer. Configure the load balancer to direct traffic to the ECS service.
Select the correct VPC and subnets and Security groups.

Click on create button

Go to Configuration and networking
Get the DNS from the network configuration

# Step 8: Deploy to ECS
Create ECS Service: Create a new service in the ECS console using the task definition. Configure the service to use the load balancer.
Update CodePipeline: Add a deploy stage to your pipeline, specifying ECS as the deployment provider. Select the ECS cluster and service created earlier.
For the deployment go back to pipelines and update the pipeline.
Image definitions file should be taken from the buildspec.yaml from the codebase

Click next

# Advantages of CI/CD Pipeline
- Automation: Automates the entire build, test, and deployment process, reducing manual intervention and errors.
- Consistency: Ensures consistent deployment processes across environments.
- Speed: Accelerates the release cycle, enabling quicker updates and faster time-to-market.
- Scalability: Easily scales to handle increased load and larger codebases.# Further Improvements
- Testing: Integrate automated testing (unit, integration, and end-to-end tests) into the pipeline.
- Monitoring: Add monitoring and logging to track the performance and health of applications.
- Security: Implement security checks and vulnerability scanning in the pipeline.