AWS Lambda aws.ec2()不返回值(NodeJS) [英] AWS Lambda aws.ec2() doesn't return values (NodeJS)

查看:116
本文介绍了AWS Lambda aws.ec2()不返回值(NodeJS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lambda函数,可以在EC2中创建快照.该函数有效,但是没有返回值(即,我想获取该值data.SnapshotId).

I have a Lambda function to create snapshots in EC2. The function works but there is no return value (ie, I want to get this value data.SnapshotId).

用于创建快照的EC2调用嵌套在对s3.getObject的调用内,而在对s3.putObject的调用前.

The EC2 call to create snapshot is nested inside a call to s3.getObject and before a call to s3.putObject.

s3.getObject(params, function(err, data) {
        if (err) {
            console.log(err);
            console.log(message);
            context.fail(message);
        } else {            
            new aws.EC2().createSnapshot(params_snapshot, function(err, data) {
              if (err) console.log(err, err.stack); // an error occurred
              else  {
                  console.log(data.SnapshotId); // this is my concern
              }
            });            
            var params_new = {
                Bucket: bucket,
                Key: key,
                Body: body
            };
            s3.putObject(params_new, function(err, data) {
                        if (err) {
                            console.log(err);
                            console.log(message);
                            context.fail(message);
                        } else {
                            console.log('CONTENT TYPE putObject:', data.ContentType);
                            context.succeed(data.ContentType);
                        }
            });
        }
    });

我最关心的是这里

    new aws.EC2().createSnapshot(params_snapshot, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else  {
          console.log(data.SnapshotId); // this is my concern
      }
    });  

推荐答案

解决方案是我需要将s3.putObject()嵌套在EC2()请求中,因为它们同时发生.

The solution is I need to nest the s3.putObject() inside EC2() request because they happen concurrently.

s3.getObject(params, function(err, data) {
    if (err) {
        console.log(err);
        console.log(message);
        context.fail(message);
    } else {            
        new aws.EC2().createSnapshot(params_snapshot, function(err, data) {
            if (err) console.log(err, err.stack);
            else  {
                console.log(data.SnapshotId);

            var params_new = {
                Bucket: bucket,
                Key: key,
                Body: body
            };
               s3.putObject(params_new, function(err, data) {
                   if (err) {
                       console.log(err);
                       console.log(message);
                       context.fail(message);
                   } else {
                       console.log('CONTENT TYPE putObject:', data.ContentType);
                       context.succeed(data.ContentType);
                   }
            });
            }
        });
    }
});

这篇关于AWS Lambda aws.ec2()不返回值(NodeJS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆