Lab: Automating Application Delivery with AWS CI/CD Pipelines
Deploy code by using AWS Continuous Integration and Continuous Delivery (CI/CD) services
Lab: Automating Application Delivery with AWS CI/CD Pipelines
In this lab, you will transition from manual deployments to a fully automated Continuous Integration and Continuous Delivery (CI/CD) workflow. Following the DevOps philosophy of TodoPlus Limited, you will set up a pipeline that automatically builds and deploys a Lambda function whenever code is pushed to a version control repository.
[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges. CI/CD resources often incur costs if left active.
Prerequisites
Before starting, ensure you have:
- An AWS Account with administrative access.
- AWS CLI installed and configured locally with
aws configure. - Git installed on your local machine.
- IAM Permissions: Ability to create IAM Roles, CodeCommit repositories, CodeBuild projects, and CodePipelines.
Learning Objectives
By the end of this lab, you will be able to:
- Create and manage source code using AWS CodeCommit.
- Define build specifications and automate artifact creation with AWS CodeBuild.
- Orchestrate a multi-stage deployment workflow using AWS CodePipeline.
- Implement automated triggers that respond to code commits.
Architecture Overview
The following diagram illustrates the workflow you will build. A developer pushes code to CodeCommit, which triggers CodePipeline. CodePipeline then uses CodeBuild to package the application and prepares it for deployment.
Step-by-Step Instructions
Step 1: Create the Source Repository
We need a central place to store our application code using Git principles.
aws codecommit create-repository --repository-name brainybee-lab-repo --repository-description "Lab repository for CI/CD"▶Console alternative
- Open the AWS CodeCommit console.
- Choose Create repository.
- Name it
brainybee-lab-repoand click Create.
Step 2: Initialize Local Code and Buildspec
CodeBuild requires a buildspec.yml file to know how to compile and package your code.
- Create a local folder and initialize git:
mkdir cicd-lab && cd cicd-lab
git init- Create a simple Lambda function
index.js:
exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify('Hello from the CI/CD Pipeline!'),
};
};- Create
buildspec.ymlin the root directory:
version: 0.2
phases:
install:
runtime-versions:
nodejs: 18
build:
commands:
- echo "Building the application..."
- zip -r deployment_package.zip index.js
artifacts:
files:
- deployment_package.zipStep 3: Create the CodeBuild Project
Now we define the environment where the build happens.
# Note: This assumes you have a service role. For lab simplicity, use the console to auto-generate the role.▶Console instructions (Recommended for Role generation)
- Navigate to CodeBuild > Build projects > Create build project.
- Project Name:
brainybee-build-project. - Source: AWS CodeCommit >
brainybee-lab-repo. - Environment: Managed Image > Amazon Linux 2 > Standard > Runtime: Node.js.
- Artifacts: Select No artifacts (we will configure this via CodePipeline later).
- Buildspec: Use the
buildspec.ymlfile in the root. - Click Create build project.
Step 4: Create the CodePipeline
This orchestrates the flow from CodeCommit to CodeBuild.
▶Console instructions
- Navigate to CodePipeline > Pipelines > Create pipeline.
- Pipeline name:
brainybee-deployment-pipeline. - Source Stage: Source provider: AWS CodeCommit; Repository:
brainybee-lab-repo; Branch:master(ormain). - Build Stage: Build provider: AWS CodeBuild; Project name:
brainybee-build-project. - Deploy Stage: For this lab, select Skip deploy stage (we are focusing on CI/Build automation).
- Review and Create pipeline.
Checkpoints
Checkpoint 1: Source Connection
Verify your local files are in CodeCommit.
- Action: Run
git pushto your CodeCommit remote. - Expected Result: CodeCommit console shows
index.jsandbuildspec.yml.
Checkpoint 2: Pipeline Trigger
Verify the automation starts.
- Action: Check the CodePipeline dashboard immediately after the push.
- Expected Result: The Source stage should turn blue (In Progress) and then green (Succeeded), followed by the Build stage.
Teardown
[!IMPORTANT] To avoid charges, delete the following resources in order:
- CodePipeline:
bash
aws codepipeline delete-pipeline --name brainybee-deployment-pipeline - CodeBuild Project:
bash
aws codebuild delete-project --name brainybee-build-project - CodeCommit Repository:
bash
aws codecommit delete-repository --repository-name brainybee-lab-repo - S3 Artifact Bucket: Delete the bucket created by CodePipeline (starts with
codepipeline-us-east-1-...).
Troubleshooting
| Error | Likely Cause | Solution |
|---|---|---|
Access Denied | Missing IAM permissions for CodeBuild or Pipeline. | Ensure the Service Role has AWSCodeBuildAdminAccess and S3FullAccess. |
Buildspec not found | File named incorrectly or not in root. | Ensure file is exactly buildspec.yml in the base directory. |
Git Push Failed | Missing Git-Remote-CodeCommit helper. | Install pip install git-remote-codecommit or use HTTPS credentials. |
Stretch Challenge
The "Full CD" Challenge: Modify the CodePipeline to include a Deploy Stage using AWS CodeDeploy.
- Create an AWS Lambda function.
- Update your
buildspec.ymlto produce anappspec.ymlfile. - Add a deployment stage to the pipeline that updates the Lambda function code using the zip file produced in the Build stage.
Cost Estimate
| Service | Cost Detail |
|---|---|
| AWS CodeCommit | First 5 active users are free. |
| AWS CodeBuild | 100 build minutes (build.general1.small) free per month. |
| AWS CodePipeline | 1 free pipeline per month; $1.00 per active pipeline thereafter. |
| S3 Storage | Standard storage rates ($0.023/GB) for build artifacts. |
Total Estimated Lab Cost: $0.00 (If within Free Tier limits).
Concept Review
Understanding the "DevOps Loop" in AWS:
\begin{tikzpicture}[node distance=2cm] \draw[thick, ->] (0,1) arc (90:-270:1cm); \node at (0,1.3) {Plan & Code}; \node at (1.5,0) {Build}; \node at (0,-1.3) {Deploy}; \node at (-1.5,0) {Monitor}; \draw[dashed] (0,0) circle (0.5cm); \node at (0,0) {\tinyCI/CD}; \end{tikzpicture}
- Continuous Integration (CI): Automating the merge and build process. (CodeCommit + CodeBuild).
- Continuous Delivery (CD): Ensuring the code is always in a deployable state. (CodePipeline).
- Continuous Deployment: Automatically pushing every change to production. (CodePipeline + CodeDeploy).