What exactly happens when a lambda is invoked locally?

I’m curious about what’s actually happening when using sls invoke local

I’ve noticed that in order to successfully invoke functions locally I have to deploy my service at least once before hand, however the execution environment is local.

I’m hoping someone can explain this behavior / what is actually happening when invoking locally. As a note, I’m accessing DynamoDB in my functions.

Thanks a ton everyone.

Invoking functions locally with Serverless just provides a wrapper to your functions which loads variables provided by the serverless.yml file and sets your IAM permissions using the profile you set. So if you create environment variables in your config, Serverless will load those and make them available to the actual handlers that you are running. The reason why you had to deploy first, is because you are using DynamoDB. In order to connect to DynamoDB tables, they must be created first. On your first deploy those tables would be created in your AWS account. Then when you run your functions locally, it will actually access those remote DynamoDB tables.

AWS created a downloadable version of DynamoDB that you can run locally: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html. It is fine to test your functions against a remote DynamoDB table if you have Internet access. If you want to be able to test the entire app locally (without Internet access), you could use the link above to create your own local DynamoDB instance.

Hope that helps,

Thanks for the detailed response, can you provide an example of how I can specify local lambda invocations to access my local DynamoDB?

Follow the instructions here: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html

Then you just use the “endpoint” option when using the SDK and specify your local host on the correct port: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB.html

This SDK above is for node. But each language specific version has the same functionality.

The response given by @jeremydaly covers it all, but I just wanted to mention the serverless-dynamodb-local plugin (which appears to use the aforementioned downloadable version of DynamoDB provided by AWS).

1 Like