AWS Lambda on Github Actions: How To Send Zipped Artifacts to AWS S3

Recently I’ve been experimenting with Github Actions as a CI tool, specifically for setting up AWS Lambda on Github Actions.

Container based CI is awesome. And I’m really excited about the community that is building up around it. I hope with container based CI we spend less time fighting CI, and more time building apps.

But until we get there — I’ll try and make the CI fighting a little less painful by giving you a head start. And in this case, we’ll be pushing zipped artifacts for AWS Lambda on Github Actions.

YAML for pushing artifacts to S3

AWS Lambda works by associating artifacts with the running Lambda exectuion. Therefore it’s quite common to zip our artifacts and upload them onto S3 to be used by Lambda. Today I’ll walk you through a quick three step method to upload zipped artifacts onto AWS for later use with AWS Lambda.

By the end of this article you’ll know the first step towards working with AWS Lambda on Github Actions and that means setting up pushing of zipped artifacts to S3. 

AWS Lambda on Github Actions Pre-Requisites

Before we get going with setting up AWS Lambda on Github Actions I’m assuming that you have got the following things setup.

I won’t go through all of the pre-requisites today, but I will point you in the right direction for each of the areas!

AWS S3 Logo

An S3 bucket and AWS account — First up, you’ll need an S3 bucket, and for that you’ll need an S3 account. If you don’t AWS setup, I have two articles I’d recommend you read first: Infrastructure As Code: A Quick And Simple Explanation for why you should setup this infra with code, and then I’d follow that with Learn The 6 Fundamentals Of Terraform — In Less Than 20 Minutes which covers the main aspects of Terraform, so that you can script an S3 bucket with Terraform.

Github Actions Logo

You have Github Actions Setup — Secondly, I’m assuming you’ve already got a Github Actions workflow setup. If you haven’t, I’d suggest reading the Github Actions documentation on workflows. You’ll essentially need to end up with a YAML file inside of a GitHub repository. Then you’ll be able to use the steps that we talk about later in this article.

Github Actions Beta

You’re part of the Github Actions Beta — At the time of writing, Github Actions is still in Beta. Which means in order to start playing with it, you’ll need to sign up for the Beta program. You can sign up on their website.

How it works

Okay — the above code shows you the end result. If you feel comfortable, you can simply copy paste. But, if you need a little explaining about what’s going on here, let me do that:

Step 1) Make a directory for our Artifact

First up, we’ll need to prepare a directory in our local repository that’s going to store our artifact. We create this separately so that we can later sync this directory with AWS. We aren’t interested in the rest of the code, that’s why we’re making a fresh directory here. We’re creating the name of our directory using the Github Action token for the directory.

Using the repository token means your artifacts S3 bucket will have the names of your repo to structure your artifacts. Having a bunch of artifacts for many lambda’s would soon get really out of hand. Here we’re creating the artifact directory as the same name as our Github repo.

Note: Do note that this will create two directories, one for the repo owner (that’s either your user name, or your organisations name) and one directory for your repo.

Step 2) Zip Our Artifact

We’ll need to now zip our artifact on the CI system. You could run this command prior to pushing but it makes sense to do it on CI as that means the process is as repeatable as possible.

To zip up our artifact we’re going to run a command on top of a base image that has zip installed. Here we’re simply zipping the src folder and putting it into our temporary artifacts directory.

Step 3) Push to S3

And then the last step is to sync up our artifacts directory with S3. The command is pretty smart, if the directories already exist in S3, it’ll ignore them, but if they don’t exist, it’ll create them.

This makes life easy, and it also means that your old artifacts won’t get overwritten. Each artifact is pushed up as the commit hash that pushed it, so you’re going to get a new artifact for every push.

Launch All the Lambda!

That’s it — just a quick one for how to setup the first part of AWS Lambda on Github Actions: pushing zipped Lambda artifacts. Github Actions is fairly new so there’s not a great deal of documentation out there.

Setting this up took a decent bit of fiddling to knock together, so hopefully that means it’s less fiddling for you and you can get on with building out your Lambda!

Speak soon Cloud Native friend!

Lou Bichard