Rock Solid Lambda Development with Tests and Version Control

Tim Hawkins
4 min readNov 13, 2020

Suitable for beginners!

Who is this tutorial for?

This tutorial is for anyone looking for a quick and convenient way to develop their lambda functions.

Why develop using this method?

I think there a few advantages to this method:

  • Develop locally. Developing and testing your lambda functions locally is a much more efficient and rewarding experience. It allows you to use your favourite IDE extensions, linters and terminal.
  • Version control. A huge advantage is keeping your version control within the same environment as all your other repos.
  • Test driving your work. Having a suite of tests to run when developing will make sure your code is rock solid.

1: Getting started

Go ahead and clone the repo below to get the boilerplate for your new function. Take a look and you’ll find a build.sh script, a directory to hold all of your Lambda code and a directory to hold your tests.

This super lightweight repo gives you everything you need to start developing. Run npm install in the root and then run npm test. You should see two tests passing, something like:

Tests pass — Nice!

2. Set up AWS

Create your Lambda function…

Go to https://aws.amazon.com/ and log in to your AWS account. Navigate to the lambda page in your region and click:

Enter your function name, select your Node version and create your function:

Create your function…

Get your security credentials…

Click your username in the AWS navigation bar and select user credentials. Here you’ll find AWS account ID. We will also need to create an access key. Click “Create access key” and copy your key and account ID to somewhere safe. You can only see this once so be careful with it.

You’ll need to add these credentials to allow you to deploy the function. There is a link below with details of how to do this, it is very quick and simple:

https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html

3. Deploy the function

Inside the repo

Inside the repo we can find the framework for the function. In index.js there is the code of your function and in test/index.test.js are the tests for this function. Have a play around with the tests and code to get used to the basics of Lambda.

Update the function name

There are a couple of changes required in order to deploy the function. Firstly, let’s change the name of the LambdaFunction directory. You should change it to the same name of your function you created in AWS in step 2. We then need to updated the script in the package.json to the same name. Just swap in the name of your function in to the line "deploy": "./build.sh LambdaFunction"

Deploy…

All that’s left is to deploy to AWS. Simply run npm run deploy in the terminal. You’ll see a series of logs as your project is added to AWS. If all goes correctly it will finish with the line !! Upload successful !!

Test the deployment

Great, you’ve deployed your function. Now to test if it works. If you head back to AWS and find your Lambda function you’ll see a test button in the top right corner. Click Select test event and select Configure test then replace the placeholder JSON with the object:

{
"name": "Tim Hawkins"
}

Click Test and you should see a successful response.

3. Start developing…

Now you have your function in the cloud you’ll want to start developing your own functionality. If you are familiar with test driven development this will be second nature to you. Develop locally using index.test.js and npm run test and deploy to AWS using npm run deploy.

I hope this is useful for you, I find it a super easy and convenient way of keeping my Lambda functions in version control with a full testing suite. Have fun with it and let me know if you have any problems in the comments.

--

--