Hands-On Lab: Implementing Multi-Stage CI/CD Pipelines with AWS CodePipeline
Implement CI/CD pipelines
Hands-On Lab: Implementing Multi-Stage CI/CD Pipelines with AWS CodePipeline
In this lab, you will architect and deploy a fully automated Continuous Integration and Continuous Delivery (CI/CD) pipeline. Following the AWS Certified DevOps Engineer - Professional curriculum, you will integrate source control, automated builds, and a manual approval gate before a simulated deployment.
[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges. AWS CodePipeline charges $1.00 per active pipeline per month after the first 30 days.
Prerequisites
- AWS Account: An active AWS account with Administrator access.
- AWS CLI: Installed and configured with
aws configureusing your credentials. - Region: This lab uses
<YOUR_REGION>(e.g.,us-east-1). - IAM Knowledge: Basic understanding of IAM roles and service-linked roles.
Learning Objectives
- Configure an Amazon S3 source provider for versioned artifacts.
- Orchestrate a build process using AWS CodeBuild.
- Implement a Manual Approval stage to enforce governance.
- Manage and inspect pipeline Artifacts stored in S3.
Architecture Overview
The following diagram illustrates the flow of code from source to manual approval.
Step-by-Step Instructions
Step 1: Create the Source and Artifact Buckets
CodePipeline requires versioned S3 buckets to detect changes and store intermediate artifacts.
# Create the source bucket
aws s3 mb s3://brainybee-lab-source-<YOUR_ACCOUNT_ID>
# Enable versioning (REQUIRED for CodePipeline S3 sources)
aws s3api put-bucket-versioning --bucket brainybee-lab-source-<YOUR_ACCOUNT_ID> --versioning-configuration Status=Enabled▶Console alternative
- Navigate to S3 > Create bucket.
- Name:
brainybee-lab-source-<YOUR_ACCOUNT_ID>. - Under Bucket Versioning, select Enable.
- Click Create bucket.
Step 2: Prepare the Source Artifact
You need a buildspec.yml file to tell CodeBuild how to handle your code.
- Create a file named
index.htmlwith simple text:<h1>Hello DevOps Pro!</h1>. - Create a file named
buildspec.ymlwith the following content:
version: 0.2
phases:
build:
commands:
- echo Build started on `date`
- cp index.html output.html
artifacts:
files:
- output.html- Zip these files and upload them to your source bucket:
zip source.zip index.html buildspec.yml
aws s3 cp source.zip s3://brainybee-lab-source-<YOUR_ACCOUNT_ID>/source.zipStep 3: Create the CodeBuild Project
CodeBuild will take your source.zip, read the buildspec.yml, and generate an output artifact.
# Note: In a real scenario, you'd create an IAM role first.
# For this lab, assume a role named 'CodeBuildServiceRole' exists.
aws codebuild create-project --name "BrainyBee-Build" \
--source "type=S3,location=brainybee-lab-source-<YOUR_ACCOUNT_ID>/source.zip" \
--artifacts "type=NO_ARTIFACTS" \
--environment "computeType=BUILD_GENERAL1_SMALL,image=aws/codebuild/standard:5.0,type=LINUX_CONTAINER" \
--service-role "arn:aws:iam::<YOUR_ACCOUNT_ID>:role/service-role/CodeBuildServiceRole"[!TIP] If you don't have an IAM role ready, use the Console alternative below as it creates the service role automatically.
▶Console alternative
- Go to CodeBuild > Create build project.
- Project name:
BrainyBee-Build. - Source provider: Amazon S3.
- Bucket:
brainybee-lab-source-<YOUR_ACCOUNT_ID>, S3 object:source.zip. - Environment: Managed image, Ubuntu, Standard runtime, Image:
aws/codebuild/standard:5.0. - Service role: New service role.
- Click Create build project.
Step 4: Create the Pipeline
Now, connect the Source and Build stages using CodePipeline.
▶Console Instructions (Recommended for Pipeline Creation)
- Navigate to CodePipeline > Create pipeline.
- Name:
BrainyBee-Pipeline. - Source Stage: Provider: Amazon S3. Bucket: your source bucket. S3 object key:
source.zip. Change detection: AWS CloudWatch Events. - Build Stage: Provider: AWS CodeBuild. Project name:
BrainyBee-Build. - Deploy Stage: Click Skip deploy stage (we will add a manual approval instead).
- Review and Create pipeline.
Step 5: Add a Manual Approval Stage
- In the CodePipeline console, select your pipeline and click Edit.
- Click + Add stage at the end of the pipeline. Name it
Approval. - Click + Add action group.
- Action name:
Manager-Approval. Action provider: Manual approval. - Click Done and then Save.
Checkpoints
| Verification Task | Expected Result |
|---|---|
| Source Check | Pipeline status changes to "In Progress" immediately after uploading source.zip. |
| Build Check | CodeBuild status shows "Succeeded" and output.html is created in the artifact S3 bucket. |
| Approval Check | Pipeline stops at the Approval stage, status is "Waiting for approval". |
Troubleshooting
| Error | Cause | Fix |
|---|---|---|
The bucket does not have versioning enabled | CodePipeline requires versioning for S3 sources. | Run the put-bucket-versioning CLI command provided in Step 1. |
AccessDenied in CodeBuild | The CodeBuild service role lacks S3 permissions. | Attach AmazonS3ReadOnlyAccess to the IAM role. |
Buildspec file does not exist | The zip file structure is nested or the filename is wrong. | Ensure buildspec.yml is in the root of the source.zip file. |
Challenge
Add a Unit Test Stage: Modify your pipeline to include a second CodeBuild project called BrainyBee-Test. This project should run a simple shell script to verify the content of output.html (e.g., grep "Hello" output.html). Insert this stage between Build and Approval.
Teardown
To avoid costs, delete all resources created:
# Delete the Pipeline
aws codepipeline delete-pipeline --name BrainyBee-Pipeline
# Delete the Build Project
aws codebuild delete-project --name BrainyBee-Build
# Empty and Delete S3 Buckets
aws s3 rb s3://brainybee-lab-source-<YOUR_ACCOUNT_ID> --force
# Note: Delete the artifact bucket created by CodePipeline as well (usually named codepipeline-<region>-...)Cost Estimate
| Service | Estimated Cost (Monthly) |
|---|---|
| AWS CodePipeline | $1.00 per active pipeline (Free tier: 1 free pipeline per month) |
| AWS CodeBuild | $0.005 per build minute (Free tier: 100 minutes of build.general1.small) |
| Amazon S3 | $0.023 per GB (Negligible for this lab) |
Concept Review
Pipeline Concepts vs. DevOps Professional Exam
| Term | Definition | Exam Context |
|---|---|---|
| Revision | A specific change in the source. | Triggers the pipeline execution. |
| Action Type | One of 6 types: Source, Build, Test, Deploy, Approval, Invoke. | You must choose the right type for custom Lambda integrations. |
| Artifacts | Files passed between stages. | Stored in S3; encrypted by KMS by default. |
| Transitions | Links between stages. | Can be disabled to stop automated flow (e.g., during a maintenance window). |