Project: Implementing CI/CD Pipeline on AWS (Complete cicd)

Project: Implementing CI/CD Pipeline on AWS (Complete cicd)

Requirements:

  1. AWS Account: You will need access to an AWS account with appropriate permissions to create and manage AWS resources.

  2. Sample Web Application: A simple web application (e.g., a basic HTML/JavaScript application) that can be deployed on AWS.

  3. AWS Services: The CI/CD pipeline will leverage the following AWS services:

    • AWS CodeCommit: For hosting the source code repository.

    • AWS CodeBuild: To build and package the application.

    • AWS CodeDeploy: To automate deployment to the target environment.

    • AWS CodePipeline: To orchestrate the CI/CD process.

Steps to Implement the CI/CD Pipeline:

Step 1: Set up the Code Repository

  1. Create a new AWS CodeCommit repository to host your source code.

The repository is created in Code commit as a "cicd-repo"

  1. Clone the repository to your local development environment.

    before that, you have to install git software on your laptop and go to IAM of aws select an existing or create a new user, go to a security credential generate the "HTTPs git credential." copy and save it in your notepad for further purpose.

In security credentials scroll down.

  1. Add your sample web application code to the repository.

    a. Now set up the CLI user and generate the secret key and access key, for accessing the aws environment through cli. Now, clone the repository "git clone <url>"

    Install aws cli on your local machine.

    Clone the code commit repository in your machine.

    You will get one pop-up related to git and pass the HTTPs git credentials below the pop-up

    Now add a few file for your application inside this empty repository.

    The above following files you have to add in "CICD-REPO" code commit repo. Now these files add to code-commit repo using these commands. git add . git commit -m "this code for cicd" , git push --all

    Now check AWS code commit console whether these filess updated or not.

    our application files are updated in the code commit repo

Step 2: Configure AWS CodeBuild

Define a build specification file (e.g., buildspec.yml) in the root of your repository. This file specifies the build steps and dependencies.

Configure a CodeBuild project that references the build specification file and defines the build environment. Set up CodeBuild to trigger a build whenever changes are pushed to the CodeCommit repository.

Now, the rest of the things leave as it is, and click on “Create Build Project”

In Source section, Select

Source provider: AWS CodeCommit.

Repository: demo-app

Reference Type: Branch

Branch: Main

In the Environment section, Select

Environment Image: Managed Image

Operating System: Ubuntu

Runtime: Standard

Image: <latest one>

For this, a service role will be required and it will be created automatically when we select the “New Service Role”. In the BuildSpec section, click on “Use a build spec file”.

Now, click on “Start Build”.

Here, you can see the “Phase Details”.

We can give “Artifacts upload location” if we want to build the code on some specific location.

Goto “Edit” button and click on “Artifacts”

For this, you have to create a bucket and enable versioning.

Type: S3 Bucket. (Create a new S3 bucket if you haven’t created it)

Bucket Name: <Your created bucket>

Name: <Your folder name in S3 bucket> (Go and create one)

The bucket was created.

Click on “Update Artifact”. Next time, whenever you start a build, your files will go there. Let’s give it a try by building it.

Step 3: Configure AWS CodeDeploy

  1. Define an application in CodeDeploy that represents your web application.

    Open the AWS Management Console and navigate to the CodeDeploy service.

    Click on "Create application" to start the application creation process.

    Select the compute platform that your web application runs on (EC2, Lambda, or ECS). Choose the appropriate option based on your deployment target.

    Here I am using EC2 instances, choose the deployment group that represents the set of instances where your application will be deployed. If you haven't created a deployment group yet, you can create one during the application creation process.

    For creating a deployment group, you have to create one ec2 as a test environment purpose and select in the deployment group.

    Here I created an EC2 instance of amazon Linux (t2.micro).

    Now go to the IAM and create 2 roles one for codedeploy purpose and another for connection between codedeploy and ec2.

    1st role was created and now creates the second role for ec2 and code deploy.

    Now go to Ec2 instance and attach the code deploy role in "testing instance".

    And now give the tags name for identification of the deployment group as Key = "Environment" and Value = "Testing".

    Now open your ec2 instance and install "code deploy agent" by using the following commands.

     # Installing the CodeDeploy agent on EC2
     ```
     sudo yum update -y
     sudo yum install -y ruby wget
     wget https://aws-codedeploy-eu-west-1.s3.eu-west-1.amazonaws.com/latest/install
     chmod +x ./install
     sudo ./install auto
     sudo service codedeploy-agent status
     ```
    

  2. Configure deployment settings, such as deployment type (in-place or blue/green) and deployment configuration. Select In-place.

    In the place of environment configuration, you should give ec2 and given tags names of testing ec2

    The remaining thing leave it as a default and create deployment group.

    For deployment of applications on the internet (www), you have to create another S3 bucket for revision of the application.

    For this, you have to set aws cli in your machine. in starting of this blog i already setup aws cli.

     # create a bucket and enable versioning
     ```
     aws s3 mb s3://cicd-explorewithashok --region ap-south-1 --profile cicd
     aws s3api put-bucket-versioning --bucket cicd-explorewithashok --versioning-configuration Status=Enabled --region ap-south-1 --profile cicd
     ```
    
     # deploy the files into S3
     ```
     aws deploy push --application-name cicd-deploy --s3-location s3://cicd-explorewithashok/codedeploy-ashok/app.zip --ignore-hidden-files --region ap-south-1 --profile cicd
     ```
    

Now create a deployment.

Here you select the revision type as s3 for this u have to run above s3 aws cli commands.

And create a deployment.

Finally, our Test Ec2 instance is attached to the code deployment now go to the test ec2 instance copy the public IP address, and paste it into the browser you will get your application output.

Step 4: Create the CI/CD Pipeline with AWS CodePipeline

  1. Create a new AWS CodePipeline pipeline.

  2. Configure the pipeline's source stage to use the CodeCommit repository.

  3. Add a build stage that references the CodeBuild project created earlier.

  4. Add a deploy stage that references the CodeDeploy application and deployment group. Review and create a pipeline

    Congratulations guys We build successfully CICD on AWS.

    See, here I deploy only as Dev/Test environment. if you want to do production deployment the same as how we created testing deployment same like you have to create a production environment but don't forget to install a code-deploy agent inside production EC2.

    This is output of our application.

Project code Githublink [https://github.com/Ashoksana/explorewithashok.git]

Hope you like my blog...!

if u like the content follow me on LinkedIn: https://www.linkedin.com/in/ashok-sana

Follow my Whatsapp & telegram community: https://chat.whatsapp.com/BzX1aruZIH645l29LxvgO3

https://t.me/ExplorewithAshok

Happy learning......!

Did you find this article valuable?

Support Ashoksana by becoming a sponsor. Any amount is appreciated!