AWS CodePipeline (CI/CD Pipeline)

Creating a CICD pipeline with AWS CodePipeline

Demonstration 3 Create 4 Stage Pipeline

In this walkthrough, you will build a complete AWS CodePipeline CI/CD workflow with four stages—Source, Build, Test, and Deploy—using AWS CodeCommit, CodeBuild, CodeDeploy, and EC2. Automating these steps ensures consistent, repeatable deployments and faster delivery cycles.

Pipeline Overview

StageAWS ServicePurpose
SourceCodeCommitStore application code and scripts
BuildCodeBuildInstall dependencies and compile assets
TestCodeBuildRun automated tests
DeployCodeDeployDeploy artifacts to EC2 instances

1. Source Stage with CodeCommit

  1. Open the AWS CodeCommit console and create a new repository named kodekloudcpdemo.

The image shows the AWS CodeCommit console with an empty repositories list. The interface includes options to create, clone, and manage repositories.

  1. Enter Repository name as kodekloudcpdemo and click Create.

The image shows the AWS CodeCommit interface for creating a new repository, with fields for repository name, description, and optional settings.

  1. Clone the repo locally:
git clone https://git-codecommit.us-east-2.amazonaws.com/v1/repos/KodeKloudCPDemo
  1. Back in the console, click Upload file to add your application files (after_install, application_start, appspec.yml, etc.). For each file, provide an author name and email, then commit.

The image shows a file explorer window open on a computer, displaying a list of files in a directory. In the background, there is a web page related to AWS CodeCommit with fields for committing changes.

  1. After each commit, you’ll see a success notification:

The image shows an AWS CodeCommit interface with a repository named "KodeKloudCPDemo" and a file named "after_install" committed to the main branch. A notification indicates a successful commit.

  1. When all nine files are uploaded, your repo should list them as shown:

The image shows an AWS CodeCommit repository interface named "KodeKloudCPDemo" with a list of files such as `after_install`, `application_start`, and `appspec.yml`.


2. Build Stage with CodeBuild

  1. Navigate to the AWS CodeBuild console and click Create build project.

The image shows the AWS CodeBuild interface with no build projects listed. There is an option to create a new build project.

  1. Set Project name to KodeKloudCPdemo.

The image shows the AWS CodeBuild interface for creating a new build project, with fields for project configuration such as project name and description.

  1. Under Source, select AWS CodeCommit, choose kodekloudcpdemo, and branch main.

The image shows an AWS CodeBuild configuration screen where the source provider is set to AWS CodeCommit, with a repository named "KodeKloudCPDemo" and the branch "main" selected.

  1. In Environment, pick:

    • Operating system: Linux
    • Runtime: Standard
    • Image: aws/codebuild/standard:2.0
  2. Keep service role and log settings at their defaults, then click Create build project.

The image shows an AWS CodeBuild interface for configuring logs, with options for CloudWatch and S3 logs.

  1. Confirm your project is ready:

The image shows an AWS CodeBuild project interface for "KodeKloudCPDemo," displaying configuration details and options to start or manage builds. A green notification bar indicates the project was successfully created.

Warning

The default buildspec.yml uses Node.js 14, but your app requires Node.js 10. Update the runtime-versions accordingly to avoid build failures.

Adjusting the Buildspec

Open buildspec.yml in your CodeCommit repo. Change Node.js version from 14 to 10:

version: 0.2
phases:
  install:
    runtime-versions:
      nodejs: 10
    commands:
      - echo "Installing dependencies"
      - npm install
  build:
    commands:
      - echo "Building the application"
      - npm run build
artifacts:
  files:
    - '**/*'

Commit your changes with author metadata.


3. Prepare EC2 Instances for CodeDeploy

  1. In the EC2 console, click Launch Instance.
  2. Name the instance CodePipelineCPdemo and choose an Ubuntu AMI.
  3. Select or create a key pair.
  4. Open HTTP (port 80) and HTTPS (port 443) in the security group.
  5. Enable Auto-assign Public IP.
  6. Under Advanced Details, attach an IAM instance profile with CodeDeploy permissions.
  7. Click Launch Instance.

The image shows an AWS EC2 instance launch configuration screen, where a user is selecting an Amazon Machine Image (AMI) and configuring instance details like the server type and security group.

Review network settings and instance summary:

The image shows an AWS EC2 instance launch configuration screen, detailing network settings and a summary of the instance specifications.

Select the IAM role:

The image shows an AWS EC2 instance launch configuration page, detailing options like purchasing, domain join directory, IAM instance profile, and a summary of the instance settings.

Verify the instance is running before proceeding to CodeDeploy.

Note

Ensure the IAM instance profile has AmazonEC2RoleforAWSCodeDeploy permissions so the CodeDeploy agent can communicate with AWS.


4. Deploy Stage with CodeDeploy

  1. Go to the AWS CodeDeploy console and click Create application.
  2. Name it KodeKloudCPdemo and choose EC2/On-premises as the compute platform.

The image shows an AWS CodeDeploy application interface for "KodeKloudCPDemo," with options to create a deployment group and manage application details. The interface includes navigation for various developer tools and deployment settings.

  1. Create a deployment group:
    • Name: KodeKloudCPdemo
    • Service role: CodeDeployDefault (or your custom role)
    • Uncheck load balancer integration.
    • Environment: EC2/On-premises
    • Tag key/value matching your EC2 instance (Name=CodePipelineCPdemo).

The image shows an AWS CodeDeploy setup page where a deployment group name is being entered, along with options for selecting a service role and deployment type.

  1. Select the EC2 instance by tag:

The image shows an AWS CodeDeploy interface where a user is selecting Amazon EC2 instances for deployment, with a dropdown menu listing various instance options.

  1. (Optional) Install or verify the CodeDeploy agent via Systems Manager, then click Create deployment group.

The image shows an AWS CodeDeploy configuration screen with options for tagging and agent configuration using AWS Systems Manager. It includes settings for installing the CodeDeploy Agent.


5. Create the Four-Stage Pipeline

  1. Open the AWS CodePipeline console and click Create pipeline.
  2. Name it KodeKloudCPdemo and use the default service role.

The image shows an AWS CodePipeline setup screen where a user is configuring pipeline settings, including the pipeline name and service role options.

Source Stage

  • Provider: AWS CodeCommit
  • Repository: kodekloudcpdemo
  • Branch: main

The image shows an AWS CodePipeline interface where a user is adding a source stage, selecting AWS CodeCommit as the source provider, and specifying repository and branch details.

Build Stage

  • Provider: AWS CodeBuild
  • Project name: KodeKloudCPdemo

The image shows an AWS CodeBuild project interface with build project details and an option to start builds indicating the project was successfully created.

Deploy Stage

  • Provider: AWS CodeDeploy
  • Region: your AWS Region
  • Application: KodeKloudCPdemo
  • Deployment group: KodeKloudCPdemo

The image shows an AWS CodePipeline interface where a user is adding a deploy stage, selecting AWS CodeDeploy as the provider, and specifying the region, application name, and deployment group.

Click Create pipeline. The pipeline initializes and starts its first run:

The image shows an AWS CodePipeline interface with a successful pipeline creation message for "KodeKloudCPDemo." It displays the status of the source and build stages, with the source stage marked as succeeded.


6. Adding the Test Stage

Once the initial pipeline run succeeds, insert a Test stage between Build and Deploy:

  1. Click Edit in the pipeline view.
  2. Under the Build stage, click Add stage, name it Test, then Add stage.

The image shows an AWS CodePipeline interface with successful build and deploy stages. The pipeline execution details indicate recent success in both stages.

  1. Inside Test, click Add action group and configure:
    • Action name: TestAction
    • Provider: AWS CodeBuild
    • Input artifact: BuildArtifact
    • Project name: KodeKloudCPdemo

The image shows an AWS CodePipeline interface where a user is editing an action, selecting AWS CodeBuild as the action provider, and specifying input artifacts and environment variables.

  1. Save changes and click Release change. The pipeline will now run all four stages in sequence, ending with a green success status across Source, Build, Test, and Deploy.

Summary

You have successfully built an automated four-stage CI/CD pipeline on AWS:

  1. Source: CodeCommit
  2. Build: CodeBuild
  3. Test: CodeBuild
  4. Deploy: CodeDeploy

Use this template to accelerate your deployments and enforce consistent delivery practices.


Watch Video

Watch video content

Practice Lab

Practice lab

Previous
Demonstration 2 Create 2 Stage Pipeline