Mayor Mayer
Mayor Mayer

Reputation: 362

AWS invoke local Lambda Endpoint with Node.js SDK

in the SAM documentation there is the possibility shown to deploy your own lambda endpoint and call it with the Python SDK.

You just have to startup the local lambda endpoint with sam local start-lambda and then continue with

# USING AWS SDK
 -------------
 #You can also use the AWS SDK in your automated tests to invoke your functions programatically.
 #Here is a Python example:

     self.lambda_client = boto3.client('lambda',
                                  endpoint_url="http://127.0.0.1:3001",
                                  use_ssl=False,
                                  verify=False,
                                 config=Config(signature_version=UNSIGNED,
                                               read_timeout=0,
                                                retries={'max_attempts': 0}))
    self.lambda_client.invoke(FunctionName="HelloWorldFunction")

My question is now, how can i do exactly the same with the Javascript SDK? I always get different errors about missing regions, not found hosts and unsupported parameters. Do you have a solution for me?

Upvotes: 1

Views: 3603

Answers (1)

serge
serge

Reputation: 1114

AWS JavaScript SDK requires region and credentials to make requests. But for local endpoints you can use arbitrary values.

Following example works for me:

const AWS = require('aws-sdk');

const lambda = new AWS.Lambda({
  apiVersion: '2015-03-31',
  endpoint: 'http://127.0.0.1:3001',
  sslEnabled: false,
  region: 'us-east-1',
  accessKeyId: 'any',
  secretAccessKey: 'any'
});

lambda.invoke({
  FunctionName: 'HelloWorldFunction'
}, (err, res) => {
  console.log(res);
});

Upvotes: 5

Related Questions