CI/CD pipeline on AWS

Table of contents

What is CodeCommit?

AWS CodeCommit is a fully managed source control service from Amazon Web Services. It allows you to host secure, scalable Git repositories in the cloud. Here’s a quick overview of its key features:

1. Version Control

CodeCommit uses Git, supporting distributed version control to help developers manage code changes.

2. Security

It offers encryption at rest and in transit. Integration with AWS Identity and Access Management (IAM) enables fine-grained access control.

3. Scalability

It can handle any size of repository and automatically scales to accommodate large projects or teams.

4. Integration

CodeCommit integrates with other AWS services like CodePipeline for CI/CD workflows, CloudWatch for monitoring, and AWS Lambda for triggering actions based on repository changes.

5. Collaboration

Developers can work together by pushing and pulling changes, creating branches, and submitting pull requests for code reviews.

6. High Availability

The service is designed for high durability and availability, ensuring your repositories are accessible when needed.

CodeCommit is often used in DevOps workflows to securely store and version application code while integrating with CI/CD processes. It’s ideal for teams or enterprises already using the AWS ecosystem.

Task-01 :

  • Set up a code repository on CodeCommit and clone it on your local.

  • You need to set up GitCredentials in your AWS IAM.

  • Use those credentials locally and then clone the repository from CodeCommit.


Step 1: Create a Repository in AWS CodeCommit

  1. Sign in to AWS Management Console:

    • Navigate to CodeCommit under the Developer Tools section.
  2. Create a Repository:

    • Click on "Create Repository".

    • Enter a name and optional description.

    • Click Create.


Step 2: Set Up Git Credentials in AWS IAM

  1. Open the IAM Console:

    • Go to the IAM dashboard.
  2. Select or Create a User:

    • If you already have a user, click on the user name.

    • If not, create a new user with programmatic access.

  3. Add Git Credentials:

    • On the user details page, go to the Security Credentials tab.

    • Scroll down to HTTPS Git credentials for AWS CodeCommit.

    • Click Generate credentials.

    • Download the credentials or note down the username and password.


Step 3: Configure Git with AWS Credentials Locally

  1. Open a Terminal/Command Prompt:

    • Run the following commands to configure your Git credentials:

        git config --global credential.helper store
      
    • When you first clone the repository, Git will prompt you to enter the credentials. They will be stored locally for future use.


Step 4: Clone the CodeCommit Repository Locally

  1. Go back to your CodeCommit repository page.

  2. Click on Clone URL (choose HTTPS).

  3. Run the following command in your terminal (replace your-repo-url with the actual URL):

     git clone https://git-codecommit.<region>.amazonaws.com/v1/repos/your-repo-name
    
  4. When prompted, enter the Git credentials you set up in Step 2.


Step 5: Verify the Clone

  • Once cloned, navigate into the directory using:

      cd your-repo-name
    
  • Run:

      git status
    

    This should show the working directory as clean, confirming the repository is cloned successfully.


Optional Step: Push Initial Code

  • If you want to push some initial code to the repository:

    1. Create a sample file in the cloned directory:

       echo "Hello, CodeCommit!" > sample.txt
      
    2. Add and commit the file:

       git add sample.txt
       git commit -m "Initial commit"
      
    3. Push the changes:

       git push origin main
      

Task-02 :

  • Add a new file from local and commit to your local branch

  • Push the local changes to the CodeCommit repository.


Step 1: Add a New File Locally

  1. Navigate to your cloned CodeCommit repository on your local system:

     cd your-repo-name
    
  2. Create a new file (e.g., new-file.txt) with some content:

     echo "This is a new file for Task-02" > new-file.txt
    

Step 2: Stage the New File

Stage the new file to prepare it for the commit:

git add new-file.txt

You can confirm the file is staged by running:

git status

It should display the new file under "Changes to be committed."


Step 3: Commit the Changes

Commit the changes with a message:

git commit -m "Added new-file.txt for Task-02"

Step 4: Push the Changes to CodeCommit

Push the changes to the CodeCommit repository:

git push origin main

If your repository uses a different branch name (e.g., master or development), replace main with the appropriate branch.


Step 5: Verify the Changes

  1. Go to the AWS CodeCommit repository in the AWS Management Console.

  2. Check that new-file.txt is now visible in the repository.

What is CodeBuild?

AWS CodeBuild is a fully managed continuous integration (CI) service provided by Amazon Web Services (AWS). It compiles source code, runs tests, and produces software packages that are ready for deployment. Here's a detailed overview:


Key Features of CodeBuild

  1. Build Automation
    Automates the build process, eliminating the need for on-premise build servers.

  2. Scalability
    Scales automatically meet the size and number of concurrent builds, ensuring fast execution even for large projects.

  3. No Server Management
    Since it’s fully managed, AWS handles all infrastructure and maintenance.

  4. Flexible Environment
    Supports multiple environments and programming languages such as Java, Python, Node.js, Ruby, Go, Docker, .NET Core, and more. You can also create custom-built environments.

  5. Integration with AWS Services
    Works well with other AWS services like CodeCommit, CodePipeline, S3, and CloudWatch, making it ideal for DevOps workflows.

  6. Security
    CodeBuild encrypts artifacts and environment variables and integrates with AWS Identity and Access Management (IAM) for access control.

  7. Parallel Builds
    You can run multiple builds concurrently, which speeds up your CI/CD pipeline and reduces development time.


How AWS CodeBuild Works

  1. Source
    The source code is pulled from supported repositories (e.g., CodeCommit, GitHub, GitHub Enterprise, Bitbucket, or S3).

  2. Build
    CodeBuild executes the build process according to the instructions in a buildspec.yml file (or inline build commands).

  3. Artifacts
    The output of the build process (e.g., binaries or Docker images) is uploaded to a specified storage location (e.g., S3).

  4. Logs
    Build logs are sent to Amazon CloudWatch, allowing developers to monitor and debug the build process in real time.


Use Cases

  • Continuous Integration: Automates code compilation and testing every time new code is committed.

  • Automated Testing: Run unit tests, integration tests, or performance tests on every code change.

  • Container Builds: Build and package Docker images for containerized applications.

  • Multi-Platform Builds: Create builds for different platforms or languages in the same project.


Example Workflow with CodeBuild

  1. A developer commits code to CodeCommit.

  2. CodePipeline triggers a build in CodeBuild.

  3. CodeBuild compiles the code, runs tests, and stores artifacts in S3.

  4. Successful builds are then deployed or moved to the next stage in the pipeline.

Task-03 :

  • Read about the Buildspec file for Codebuild.

  • create a simple index.html file in the CodeCommit Repository

  • you have to build the index.html using the NGINX server


Step 1: Understand the Buildspec File

The buildspec.yml the file is a configuration file used by AWS CodeBuild to define build steps. It contains:

  • Version: Specifies the build spec version.

  • Phases: Defines the build steps (install, pre-build, build, post-build).

  • Artifacts: Specifies files to store or upload as build outputs.


Step 2: Create an index.html in CodeCommit

  1. Navigate to Your CodeCommit Repository:

    • Clone the repository or navigate to it in the AWS Management Console.
  2. Create a Simple index.html: Add an index.html file with basic content:

     <!DOCTYPE html>
     <html lang="en">
     <head>
         <meta charset="UTF-8">
         <meta name="viewport" content="width=device-width, initial-scale=1.0">
         <title>Hello, Nginx!</title>
     </head>
     <body>
         <h1>Hello from AWS CodeBuild with Nginx!</h1>
     </body>
     </html>
    

    Commit this file to your repository.


Step 3: Create the buildspec.yml File

Add a buildspec.yml file in the root of your repository:

version: 0.2

phases:
  install:
    commands:
      - echo "Installing Nginx..."
      - yum install -y nginx

  pre_build:
    commands:
      - echo "Starting Nginx service"
      - service nginx start

  build:
    commands:
      - echo "Copying index.html to Nginx directory"
      - cp index.html /usr/share/nginx/html/

  post_build:
    commands:
      - echo "Build complete. Index.html is now being served by Nginx."

Step 4: Configure a CodeBuild Project in AWS

  1. Open the AWS Management Console and navigate to CodeBuild.

  2. Create a New Build Project:

    • Source: Select your CodeCommit repository.

    • Build spec: Ensure the option is set to "Use the buildspec.yml file in the source code".

    • Environment:

      • Choose a managed Ubuntu environment.

      • Runtime: Use a standard runtime (Amazon Linux 2).

    • Service Role: Create or select an appropriate IAM role with CodeBuild permissions.

  3. Start the Build:

    • Run the build and monitor the logs.

Step 5: Verify Nginx is Serving index.html

  • During the build process, CodeBuild should log that Nginx has started and index.html has been copied to the Nginx web directory (/usr/share/nginx/html/).

  • This confirms that index.html was successfully built and served by Nginx.


Task-04 :

  • Add the build spec.yaml file to CodeCommit Repository and complete the build process.


Step 1: Add buildspec.yml to CodeCommit Repository

  1. Navigate to Your Local Repository (if cloned locally):

     cd your-repo-name
    
  2. Create the buildspec.yml File in the root directory with the following content:

     version: 0.2
    
     phases:
       install:
         commands:
           - echo "Installing Nginx..."
           - yum install -y nginx
    
       pre_build:
         commands:
           - echo "Starting Nginx service"
           - service nginx start
    
       build:
         commands:
           - echo "Copying index.html to Nginx directory"
           - cp index.html /usr/share/nginx/html/
    
       post_build:
         commands:
           - echo "Build complete. Index.html is now being served by Nginx."
    
  3. Stage and Commit the Changes:

     git add buildspec.yml
     git commit -m "Added buildspec.yml to automate the Nginx build process"
     git push origin main
    

Step 2: Start a Build in AWS CodeBuild

  1. Navigate to the AWS Management Console:

    • Go to CodeBuild and select your build project.
  2. Start a New Build:

    • Click on Start Build for your existing CodeBuild project.

    • Ensure the project is configured to pull the latest changes from CodeCommit.

    • Click Start Build to begin.


Step 3: Monitor the Build Process

  1. Check the Build Logs:

    • You can view detailed logs in the Build Logs section of the CodeBuild project dashboard.

    • Ensure that each phase (install, pre_build, build, post_build) runs without errors.

Expected log output:

  • Installation of Nginx

  • Starting Nginx service

  • Copying index.html to the Nginx directory

  • Confirmation message: "Build complete. Index.html is now being served by Nginx."


Step 4: Verify Build Completion

  • The build should be completed successfully without errors.

  • If you set up artifact storage (e.g., S3) or additional post-build actions, check that they are also executed.


What is CodeDeploy?

AWS CodeDeploy is a fully managed deployment service that automates software deployment to a variety of computing services such as Amazon EC2 instances, on-premises servers, AWS Lambda, and Amazon ECS. It is designed to facilitate reliable, automated deployments for various application types.


Key Features of CodeDeploy

  1. Automated Deployments

    • Automates the deployment process to reduce manual errors and ensure consistent deployments across different environments.
  2. Blue/Green and In-Place Deployments

    • In-Place Deployment: Updates the existing instances in the deployment group.

    • Blue/Green Deployment: Creates a new environment (green), tests it, and switches traffic from the old environment (blue) to the new one once validated.

  3. Wide Compute Support

    • Supports Amazon EC2, ECS, on-premises servers, and AWS Lambda functions.
  4. Application Lifecycle Hooks

    • Supports pre- and post-deployment hooks to run custom scripts at different stages of the deployment process (e.g., BeforeInstall, AfterInstall).
  5. Monitoring and Rollbacks

    • Monitors the deployment status in real-time and can automatically roll back in case of deployment failure.
  6. Integration with Other AWS Services

    • Works with CodePipeline for end-to-end CI/CD pipelines and CloudWatch for monitoring and alarms.

How AWS CodeDeploy Works

  1. Application Setup

    • An application is defined in CodeDeploy, and a deployment group is created (a set of EC2 instances, ECS services, or Lambda functions).
  2. AppSpec File

    • The appspec.yml or appspec.json the file defines the deployment instructions, including where to copy files and what commands to run at various lifecycle hooks.
  3. Deployment

    • CodeDeploy pulls the specified code or artifact from an S3 bucket, GitHub, or CodeCommit and deploys it according to the instructions in the appspec file.
  4. Post-Deployment Monitoring

    • The deployment is monitored, and CodeDeploy can automatically roll back if errors are detected or health checks fail.

Deployment Lifecycle Hooks

  • BeforeInstall: Runs before application files are copied to the instance.

  • AfterInstall: Runs after the files have been copied.

  • ApplicationStart: Runs after the application has started.

  • ApplicationStop: Runs before the application is stopped (during redeployments).

  • ValidateService: Runs to validate the deployed application’s health.


Use Cases

  • Web Application Deployment: Automating the rollout of new versions of a web app to EC2 instances or on-premises servers.

  • Lambda Deployment: Automatically deploying new versions of AWS Lambda functions in response to new code pushes.

  • Microservices Deployment: Managing complex deployments of containerized services running on Amazon ECS.

Task-05 :

  • Read about Appspec.yaml file for CodeDeploy.

  • Deploy index.html file on EC2 machine using nginx

  • you have to set a CodeDeploy agent to deploy code on EC2.


Step 1: Understand appspec.yml

The appspec.yml the file is used by AWS CodeDeploy to define how to manage files and run commands during a deployment. Key sections:

  1. version: The version of the AppSpec file (usually 0.0 for EC2).

  2. os: The operating system type (linux or windows).

  3. files: Specifies the source and destination of files to be deployed.

  4. hooks: Defines lifecycle event hooks (e.g., BeforeInstall, AfterInstall, ApplicationStart).


Step 2: Prepare Your index.html File

  1. In your CodeCommit repository, create a simple index.html file if it doesn’t exist:

     <!DOCTYPE html>
     <html>
     <head>
         <title>Hello from CodeDeploy!</title>
     </head>
     <body>
         <h1>Deployed using AWS CodeDeploy</h1>
     </body>
     </html>
    

Step 3: Create the appspec.yml File

In the root of your repository, create the appspec.yml file with the following content:

version: 0.0
os: linux
files:
  - source: /
    destination: /usr/share/nginx/html/

hooks:
  BeforeInstall:
    - location: scripts/install_nginx.sh
      timeout: 300
  ApplicationStart:
    - location: scripts/start_nginx.sh
      timeout: 300

Step 4: Create Supporting Shell Scripts

Create a scripts/ directory in your repository with the following two scripts:

  1. install_nginx.sh: Installs and starts Nginx.

     #!/bin/bash
     yum update -y
     yum install -y nginx
    
  2. start_nginx.sh: Ensures Nginx is started.

     #!/bin/bash
     service nginx start
    

Make sure both scripts are executable:

chmod +x scripts/install_nginx.sh scripts/start_nginx.sh

Step 5: Set Up an EC2 Instance for CodeDeploy

  1. Launch an EC2 Instance:

    • Choose Amazon Linux 2 or Ubuntu as the AMI.

    • Ensure that the security group allows inbound traffic on port 80 (for Nginx).

    • Assign an appropriate IAM role to the instance with the AWSCodeDeployRole policy.

  2. Install the CodeDeploy Agent (on the EC2 instance):

    Connect to the EC2 instance via SSH and run:

     sudo yum update -y
     sudo yum install -y ruby
     sudo yum install -y wget
    
     wget https://aws-codedeploy-<region>-latest.s3.amazonaws.com/latest/install
     sudo chmod +x ./install
     sudo ./install auto
    

    Start and verify the agent:

     sudo service codedeploy-agent start
     sudo service codedeploy-agent status
    

Step 6: Set Up a CodeDeploy Application

  1. Go to the AWS CodeDeploy Console:

    • Create a new application.

    • Choose the compute platform as "EC2/On-premises".

  2. Create a Deployment Group:

    • Name the deployment group.

    • Attach it to the EC2 instance (using tags or manually selecting the instance).

    • Ensure the service role has AWSCodeDeployRole permissions.


Step 7: Create a Deployment

  1. In CodeDeploy, click "Create Deployment".

  2. Select Your Application and Deployment Group.

  3. Choose the Revision Source:

    • If using CodeCommit, select the repository and branch.
  4. Click Deploy.


Step 8: Verify the Deployment

  1. Once the deployment is complete, access the EC2 instance’s public IP or domain name in a browser:

     http://<ec2-public-ip>
    

    You should see the index.html page with the message "Deployed using AWS CodeDeploy".


Task-06 :

  • Add app spec.yaml file to CodeCommit Repository and complete the deployment process.


Step 1: Add the appspec.yml to the CodeCommit Repository

  1. Navigate to Your Local Repository:

     cd your-repo-name
    
  2. Ensure the appspec.yml the file is present at the root of your repository. The content should be similar to this:

     version: 0.0
     os: linux
     files:
       - source: /
         destination: /usr/share/nginx/html/
    
     hooks:
       BeforeInstall:
         - location: scripts/install_nginx.sh
           timeout: 300
       ApplicationStart:
         - location: scripts/start_nginx.sh
           timeout: 300
    
  3. Stage, Commit, and Push the Changes:

     git add appspec.yml
     git commit -m "Added appspec.yml for CodeDeploy"
     git push origin main
    

Step 2: Verify Files in CodeCommit

  • Go to the AWS CodeCommit Console.

  • Confirm that the appspec.yml and related scripts (install_nginx.sh and start_nginx.sh) are present in the repository.


Step 3: Start the Deployment Process

  1. Go to the AWS CodeDeploy Console.

  2. Select Your Application and Deployment Group that were previously created.

  3. Create a New Deployment:

    • Application Name: Select the appropriate application.

    • Deployment Group: Choose the group linked to your EC2 instance.

    • Revision Type: Select "CodeCommit".

    • Repository: Choose the repository where you added the appspec.yml.

    • Branch: Choose the branch where you committed the file.

    • Click Deploy.


Step 4: Monitor Deployment Progress

  1. Check the Deployment Status:

    • In the CodeDeploy console, monitor the status of each lifecycle event (e.g., BeforeInstall, ApplicationStart).

    • Ensure that each event runs successfully.

  2. View Logs:

    • If any phase fails, view the logs for detailed error messages and troubleshoot accordingly.

Step 5: Verify the Deployment

  • Once the deployment is complete, open your browser and navigate to:

      http://<your-ec2-instance-public-ip>
    
  • You should see the index.html page, confirming a successful deployment.


Tips for Debugging (If Deployment Fails):

  1. Check Logs:

    • View logs located in /var/log/aws/codedeploy-agent on your EC2 instance.
  2. Ensure EC2 Security Group allows HTTP traffic (port 80).

  3. Confirm CodeDeploy Agent is running on the EC2 instance:

     sudo service codedeploy-agent status
    

What is CodePipeline?

AWS CodePipeline is a fully managed continuous integration and continuous delivery (CI/CD) service that automates the build, test, and deployment phases of your application. It enables you to quickly and reliably release updates with minimal manual intervention.


Key Features of CodePipeline

  1. Automation: Automatically builds, tests, and deploys code whenever a change is detected in the source repository (e.g., CodeCommit, GitHub, S3).

  2. Integrations:

    • Native integration with AWS services like CodeCommit, CodeBuild, CodeDeploy, and Lambda.

    • Third-party tools (e.g., Jenkins, GitHub, Bitbucket).

  3. Pipeline Stages: CodePipeline consists of multiple stages, each representing a step in the release process (e.g., Source, Build, Deploy, Approval).

  4. Parallel Execution: Can run multiple stages concurrently to speed up the delivery process.

  5. Manual Approvals: Allows you to add approval steps before proceeding to the next stage.

  6. Artifact Management: Manages and passes build artifacts between different stages in the pipeline.

  7. Version Control Integration: Works with popular version control systems for source code.


How CodePipeline Works

  1. Source Stage:

    • Monitors change in a source repository (CodeCommit, GitHub, S3).

    • When changes are detected, they trigger the pipeline.

  2. Build Stage:

    • Uses services like CodeBuild or third-party tools to compile the code, run tests, and package the application.
  3. Test (Optional):

    • Runs additional integration or unit tests to verify the application.
  4. Deploy Stage:

    • Deploys the built and tested code to environments like EC2, Lambda, ECS, or S3 using CodeDeploy or other deployment services.
  5. Approval Stage (Optional):

    • Allows for manual approvals before moving to the next stage, ensuring compliance and oversight.

Example Use Case

For a web application:

  1. Source: Code changes are pushed to AWS CodeCommit.

  2. Build: CodeBuild compiles the application and runs tests.

  3. Deploy: CodeDeploy deploys the application to EC2 instances.

  4. Approval: A manual approval stage before production deployment.


Benefits of CodePipeline

  • Faster Releases: Automates the entire release process.

  • Reliability: Ensures consistent deployment processes across environments.

  • Scalability: Easily adapts to larger applications and teams.

  • Flexibility: Supports multiple third-party tools and services.

  • Reduced Manual Effort: Lessens the chance of human errors during the deployment process.

Task-07 :

  • Create a Deployment group of Ec2 Instance.

  • Create a CodePipeline that gets the code from CodeCommit, Builds the code using CodeBuild, and deploys it to a Deployment Group.

Step 1: Create a Deployment Group for EC2 Instances

  • Go to AWS CodeDeploy Console:

    • Navigate to CodeDeploy in the AWS Management Console.
  • Create a New Application:

    • Application Name: Choose a meaningful name (e.g., "MyWebApp").

    • Compute Platform: Select EC2/On-Premises.

  • Create a Deployment Group:

    • Deployment Group Name: Provide a name (e.g., "MyWebApp-Deployment-Group").

    • Service Role: Select the IAM role with the AWSCodeDeployRole policy.

    • Environment Configuration:

      • Choose Amazon EC2 Instances.

      • Tag the EC2 instances with a key-value pair (e.g., Environment=Production).

    • Deployment Settings: Leave the defaults or choose your preferred settings.

    • Click Create Deployment Group.


Step 2: Create the CodePipeline

  1. Go to AWS CodePipeline Console:

    • Navigate to CodePipeline and click Create Pipeline.
  2. Pipeline Settings:

    • Pipeline Name: Choose a meaningful name (e.g., "MyWebApp-Pipeline").

    • Service Role: Create or select an existing role with permissions for CodePipeline, CodeCommit, CodeBuild, and CodeDeploy.


Step 3: Add Source Stage (CodeCommit)

  1. Source Provider: Select AWS CodeCommit.

  2. Repository Name: Choose the repository where your code is stored.

  3. Branch: Select the branch to track (e.g., "main").

  4. Click Next.


Step 4: Add Build Stage (CodeBuild)

  1. Build Provider: Select AWS CodeBuild.

  2. Project: Create a new CodeBuild project:

    • Project Name: Provide a name (e.g., "MyWebApp-Build").

    • Environment: Use Managed Image and select the appropriate OS and runtime (e.g., Amazon Linux 2, Standard Image).

    • Build spec: Ensure you have an buildspec.yml in the root of your repository or specify it manually in the project.

    • Click Create Build Project.

  3. Back in the CodePipeline console, select the newly created build project.


Step 5: Add Deploy Stage (CodeDeploy)

  1. Deploy Provider: Select AWS CodeDeploy.

  2. Application Name: Choose the application you created earlier.

  3. Deployment Group: Select the deployment group you created.

  4. Click Next.


Step 6: Review and Create a Pipeline

  1. Review all stages and settings.

  2. Click Create Pipeline.

The pipeline will automatically run, triggering the build and deployment process when changes are pushed to the CodeCommit repository.


Step 7: Verify the Deployment

  1. Check CodePipeline:

    • Monitor the progress of each stage (Source, Build, Deploy).
  2. Check CodeDeploy:

    • Ensure that the deployment is successful.
  3. Access the Application:

    • Open the EC2 instance’s public IP in a browser to verify the deployed index.html.