Testing Lambdas easily and locally

Well, there are so many ways to use server less functions and AWS Lambdas are the biggest way. If you are deploying these, there are also literally a dozen ways to do that. The simplest thing to do is to upload the Lambda and then use the AWS console to test it, but it doesn’t really scale.

The easiest way for simple lambdas is just to use Netlify Functions. They have abstracted 90% of the setup. You literally just do this:

  1. Go to your website and make sure it is connected to netlify
  2. Then you can create ./netlify/functions and by default any javascript program found there is going to be loaded into https://yoursite/.functions notice that there is a dot here. And that is all there is to it.
  3. You can also use Go to do this, but I was not able to get it to recognize Go scripts stored there. So I have a note out to Netlify on this.
  4. The really nice thing about this is that if you run netlify dev locally then it will just run those functions locally. You don’t have to do anything else

Under the covers, this is really using AWS Lambda, so really nice to get functions that are simple up and running quickly.

Using SAM to run Lambdas

There is also something called Serverless Application Model SAM which runs AWS locally. This, this is also pretty simple, you download it and install the templates with the instructions below. The actual lambda lives in sam/hello_world/app.py

brew install aws-sam-cli
cd test_directory
sam init
sam local invoke
# if you want to run this under docker
sam build--use-container

Using Localstacks and CDK

Now Sam is pretty simple, but if you want an entire stack. That is collection of servers and resources, then the new Cloud Development Kit is the way to go. It is more complicated, but basically when you combine it with local stack, you can do a cdklocal and get a local emulation of the entire Amazon environment.

This is pretty complicated mainly because cdk sprays all kinds of files on your machine and I’m still working through it, but the basics are to

brew install localstack
npm install @aws-cdk/aws-lambda @aws-s3
pip install -g awscdk-local
cdk init app --language typescript

Then theoretically if you follow the AWS Serverless Example you can just run the entire application locally. Wish me luck!

I think overall if you are just doing lambdas, Netlify is the way to go. No administration is easier!

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect