Hands-On Lab945 words

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:

  1. An AWS Account with administrative access.
  2. AWS CLI installed and configured locally with aws configure.
  3. Git installed on your local machine.
  4. 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.

Loading Diagram...

Step-by-Step Instructions

Step 1: Create the Source Repository

We need a central place to store our application code using Git principles.

bash
aws codecommit create-repository --repository-name brainybee-lab-repo --repository-description "Lab repository for CI/CD"
Console alternative
  1. Open the AWS CodeCommit console.
  2. Choose Create repository.
  3. Name it brainybee-lab-repo and click Create.

Step 2: Initialize Local Code and Buildspec

CodeBuild requires a buildspec.yml file to know how to compile and package your code.

  1. Create a local folder and initialize git:
bash
mkdir cicd-lab && cd cicd-lab git init
  1. Create a simple Lambda function index.js:
javascript
exports.handler = async (event) => { return { statusCode: 200, body: JSON.stringify('Hello from the CI/CD Pipeline!'), }; };
  1. Create buildspec.yml in the root directory:
yaml
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.zip

Step 3: Create the CodeBuild Project

Now we define the environment where the build happens.

bash
# 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)
  1. Navigate to CodeBuild > Build projects > Create build project.
  2. Project Name: brainybee-build-project.
  3. Source: AWS CodeCommit > brainybee-lab-repo.
  4. Environment: Managed Image > Amazon Linux 2 > Standard > Runtime: Node.js.
  5. Artifacts: Select No artifacts (we will configure this via CodePipeline later).
  6. Buildspec: Use the buildspec.yml file in the root.
  7. Click Create build project.

Step 4: Create the CodePipeline

This orchestrates the flow from CodeCommit to CodeBuild.

Console instructions
  1. Navigate to CodePipeline > Pipelines > Create pipeline.
  2. Pipeline name: brainybee-deployment-pipeline.
  3. Source Stage: Source provider: AWS CodeCommit; Repository: brainybee-lab-repo; Branch: master (or main).
  4. Build Stage: Build provider: AWS CodeBuild; Project name: brainybee-build-project.
  5. Deploy Stage: For this lab, select Skip deploy stage (we are focusing on CI/Build automation).
  6. Review and Create pipeline.

Checkpoints

Checkpoint 1: Source Connection

Verify your local files are in CodeCommit.

  • Action: Run git push to your CodeCommit remote.
  • Expected Result: CodeCommit console shows index.js and buildspec.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:

  1. CodePipeline:
    bash
    aws codepipeline delete-pipeline --name brainybee-deployment-pipeline
  2. CodeBuild Project:
    bash
    aws codebuild delete-project --name brainybee-build-project
  3. CodeCommit Repository:
    bash
    aws codecommit delete-repository --repository-name brainybee-lab-repo
  4. S3 Artifact Bucket: Delete the bucket created by CodePipeline (starts with codepipeline-us-east-1-...).

Troubleshooting

ErrorLikely CauseSolution
Access DeniedMissing IAM permissions for CodeBuild or Pipeline.Ensure the Service Role has AWSCodeBuildAdminAccess and S3FullAccess.
Buildspec not foundFile named incorrectly or not in root.Ensure file is exactly buildspec.yml in the base directory.
Git Push FailedMissing 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.

  1. Create an AWS Lambda function.
  2. Update your buildspec.yml to produce an appspec.yml file.
  3. Add a deployment stage to the pipeline that updates the Lambda function code using the zip file produced in the Build stage.

Cost Estimate

ServiceCost Detail
AWS CodeCommitFirst 5 active users are free.
AWS CodeBuild100 build minutes (build.general1.small) free per month.
AWS CodePipeline1 free pipeline per month; $1.00 per active pipeline thereafter.
S3 StorageStandard 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).

Ready to study AWS Certified Developer - Associate (DVA-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free