AWS 使用 Node.js SDK 调用本地 Lambda 端点

AWS invoke local Lambda Endpoint with Node.js SDK

SAM documentation 中显示了部署您自己的 lambda 端点并使用 Python SDK 调用它的可能性。

您只需使用 sam local start-lambda 启动本地 lambda 端点,然后继续

# 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")

我现在的问题是,如何使用 Javascript SDK 做完全相同的事情?我总是收到有关缺少区域、未找到主机和不支持参数的不同错误。 你有解决方案吗?

AWS JavaScript SDK 需要区域和凭据才能发出请求。但对于本地端点,您可以使用任意值。

以下示例适用于我:

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);
});