Running AWS Lambda locally in macOS

Vadionline
2 min readDec 28, 2020

Assuming you are familiar with all the jargons of AWS, cloud and Serverless ecosystem, this post is targeted to show how to develop and test your Lambda functions locally.

What you need:

  1. AWS CLI, AWS Account
  2. Docker Engine
  3. VS Code like editor
  4. Some mood to write code!

The easiest way to set up application is through AWS console, I am not comfortable in crazy maze of AWS cloud formation templates. However, if you create an application and a function through AWS console, it creates one for you. So, go for it and don’t waste your time in correcting AWS templates.

First, you need to install and if you already have one, upgrade the sam cli. It needs additional brew manager ‘tap’.

brew tap aws/tap

brew install aws-sam-cli

Or

brew upgrade aws-sam-cli

You also need a working docker engine daemon in your local set up. Don’t forget!

Assuming you have some kind of local editors like VS Code, import the function to your local environment. Note: you may need to set up VS Code with AWS Toolkit extension and authenticated with a user profile.

To run a lambda function locally you need to call this in its simplest form from sam application root directory (where you have placed template.yaml).

sam local invoke

If it complains that it did not find the function, open template.yaml and add run configuration through “Code Lens” actions. You would see “add debug configuration” button within template.yaml.

But to run the lambda function successfully in local environment, you would need gcloud command utility to authorize your account for docker. Bizarre dependency from AWS team that they had to depend on a rival library. Well, if you don’t log in through gcloud, you will receive the following error. I was forced to install gcloud utility.

docker.credentials.errors.InitializationError: docker-credential-gcloud not installed or not available in PATH

After authentication, sam local invoke will run successfully. It will take some time to build docker image for the first time. But it will be a breezing fast on subsequent invocations.

Please let me know, you all had success in your set-ups.

Happy coding!

--

--

Vadionline

Digital Transformation, Product Innovation expert, Cloud Technology enthusiast, IT management practitioner. I enjoy exploring technical stuffs.