简体   繁体   English

在来自 AWS 的相同 lambda 代码中运行 lambda 函数

[英]Run lambda functions in the same lambda code from AWS

I have the following lambda function which is working fine to start the databricks cluster, when invoked.我有以下 lambda 函数,它在调用时可以正常启动 databricks 集群。 Now, I would like to add another lambda function and run it in sequence say after 60 seconds of interval.现在,我想添加另一个 lambda 函数并在间隔 60 秒后按顺序运行它。 I tried it by listing both lambda functions one after the other, but only the last one was executed, and the job failed since the cluster was in TERMINATED state.我通过一个接一个地列出两个 lambda 函数来尝试它,但只执行了最后一个,并且由于集群处于 TERMINATED 状态,所以作业失败了。 Can someone please help me in running the job, after the cluster is STARTED.集群启动后,有人可以帮我运行作业吗?

Lambda for STARTING databricks cluster:用于 STARTING databricks 集群的 Lambda:

const https = require("https");   
var tokenstr = "token:xxxxxxxxaaaaaabbbbbccccccc";

exports.handler = (event, context, callback) => 
{
     var data = JSON.stringify({
        "cluster_id": "2222-111000-123abcde"
      });

         var start_cluster_options = {
             host: "aaa.cloud.databricks.com",
             port: 443,
             path: "/api/2.0/clusters/start",
             method: "POST",
             // authentication headers
             headers: {
              "Authorization": "Basic " + new Buffer(tokenstr).toString("base64"),
              "Content-Type": "application/json",
              "Content-Length": Buffer.byteLength(data)
             }
          };

          var request = https.request(start_cluster_options, function(res){
            var body = "";

            res.on("data", function(data) {
              body += data;
            });

            res.on("end", function() {
              console.log(body);
            });

            res.on("error", function(e) {
              console.log("Got error: " + e.message);
            });

          });

      request.write(data);
      request.end();
    };

Function to run the databricks job from lambda:从 lambda 运行数据块作业的函数:

exports.handler = (event, context, callback) => {
     var data = JSON.stringify({
   "job_id": 11111
   });

var run_job_options = {
    host: "aaa.cloud.databricks.com",
      port: 443,
      path: "/api/2.0/jobs/run-now",
      method: "POST",
      // authentication headers
      headers: {
       "Authorization": "Basic " + new Buffer(tokenstr).toString("base64"),
       "Content-Type": "application/json",
       "Content-Length": Buffer.byteLength(data)
     }
   };

   var request = https.request(run_job_options, function(res){
     var body = "";

     res.on("data", function(data) {
       body += data;
     });

I would like to have both START / RUN_JOB in the same lambda functions, if its not the better approach, please help me, am new to LAMBDA invocations.我想在同一个 lambda 函数中同时使用 START / RUN_JOB,如果它不是更好的方法,请帮助我,我是 LAMBDA 调用的新手。

UPDATE:更新:

I have modified my code as suggested by @Dudemullet, and getting an error message " 2018-08-15T22:28:14.446Z 7dfe42ff-a0da-11e8-9e71-f77e93d8a2f8 Task timed out after 3.00 seconds ", not sure, what am I doing wrong, please help.我已按照@Dudemullet 的建议修改了我的代码,并收到一条错误消息“ 2018-08-15T22:28:14.446Z 7dfe42ff-a0da-11e8-9e71-f77e93d8a2f8 任务在 3.00 秒后超时”,我不确定是什么做错了,请帮忙。

const https = require("https");
var tokenstr = "token:xxxxxxxxaaaaaabbbbbccccccc";

 var data = JSON.stringify({
    "cluster_id": "2222-111000-123abcde"
  });

 var data2 = JSON.stringify({
   "job_id": 11111
 });

  var start_cluster_options = {
     host: "aaa.cloud.databricks.com",
     port: 443,
     path: "/api/2.0/clusters/start",
     method: "POST",
     // authentication headers
     headers: {
      "Authorization": "Basic " + new Buffer(tokenstr).toString("base64"),
      "Content-Type": "application/json",
      "Content-Length": Buffer.byteLength(data)
     }
  };

 var run_job_options = {
     host: "aaa.cloud.databricks.com",
     port: 443,
     path: "/api/2.0/jobs/run-now",
     method: "POST",
     // authentication headers
     headers: {
      "Authorization": "Basic " + new Buffer(tokenstr).toString("base64"),
      "Content-Type": "application/json",
      "Content-Length": Buffer.byteLength(data2)
    }
  };

exports.handler = (event, context, callback) => 
{
   https.request(start_cluster_options, function(res){});
   setTimeout(() => {
    https.request(run_job_options, function(res){});
    callback(); // notify lambda everything is complete
    }, 60);
};

I do lambda functions in python, but this function, I am extending from a sample, so I'm not sure on node.js coding.我在 python 中执行 lambda 函数,但是这个函数是从一个示例扩展而来的,所以我不确定 node.js 编码。

****** END OF UPDATE ****** ****** 更新结束 ******

Ideally I would like to have it within the AWS lambda, not going into AWS Step functions, etc.理想情况下,我希望将它包含在 AWS lambda 中,而不是进入 AWS Step 函数等。

Thanks谢谢

You can do this with AWS Step Functions .您可以使用AWS Step Functions做到这一点。 It is basically like a workflow.它基本上就像一个工作流程。

At a high level, this is what you may want to do:概括地说,这就是您可能想要执行的操作:

1) Run your lambda to start the cluster and return cluster id or something.
2) Check cluster status every 10 seconds.
3) If the cluster is up, execute `submit job` lambda function.

Lets say you have this abstracted down to two functions.假设您将其抽象为两个函数。

startServer and runJob startServerrunJob

Your lambda will run until you call the callback or the execution time (TTL) has expired.您的 lambda 将一直运行,直到您调用回调或执行时间 (TTL) 已过期。 So you could write code that looked like this.所以你可以编写看起来像这样的代码。

exports.handler = (event, context, callback) => {

  https.request(start_cluster_options, function (res) {

    setTimeout(() => {
      https.request(run_job_options, function (res) {

        callback();

      });
    }, 60);

  });
};

Another easy way of doing this is with SQS.另一个简单的方法是使用 SQS。 Lambdas can now use SQS as an event source. Lambda 现在可以使用 SQS 作为事件源。 So you could create a message in an SQS queue and set its visibility timeout to whatever time you need.因此,您可以在 SQS 队列中创建一条消息,并将其可见性超时设置为您需要的任何时间。 Sqs visibility timeout Sqs 可见性超时

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

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