Day 14: CI/CD with AWS CodePipeline and CodeBuild
Welcome to Day 14 of our exciting "30 Days of AWS" journey! If you've been following along from the beginning, kudos to you for diving into the world of Amazon Web Services. Your dedication and curiosity are truly commendable.
For those who might have just joined us or are specifically interested in today's topic, a warm welcome to you as well! While each article in this series delves into a different facet of AWS, rest assured that they are all interconnected, building upon the knowledge we've been cultivating day by day.
If you're here for the first time, I encourage you to take a moment to catch up on our previous discussions. This will enhance your understanding and ensure a seamless flow as we dive deeper into the fascinating journey of AWS together.
In today’s instalment, we will explore "CI/CD with AWS CodePipeline and CodeBuild." CI/CD stands for Continuous Integration and Continuous Deployment, a core practice in modern software development that automates the process of building, testing, and deploying code changes.
As always, feel free to engage, ask questions, and share your thoughts in the comments. Your participation is what makes this series vibrant and valuable. I’m thrilled to have you join us on this journey. Let’s get started!
What is CI/CD? Let’s Make It Simple!
Alright, let’s break down CI/CD in the simplest way possible:
Continuous Integration (CI):
Imagine a classroom project where 5 students are each working on different parts of a presentation. Every day, they come together to combine their work into a single, unified document. Now, what happens if two students accidentally work on the same slide or use different styles? It’s a mess!CI is like a smart daily check-in system that automatically merges everyone’s work and ensures the final document (code) is neat, organized, and free of conflicts.
Continuous Deployment (CD):
Now imagine that after every successful check-in, a copy of this presentation is automatically printed and shared with your teacher, classmates, and parents—without anyone having to do it manually. This means every time someone improves the document, the changes are immediately seen by everyone.In the software world, CD means that every time a developer makes changes to the code, they’re automatically tested, approved, and pushed to production (the live environment) without any manual steps.
Why Do We Need CI/CD?
Let’s face it: software development can get messy. You may have multiple people working on different parts, new changes breaking old features, or errors that aren’t found until the very end. CI/CD solves these problems by making sure:
Every change is automatically tested.
Issues are caught early and fixed quickly.
Updates happen smoothly without breaking anything.
It’s like having a smart assistant that continuously watches your back!
The AWS Tools: CodePipeline and CodeBuild
AWS provides two services that make CI/CD easy:
AWS CodePipeline:
This is like the assembly line in a factory. As soon as new code is added, CodePipeline automatically moves it through different stages—building, testing, and deploying.AWS CodeBuild:
Think of this as a robotic builder inside the assembly line. Whenever CodePipeline asks it to, CodeBuild picks up the code, compiles it, runs tests, and makes it ready for deployment.
By combining these tools, you can automate the entire process of pushing code from your laptop to a live environment in AWS.
Step-by-Step: Creating a CI/CD Pipeline
Let’s build a simple CI/CD pipeline using CodePipeline and CodeBuild. We’ll automate the deployment of a small website hosted on S3 (Amazon’s storage service).
Step 1: Set Up the Website in S3
Go to the S3 Console and create a new bucket named
my-codepipeline-deployment-bucket
.Enable static website hosting.
- Choose Index Document:
index.html
.
- Choose Index Document:
Leave the permissions as default for now (public access).
Create the bucket.
Step 2: Prepare the Project Repository
Go to GitHub and create a new repository called
my-first-cicd
.Clone the repository to your local machine.
Add a simple HTML file named
index.html
:<!DOCTYPE html> <html> <head> <title>CI/CD with AWS CodePipeline</title> </head> <body> <h1>Welcome to AWS CodePipeline!</h1> <p>This page was deployed automatically using CI/CD.</p> </body> </html>
Commit and push the changes to the
main
branch of your repository.
Step 3: Create a CodePipeline in AWS
Go to the AWS CodePipeline Console.
Click on Create Pipeline and name it
my-first-pipeline
.Choose a new service role and click Next.
Step 4: Connect the Source (GitHub)
For Source Provider, select GitHub.
Connect your GitHub account and select the
my-first-cicd
repository.Choose
main
as the branch and click Next.
Step 5: Add a Build Stage Using CodeBuild
Choose AWS CodeBuild as the build provider.
Create a new build project:
- Environment:
Ubuntu
, runtime:standard
.
- Environment:
Set up the buildspec (instructions for CodeBuild) in your project root as
buildspec.yml
:version: 0.2 phases: build: commands: - echo "Building the website..." - mkdir -p output - cp index.html output/ artifacts: files: - output/**/*
Click Save.
Step 6: Deploy to S3
Add a Deploy Stage in CodePipeline.
Choose Amazon S3 as the deploy provider.
Select the S3 bucket you created (
my-codepipeline-deployment-bucket
).Click Next and create the pipeline.
Step 7: Test the Pipeline
Make a change in your
index.html
file (e.g., change the message).Push the changes to the
main
branch.Watch CodePipeline in action as it pulls the code, builds it using CodeBuild, and deploys it to the S3 bucket.
Once complete, check the S3 bucket URL, and you’ll see your changes live!
Benefits of CI/CD for Developers
No More Manual Deployments: With CI/CD, everything is automated. No need to upload files or run deployment scripts manually.
Faster Feedback: Developers get immediate feedback when something goes wrong, allowing them to fix issues early.
Consistency: Since everything is automated, the chances of human error are minimized.
Summary
Today, we explored:
What CI/CD is and how it simplifies software development.
Using AWS CodePipeline and CodeBuild to create a simple CI/CD pipeline.
Automating the deployment of a website to an S3 bucket.
What’s Next?
In Day 15, we’ll extend this setup by integrating it with AWS Lambda for serverless deployments. We’ll explore how to automatically trigger functions and deploy serverless applications using CI/CD.
Stay tuned, and let’s keep this AWS learning journey going strong!
Hope you find this blog helpful. Please share your thoughts in the comments—it will help me refine and provide more insightful content. Happy Learning!
Connect with Me - LinkedIn - Twitter/X - Topmate