Skip to content

Instantly share code, notes, and snippets.

@helephant
Created October 7, 2019 11:24
Show Gist options
  • Save helephant/31038a037581dc59bf648c97bda2b5f0 to your computer and use it in GitHub Desktop.
Save helephant/31038a037581dc59bf648c97bda2b5f0 to your computer and use it in GitHub Desktop.

When using AWS SAM it is possible to call your local lambdas using the AWS SDK, just as you would lambdas running in AWS. I found this very useful when building acceptance tests that would work on local as well as against our AWS staging environment.

To do it, just run sam local as a service on a specific port:

sam local start-lambda -p 8035

Then you can run your code which calls the lambda.

const invokeLambda = async (lambdaName, payload) => {
AWS.config = {
region: 'eu-west-1',
lambda: { endpoint: http://127.0.0.1:8035 }, // whatever port you're running SAM on
};
const lambda = new AWS.Lambda();
return lambda
.invoke({
FunctionName: lambdaName,
InvocationType: 'RequestResponse',
Payload: JSON.stringify(payload),
})
.promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment