๐ Building a Serverless CI/CD Pipeline with AWS Lambda, CodePipeline, and GitHub
Author: Ritesh
Introduction
In this blog, Iโll walk you through how I built a fully serverless CI/CD pipeline using AWS services โ no EC2, no Jenkins, no manual intervention.
This project automatically deploys my application to Amazon S3 + CloudFront whenever I push code to GitHub, leveraging Lambda, API Gateway, CodePipeline, and CodeBuild.
Along the way, Iโll also share the difficulties I faced (like API Gateway setup and IAM permissions) and how I solved them.
Project Goal
โAutomate code deployment from GitHub โ AWS โ CloudFront using only serverless services.โ
Architecture Overview
Service used
| Service | Role |
|---|---|
| GitHub | Code repository & webhook trigger |
| API Gateway | Receives webhook requests |
| AWS Lambda | Starts CodePipeline |
| AWS CodePipeline | Automates build and deploy stages |
| AWS CodeBuild | Builds and tests the application |
| Amazon S3 | Hosts static website |
| Amazon CloudFront | Delivers the website globally |
| Amazon CloudWatch | Logs & monitors activity |
| AWS IAM | Manages permissions securely |
Step-by-Step Implementation
Step 1: Create and Push Code to GitHub
Create a new repository โ e.g. serverless-ci-cd-pipeline.
Add your frontend or simple app files (HTML/CSS/JS).
Push your code to GitHub:
git add .
git commit -m "initial commit"
git push origin main
Step 2: Create an AWS CodePipeline
- Go to AWS CodePipeline โ Create pipeline
- Source stage:
- Provider: GitHub
Connect your repo and branch
- Build stage:
- Provider: AWS CodeBuild
Create a new CodeBuild project
- Deploy stage:
Provider: Amazon S3
Select your target S3 bucket
Now your pipeline can build and deploy manually โ next weโll automate it using webhooks.
Step 3: Create an API Gateway Endpoint
- Open Amazon API Gateway โ Create API
Choose HTTP API โ Click Build
Under โIntegrations,โ choose Lambda function (weโll create it in the next step).
Click Create
- Copy the Invoke URL (youโll use this in your GitHub webhook).
Step 4: Create AWS Lambda Function
Go to Lambda โ Create function
Runtime: Python 3.9
Function name: trigger-codepipeline
- Paste this code:
import boto3
import json
def lambda_handler(event, context):
pipeline_name = "your-pipeline-name"
codepipeline = boto3.client('codepipeline')
response = codepipeline.start_pipeline_execution(name=pipeline_name)
return {
'statusCode': 200,
'body': json.dumps('Pipeline triggered successfully!')
}
- Go to Configuration โ Permissions โ Execution Role
Attach policy: AWSCodePipelineFullAccess
Attach policy: CloudWatchLogsFullAccess
- Deploy your Lambda function.
โ Now Lambda can trigger your CodePipeline programmatically.
Step 5: Connect Lambda to API Gateway
In API Gateway โ Integrations, select your Lambda function.
Deploy the API.
Test it by sending a POST request using:
curl -X POST https://<your-api-id>.execute-api.us-east-1.amazonaws.com
Step 6: Add GitHub Webhook
- Go to your GitHub Repo โ Settings โ Webhooks โ Add Webhook
Payload URL = your API Gateway Invoke URL
Content type = application/json
- Select event: Just the push event
6 .Click Add Webhook
Now, every time you push code to GitHub, it automatically triggers the pipeline!
Testing the Pipeline
- Make a small code change and push it:
git add .
git commit -m "update UI"
git push
Go to AWS CodePipeline, and youโll see the pipeline running automatically.
After a few minutes, your updated code will be deployed to S3 and available via CloudFront URL.
Common Issues I Faced (and Solved)
| Issue | Cause | Solution |
|---|---|---|
| โ AccessDenied in S3 | Missing IAM permissions | Attached AmazonS3FullAccess to CodePipeline role |
| โ Lambda failed to trigger pipeline | No permission to start pipeline | Added AWSCodePipelineFullAccess policy to Lambda role |
| โ Webhook not triggering | API Gateway method not deployed | Re-deployed the API after integration |
| โ Internal server error (500) | JSON format mismatch | Validated payload from GitHub with test event |
Security Best Practices
Use IAM least privilege roles (donโt overgrant permissions).
Always use HTTPS for API Gateway.
If using GitHub tokens, rotate them regularly.
Log all actions with CloudTrail (optional).
Final Outcome
- Fully automated, serverless CI/CD pipeline
- No EC2 or Jenkins โ pay only for what you use
- Deployment happens instantly on every GitHub push
Architecture Summary:
GitHub โ API Gateway โ Lambda โ CodePipeline โ CodeBuild โ S3 โ CloudFront
Key Learnings
How to integrate GitHub Webhooks with AWS Lambda
Event-driven DevOps design
-
Secure IAM role management
- Real-world CI/CD automation using AWS native tools
Conclusion
This project helped me understand the power of Serverless DevOps โ simple, scalable, and cost-efficient.
Itโs a perfect real-world example of how CI/CD can be built entirely with AWS services โ a valuable step in my DevOps learning journey.
If youโre getting started in DevOps or AWS, try building this โ youโll learn Lambda, API Gateway, and CodePipeline in action.
Project Links
GitHub: https://github.com/ritesh355/serverless-ci-cd-demo/settings/hooks
๐ข Connect With Me
- ๐ผ LinkedIn
- ๐ Hashnode Blog
- ๐ป GitHub











Top comments (0)