【问题标题】:AWS Lambda aws.ec2() doesn't return values (NodeJS)AWS Lambda aws.ec2() 不返回值 (NodeJS)
【发布时间】:2015-12-06 02:35:58
【问题描述】:

我有一个 Lambda 函数来在 EC2 中创建快照。函数有效但没有返回值(即我想得到这个值data.SnapshotId)。

创建快照的 EC2 调用嵌套在对 s3.getObject 的调用中和对 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
      }
    });  

【问题讨论】:

  • 您有问题吗?
  • 是的,为什么我没有收到来自 aws.EC2() 的回调值
  • data.SnapshotId 没有被记录到控制台吗?
  • 是的,它不是,data.SnapshotId 只是data 的一个子集。我认为这是异步的问题,但我对 NodeJS 或 JS 并不熟悉
  • params_snapshot 在哪里定义?此外,您的每个回调函数都在形参中定义了一个 data 变量,这意味着在每个回调中您将失去对前一个 data 对象的引用。

标签: node.js amazon-web-services aws-lambda


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 1970-01-01
      • 2014-09-13
      • 2020-03-16
      • 2020-06-14
      • 2020-01-03
      • 2022-10-14
      • 2018-10-04
      • 1970-01-01
      相关资源
      最近更新 更多