How to Automate Deployments from GitHub to AWS Lambda using GitHub Actions

How to Automate Deployments from GitHub to AWS Lambda using GitHub Actions

GitHub can be utilized to automatically deploy code changes to your AWS Lambda by using GitHub’s native tool GitHub Actions.

Lambda and GitHub Actions Overview

First, lets go over what AWS Lambda is and what GitHub Actions can accomplish for us.

AWS Lambda is a serverless computing service that allows you to run your code without having to worry about infrastructure management. With Lambda, you can easily deploy and run your code in response to events like changes to data in an Amazon S3 bucket or messages in an Amazon SQS queue.

GitHub Actions is a powerful tool that allows you to automate your software workflows, including building, testing, and deploying your code. By using GitHub Actions, you can easily integrate your code deployment process with your GitHub repository, and automate deployments to AWS Lambda whenever changes are made to your code.

GitHub to AWS Lambda Set Up

Now that we have an understanding of these two services, lets dive into how to automate your deployments from GitHub to AWS Lambda using GitHub Actions.

Create a Lambda Function in AWS

This involves defining the function name, runtime environment, memory allocation, and other settings. You can create a Lambda function using the AWS Management Console or by using the AWS CLI.

  1. Search for Lambda in the search bar.
  2. On the AWS Lambda page, click on Create Function button
  3. Add a name to the lambda function, set Runtime, and click on Create Function.
Write the Code for your Lambda Function Locally

This involves writing the code for your Lambda function using your preferred programming language and IDE. After writing the code for your Lambda function, you can push it to a branch in GitHub. This allows you to version control your code and collaborate with other developers.

Set Up GitHub Actions Workflow

You can create a GitHub Actions workflow to automatically build, test, and deploy your code to AWS Lambda whenever changes are made to your GitHub repository. Make sure to go and check out this article to see how to set up an OIDC provider with GitHub. Here is an example GitHub Actions workflow and some explanation on what is going on in the file.

name: lambda-deployment-with-github-action

on: 
  push:
    branches:
        master

jobs:
  lambda-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          role-to-assume:arn:aws:iam::1234567890:role/example-role
          role-session-name: samplerolesession
          aws-region: us-east-2

      - name: Zip lambda file
        shell: bash
        run: zip -r deployment.zip .

      - name: Copy Zip to S3
        shell: bash
        run: |
          aws s3 cp deployment.zip s3://${{ secrets.S3_BUCKET }}/${{ secrets.SOURCE_DIR }}/${{ github.ref_name }}/deployment.zip
      
      - name: Update function code
        shell: bash
        run: |
          aws lambda update-function-code --function-name ${{ secrets.FUNCTION_NAME }}-${{ github.ref_name }} --s3-bucket ${{ secrets.S3_BUCKET }} --s3-key ${{ secrets.SOURCE_DIR }}/${{ github.ref_name }}/deployment.zip

This file will:

  1. Checkout the current repo
  2. Configure AWS credentials by using your OIDC provider
  3. Zip up the lambda code into a file called deployment.zip
  4. Copy the zipped lambda file to S3 using repository variables to define the S3 bucket path
  5. Use the zipped lambda file in S3 to update lambda code using repository variables to define the function name
Test your deployment

Once you have set up your GitHub Actions workflow to deploy your code to AWS Lambda, you can test it by making changes to your code in GitHub. When you commit and push your changes, GitHub Actions will automatically trigger your workflow and deploy your code to AWS Lambda. You can go into the AWS Management Console and check to see that the Lambda code was updated.

Want to automate more services with GitHub Actions? Check out this article on how to automate deployments to AWS ECS using GitHub Actions!

Share This Post

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Do You Want To Take on the cloud?

drop us a line and keep in touch