BrainyBeeBrainyBee
ExploreBlogStart Studying
HomeAWS Certified DevOps Engineer - Professional (DOP-C02)Lab: Managing Artifact Lifecycles with AWS CodeBuild and S3
Hands-On Lab1,145 words

Lab: Managing Artifact Lifecycles with AWS CodeBuild and S3

Build and manage artifacts

Lab: Managing Artifact Lifecycles with AWS CodeBuild and S3

This lab provides a guided experience in configuring AWS CodeBuild to produce and manage software artifacts. You will learn how to define a buildspec.yml file, configure an S3 bucket for artifact storage, and trigger a build process to generate a deployable package.

[!WARNING] Remember to run the teardown commands at the end of this lab to avoid ongoing charges in your AWS account.

Prerequisites

  • AWS Account: An active AWS account with Administrator access.
  • AWS CLI: Installed and configured with aws configure (use Region us-east-1 for this lab).
  • IAM Permissions: Ability to create S3 buckets, CodeBuild projects, and IAM Roles.
  • Programming Environment: A local terminal or AWS Cloud9 instance.

Learning Objectives

By the end of this lab, you will be able to:

  1. Create and configure an Amazon S3 bucket for secure artifact storage.
  2. Define build phases and artifact locations using a buildspec.yml file.
  3. Create an AWS CodeBuild project using the AWS CLI and Console.
  4. Verify artifact integrity and lifecycle in the destination repository.

Architecture Overview

The following diagram illustrates the flow of data from the source through the build environment to the final artifact storage.

Loading Diagram...
Figure 1 — Mermaid diagram

Step-by-Step Instructions

Step 1: Create the Artifact S3 Bucket

We need a centralized location to store the results of our build process. We will use Amazon S3 as our artifact repository.

bash
# Generate a unique suffix RANDOM_ID=$RANDOM BUCKET_NAME="brainybee-artifacts-$RANDOM_ID" # Create the bucket aws s3 mb s3://$BUCKET_NAME --region us-east-1
▶Console alternative
  1. Sign in to the AWS Management Console.
  2. Navigate to S3 > Create bucket.
  3. Enter a unique name (e.g., brainybee-artifacts-unique-id).
  4. Keep default settings and click Create bucket.

Step 2: Prepare the Build Specification

The buildspec.yml file is the heart of CodeBuild. It tells the service exactly what commands to run and which files to package.

Create a file named buildspec.yml in your local directory:

yaml
version: 0.2 phases: install: runtime-versions: python: 3.11 build: commands: - echo "Starting build process on `date`" - mkdir -p target - echo "<html><h1>Hello from BrainyBee Lab</h1></html>" > target/index.html post_build: commands: - echo "Build completed on `date`" artifacts: files: - "**/*" base-directory: 'target' name: MyWebArtifact-$(date +%Y-%m-%d)

[!TIP] The artifacts section identifies which files CodeBuild should upload to S3. Using **/* captures all files recursively inside the base-directory.

Step 3: Create the Source Bundle

CodeBuild needs a source. For this lab, we will zip our buildspec.yml and upload it to the same S3 bucket to simulate a source repository.

bash
# Zip the buildspec zip source.zip buildspec.yml # Upload to S3 aws s3 cp source.zip s3://$BUCKET_NAME/source.zip

Step 4: Create the CodeBuild Project

We will now create the project. We must provide an IAM Service Role that has permission to write to S3.

[!NOTE] In a production environment, you would follow the principle of least privilege for the IAM Role. For this lab, we will use a JSON configuration file.

Create a file named project-config.json (replace <YOUR_BUCKET_NAME> and <YOUR_ACCOUNT_ID>):

json
{ "name": "brainybee-build-project", "source": { "type": "S3", "location": "<YOUR_BUCKET_NAME>/source.zip" }, "artifacts": { "type": "S3", "location": "<YOUR_BUCKET_NAME>", "name": "output", "packaging": "ZIP" }, "environment": { "type": "LINUX_CONTAINER", "image": "aws/codebuild/amazonlinux2-x86_64-standard:5.0", "computeType": "BUILD_GENERAL1_SMALL" }, "serviceRole": "arn:aws:iam::<YOUR_ACCOUNT_ID>:role/service-role/codebuild-service-role" }

Execute the creation command:

bash
aws codebuild create-project --cli-input-json file://project-config.json
▶Console alternative
  1. Navigate to CodeBuild > Build projects > Create build project.
  2. Project name: brainybee-build-project.
  3. Source: S3. Select your bucket and source.zip.
  4. Environment: Managed image, Amazon Linux 2, Standard runtime 5.0.
  5. Artifacts: S3. Select your bucket. Set artifacts packaging to Zip.
  6. Click Create build project.

Step 5: Start the Build

bash
aws codebuild start-build --project-name brainybee-build-project

Checkpoints

  1. Build Status: Run aws codebuild batch-get-builds --ids <BUILD_ID> (the ID is returned from the start command). Verify buildStatus is SUCCEEDED.
  2. S3 Check: List the bucket contents to see the output artifact.
    bash
    aws s3 ls s3://$BUCKET_NAME/
    Expected Result: You should see a file like MyWebArtifact-YYYY-MM-DD.zip in the bucket.

Concept Review

Understanding the lifecycle of a build is critical for the DevOps Professional exam. The following TikZ diagram shows the internal state transitions during a CodeBuild run.

Compiling TikZ diagram…
⏳
Running TeX engine…
This may take a few seconds
Figure 2 — TikZ diagram

Comparison: Artifact Storage Options

ServiceBest Use CasePrimary Feature
Amazon S3General files, static assetsHighly durable, Versioning, Lifecycle policies
AWS CodeArtifactSoftware packages (npm, maven, pip)Dependency management, internal sharing
Amazon ECRDocker / Container imagesIntegrated with ECS/EKS, image scanning

Troubleshooting

IssuePossible CauseFix
ACCESS_DENIED in logsIAM Role missing S3 permissionsAttach s3:PutObject policy to the CodeBuild Service Role.
YAML_FILE_ERRORSyntax error in buildspec.ymlValidate YAML structure; ensure version is 0.2 (not 2.0).
DOWNLOAD_SOURCE_FAILEDS3 bucket permissions or pathEnsure the bucket is in the same region and the file name is exact.

Stretch Challenge

  1. Multiple Artifacts: Modify your buildspec.yml to produce two secondary artifacts: one for index.html and another for a fake log file, and store them in different S3 prefixes.
  2. Encryption: Enable Server-Side Encryption (SSE-KMS) on the artifact bucket and update the CodeBuild project to use a customer-managed key.

Cost Estimate

ServiceUsageEstimated Cost (Monthly)
AWS CodeBuild30 minutes (small)$0.15 (Free tier covers first 100 mins)
Amazon S3< 1GB Storage$0.02 (Free tier covers 5GB)
Total~$0.17

Clean-Up / Teardown

To avoid ongoing charges, delete the resources created during this lab.

bash
# 1. Delete the S3 Bucket (and all contents) aws s3 rb s3://$BUCKET_NAME --force # 2. Delete the CodeBuild Project aws codebuild delete-project --name brainybee-build-project # 3. Delete the local files rm buildspec.yml project-config.json source.zip
All AWS Certified DevOps Engineer - Professional (DOP-C02) Study Resources

Related Notes

  • Build and Manage Artifacts: AWS DevOps Professional Study Guide920 words
  • Mastering AWS Alerting and Automated Remediation1,050 words
  • Study Guide: Analyzing Failed Deployments in AWS940 words
  • Incident Analysis: Troubleshooting Failed Processes in AWS1,050 words
  • Mastering AWS Monitoring & Security Analytics: Logs, Metrics, and Findings1,050 words
  • AWS Log Analysis: Athena, CloudWatch Insights, and OpenSearch920 words
  • Analyzing Real-Time Log Streams with Amazon Kinesis Data Streams985 words
  • CloudWatch Anomaly Detection Alarms: Professional Study Guide820 words
  • AWS Application Storage Patterns: EBS, EFS, and S31,054 words
  • Lab: Automating Security Controls and Data Protection with AWS Secrets Manager and Config942 words
  • Master Study Guide: Automating Security Controls & Data Protection (AWS DOP-C02)1,184 words
  • Mastering AWS CloudFormation StackSets: Multi-Account & Multi-Region Orchestration895 words

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

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

Start Studying

Ready to study AWS Certified DevOps Engineer - Professional (DOP-C02)?

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

Start Studying — Free
AWS Certified DevOps Engineer - Professional (DOP-C02) ResourcesExplore All HivesBlogHome

© 2026 BrainyBee. Free AI-powered exam prep.

Loading Diagram...
Flowchart, left to right. Source (S3 Bucket) connects to AWS CodeBuild Project ("Input Artifact"). CodeBuild connects to buildspec.yml ("Config"). Spec connects to Install / Build / Post-Build. Phases connects to Artifact Repository (S3) ("Output Artifact"). Dest connects to Expiration/Transition ("Lifecycle Policy").