This guide explains how to automate the deployment of an AWS Lambda function using GitHub Actions. This setup allows your function to be updated automatically whenever you push changes to your GitHub repository.
AWS Lambda is a serverless compute service that runs your code in response to events.
GitHub Actions is a CI/CD (Continuous Integration/Continuous Deployment) tool that automates workflows, like deploying your code.
This guide will show you how to deploy your AWS Lambda function automatically whenever you push code to GitHub.
Before starting, ensure you have:
- Go to AWS Lambda Console
- Click Create function
- Choose Author from scratch
- Set the function name (e.g.,
myLambdaFunction
) - Choose Python 3.x as the runtime
- Under Permissions, choose Create a new role with basic Lambda permissions
- Click Create function
- Go to AWS Lambda Console
- Click on your function
- Check the region in the top-right corner (e.g.,
us-east-1
)
- Go to AWS IAM Console
- Click Users → Add user
- Set the name (e.g.,
GitHubActionsUser
) - Choose Programmatic access
- Click Next and attach
AWSLambdaFullAccess
- Click Create user and download the Access Key & Secret Key
- Go to your GitHub repository
- Click Settings → Secrets and variables → Actions
- Click New repository secret
- Add the following secrets:
AWS_ACCESS_KEY_ID
→ (Your AWS Access Key ID)AWS_SECRET_ACCESS_KEY
→ (Your AWS Secret Access Key)
- In your GitHub repo, go to the
/.github/workflows/
directory (or create it if it doesn't exist) - Create a new file named
deploy-lambda.yml
- Add the following content:
name: Deploy AWS Lambda
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1 # Change this to match your AWS region
- name: Zip the Lambda function
run: zip function.zip lambda_function.py
- name: Deploy Lambda function
run: |
aws lambda update-function-code \
--function-name myLambdaFunction \
--zip-file fileb://function.zip
Add and commit the workflow file:
git add .github/workflows/deploy-lambda.yml
git commit -m "Added GitHub Actions for AWS Lambda"
git push origin main
Whenever you update lambda_function.py
and push the changes to GitHub, the GitHub Actions workflow will automatically deploy your function.
- Go to AWS Lambda Console
- Click on your function
myLambdaFunction
- Click Test
- Create a test event (default settings are fine)
- Click Test again to execute the function
Check the response. If successful, you should see:
{
"statusCode": 200,
"body": "AWS Lambda Updated via GitHub Actions!"
}
- Created an AWS Lambda function
- Set up IAM permissions & GitHub Secrets
- Configured GitHub Actions for deployment
- Successfully deployed AWS Lambda via GitHub Actions
- Tested and confirmed that deployments work
Now, every time you push a change to GitHub, your AWS Lambda function will be updated automatically.
Just edit lambda_function.py
and push the changes to GitHub. GitHub Actions will automatically redeploy the function.
- Check GitHub Actions logs for errors
- Ensure your IAM user has
AWSLambdaFullAccess
- Make sure your AWS region is correct
Yes. Just change the runtime in AWS Lambda and update the function code accordingly.
- Add more functionality to your Lambda function
- Automate deployments for multiple Lambda functions
- Integrate with AWS API Gateway or other AWS services
Now you can easily deploy AWS Lambda with GitHub Actions.