Deploying AWS Lambda functions with GithHub Actions.

Nick Skriabin
3 min readJan 25, 2021

Lambda is a cloud functions service, a part of AWS infrastructure. It allows to build serverless architecture based on different platforms (Python, Node.js, etc.). Another Lambda application is to spread the load over the infrastructure to reduce the pressure on each individual part.

At Pickio we’re using Lambda to process images outside of the primary backend instances. It helps to decrease request-response times to the API and overall load.

In this article I will show you how we’re deploying our Lambdas to AWS using GitHub Actions.

Prerequisites

  • AWS account with admin/root access
  • GitHub repo where Lambda functions will live
  • aws-cli installed locally (optional)

Access to AWS from GitHub Actions

To make everything work what we need is to setup an AWS IAM role and a user that. We will use it to deploy code to Lambda.

I’ll be using aws-cli for the setup, but you can follow those steps in AWS Management Console.

The setup process consist of just three steps:

  • Create a group with rights to upload code to AWS Lambda
  • Create a user and add him to the group
  • Create access key for the user for further authentication within Github Actions

First, let’s create a group with proper policies attached:

aws iam create-group — group-name lambda-management
aws iam attach-group-policy — policy-arn arn:aws:iam::aws:policy/AWSLambdaFullAccess — group-name lambda-management

Create a user and add him to the group:

aws iam create-user — user-name lambda-deploy
aws iam add-user-to-group — user-name lambda-deploy — group-name lambda-management

Last step is to generate an access key for the newly created user:

aws iam create-access-key — user-name lambda-deploy

As the response you will receive a JSON containing AWS_ACCESS_KEY_ID and AWS_SECRET_KEY_ID, copy them and put in a safe place, we’re gonna need it later.

Prepare GitHub secrets

Before we can start building a deploy action, we need to provide GitHub with an access key:

  • Inside you repo navigate to Setting > Secrets
  • Click on New repository secret
  • As the name of the secret type AWS_ACCESS_KEY_ID
  • As the value put your access key id from the previous section
  • Click Add secret
  • Repeat these steps for AWS_SECRET_KEY_ID

Now we’re ready to setup the deployment.

AWS Lambda deployment

We’re using Node.js as the Lambda platform as well as for the deployment, but the process will be pretty much similar for the environment of your choice.

Our lambda folder structure looks like this:

Pay attention to lambda-function-X names. Every folder inside the lambda contains the source code of separate functions. Folders names must be the same as the names of your Lambdas in AWS.

Now, the deployment. To upload the source code to Lambda you need to put it inside a zip archive. We’re using a simple script to do so for every folder inside lambda:

You can test the script locally to be sure that everything works. In this case you’ll need to add access key to your local environment and aws-cli to be installed.

Github Actions

Now comes the fun part: automated deployment.

We trigger Lambda deployment on a commits to lambda branch, but you, surely, can change it. To make it work create .github directory at the root of your project and put the following file inside:

After that you can push your changes to the repository and see everything in action:

Github Actions deployment process

That’s pretty much it. Now you have automated AWS Lambda deployment. You can now enhance it with tests or versioning, but the basic setup is done.

Hope that this material would be helpful. If you have any questions, don’t hesitate to ask me in the comments section.

Cheers.

--

--