AWS Lambda aws.ec2() 没有 return 值 (NodeJS)

AWS Lambda aws.ec2() doesn't return values (NodeJS)

我有一个 Lambda 函数可以在 EC2 中创建快照。该函数有效但没有 return 值(即,我想获得此值 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
      }
    });  

试试这个:

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

解决方案是我需要将 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);
                   }
            });
            }
        });
    }
});