Day 141: Fetching Code from Bitbucket & Building with AWS CodeBuild
How to Retrieve Code from Bitbucket and Build Using AWS CodeBuild

Today, I expanded my AWS CI/CD skills by integrating Bitbucket with AWS CodeBuild.
After setting up CodeBuild yesterday, I wanted to connect it to a real-world source — Bitbucket — and automate the build process from code commit to build artifact creation.
🧠 What I Learned
🔹 Why Use Bitbucket with CodeBuild?
Bitbucket is a popular Git repository service by Atlassian.
Connecting it to AWS CodeBuild allows:
Seamless continuous integration (CI) from Bitbucket pushes.
Automated builds on every commit.
Centralized code and build management in AWS.
This integration is ideal for DevOps pipelines that rely on Bitbucket as the main version control system.
⚙️ Step-by-Step Setup
1️⃣ Connect Bitbucket to AWS CodeBuild
In the AWS Console → CodeBuild → Create Build Project:
Source Provider: Select Bitbucket.
Click Connect to Bitbucket — AWS will prompt for authorization.
Allow access to your repositories.
Once authorized, AWS will display your Bitbucket repositories.
2️⃣ Choose Repository and Branch
Select:
Repository: The project you want to build (e.g.,
my-webapp).Branch: Usually
mainormaster.
You can also configure webhooks, so a new build triggers automatically whenever code is pushed to Bitbucket.
3️⃣ Define the Build Environment
Environment image:
aws/codebuild/standard:7.0(latest)Runtime: Choose the language your project uses (Node.js, Python, Java, etc.)
Buildspec file: Set the location (e.g.,
buildspec.ymlin your repo root)
Example buildspec.yml:
version: 0.2
phases:
install:
commands:
- echo "Installing dependencies..."
- npm install
build:
commands:
- echo "Building project from Bitbucket source..."
- npm run build
artifacts:
files:
- '**/*'
base-directory: dist
This file defines how CodeBuild compiles and packages your Bitbucket code.
4️⃣ Add IAM Permissions
Ensure your CodeBuild service role has the following permissions:
codebuild:*s3:*logs:*bitbucket:*(via AWS connection)
This ensures CodeBuild can access Bitbucket, store logs, and upload build artifacts.
5️⃣ Test the Integration
Push a new commit to Bitbucket → Webhook triggers → CodeBuild starts automatically.
You can monitor build logs in CloudWatch or directly inside AWS CodeBuild.
If successful, your build artifacts (for example, a .zip or compiled files) will appear in your S3 bucket or output directory.
🧩 Common Issues
❗ Authentication errors: Ensure Bitbucket OAuth connection is valid.
❗ Missing buildspec file: Double-check file path and YAML syntax.
❗ Permission denied: Update IAM roles and repository permissions.
✅ Benefits
Direct Bitbucket → CodeBuild integration
Fully managed build environment
Supports parallel builds
Integrated logging and artifact management
🔍 Takeaway
“CodeBuild with Bitbucket automates the bridge between code commits and build artifacts — enabling fast, reliable, and hands-free CI.”
This integration mirrors real-world DevOps pipelines — code pushes automatically trigger builds and prepare applications for deployment.




