DEV Community

Cover image for ๐Ÿš€AWS & DevOps Project : ๐Ÿš€ Building a Serverless CI/CD Pipeline with AWS Lambda, CodePipeline, and GitHub
Ritesh Singh
Ritesh Singh

Posted on

๐Ÿš€AWS & DevOps Project : ๐Ÿš€ Building a Serverless CI/CD Pipeline with AWS Lambda, CodePipeline, and GitHub

๐Ÿš€ 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

  1. Create a new repository โ€” e.g. serverless-ci-cd-pipeline.

  2. Add your frontend or simple app files (HTML/CSS/JS).

  3. Push your code to GitHub:

git add .
git commit -m "initial commit"
git push origin main
Enter fullscreen mode Exit fullscreen mode

Step 2: Create an AWS CodePipeline

  1. Go to AWS CodePipeline โ†’ Create pipeline


  1. Source stage:
  • Provider: GitHub

Connect your repo and branch

  1. Build stage:
  • Provider: AWS CodeBuild

Create a new CodeBuild project

  1. 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

  1. Open Amazon API Gateway โ†’ Create API

  1. Choose HTTP API โ†’ Click Build

  2. Under โ€œIntegrations,โ€ choose Lambda function (weโ€™ll create it in the next step).

  3. Click Create

  1. Copy the Invoke URL (youโ€™ll use this in your GitHub webhook).

Step 4: Create AWS Lambda Function

  1. Go to Lambda โ†’ Create function

  2. Runtime: Python 3.9

  3. Function name: trigger-codepipeline

  1. 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!')
    }
Enter fullscreen mode Exit fullscreen mode
  1. Go to Configuration โ†’ Permissions โ†’ Execution Role
  • Attach policy: AWSCodePipelineFullAccess

  • Attach policy: CloudWatchLogsFullAccess

  1. 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
Enter fullscreen mode Exit fullscreen mode

Step 6: Add GitHub Webhook

  1. Go to your GitHub Repo โ†’ Settings โ†’ Webhooks โ†’ Add Webhook

  1. Payload URL = your API Gateway Invoke URL

  2. Content type = application/json

  1. 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

  1. Make a small code change and push it:
git add .
git commit -m "update UI"
git push
Enter fullscreen mode Exit fullscreen mode
  1. Go to AWS CodePipeline, and youโ€™ll see the pipeline running automatically.

  2. 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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)