简体   繁体   English

将nodejs function转换为AWS Lambda兼容

[英]convert nodejs function to AWS Lambda compatible

I am pretty new to both nodejs and AWS Lambda.我对 nodejs 和 AWS Lambda 都很陌生。 I have created a very small nodejs function that is working fine locally.我创建了一个非常小的nodejs function,它在本地运行良好。 Now I need to run it on AWS Lambda, but looks like there are some handlers requirement which I am not understanding completely.现在我需要在 AWS Lambda 上运行它,但看起来有一些我不完全理解的处理程序要求。

Below is my nodejs function that I need to run it on Lambda.下面是我需要在 Lambda 上运行的 nodejs function。 Any idea what changes do I need to make to execute it on AWS?知道我需要进行哪些更改才能在 AWS 上执行它吗? Thanks谢谢

(async function () {
DOMAIN = "abc.xyz.com";
KEY = "***";
const mailchimpClient = require("@mailchimp/mailchimp_transactional")(KEY);
    const run = async () => {
          const response = await mailchimpClient.senders.addDomain({
            domain: DOMAIN,
          });
          console.log(response);
        };
        
        run();
})();

Basically you just need to export a function with a specific name: handler基本上你只需要导出一个具有特定名称的 function: handler

exports.handler =  async function(event, context) {
  console.log("EVENT: \n" + JSON.stringify(event, null, 2))
  return "foo.bar"
}

In this handler, you just need to return something to mark as success or throw an error to mark as failure.在这个处理程序中,你只需要返回一些东西来标记为成功或抛出一个错误来标记为失败。

In your case, this should work:在您的情况下,这应该有效:

var DOMAIN = "abc.xyz.com";
var KEY = "***";
const mailchimpClient = require("@mailchimp/mailchimp_transactional")(KEY);

exports.handler =  async function(event, context) {
  const response = await mailchimpClient.senders.addDomain({
    domain: DOMAIN,
  });
  console.log(response);
  return "success"
}

Here more examples and advanced configurations:这里有更多示例和高级配置:

It depends if you are using any framework to create your aws serverless code.这取决于您是否使用任何框架来创建您的 aws 无服务器代码。

However, your usual code would be somthing like this.但是,您通常的代码将是这样的。

    exports.handler = function(event, context) {
      console.log('Lambda A Received event:', JSON.stringify(event, null, 2));
      context.succeed('Hello ' + event.name);
    };

If you want a easier way to work with AWS serverless code such as Lambdas look at arc.codes如果您想要更轻松地使用 AWS 无服务器代码(例如 Lambdas),请查看arc.codes

Also, here is a link to the AWS docs https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html此外,这里是 AWS 文档https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM