我编写了以下代码以用于AWS Lambda函数。目的是
整个过程运行良好,但是一旦我将此代码推送到AWS Lambda,它就会失败。它能够获取数据,但不能在S3上创建文件。这是完整的代码:
"use strict";
var express = require('express');
var app = express();
var config = require('./config/dev');
var sql = require("mssql");
var AWS = require('aws-sdk');
var PromiseFtp = require('promise-ftp');
var fs = require('fs');
exports.handler = (event, context, callback) => {
CreateFileOnS3("This is a sample text file created by lambda function", event, context, callback);
};
function CreateFileOnS3(fileData, event, context, callback) {
const fileName = generateFileName();
console.log('Sending file to S3...');
const s3 = new AWS.S3(config.awsCredentials);
const params = {
Bucket: config.app.s3Bucket,
Key: fileName,
Body: fileData
};
s3.upload(params, function (s3Err, data) {
if (s3Err) {
console.log('There was an error creating file on S3');
callback(true, 'There was an error creating file on S3');
}
else {
console.log(`File uploaded successfully at ${data.Location}`);
callback(null, 'File uploaded successfully at ${data.Location}');
}
});
}
function generateFileName() {
var _d = new Date(),
y = _d.getFullYear(),
m = _d.getMonth() + 1,
d = _d.getDate();
return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + '.txt';
}
这是CloudWatch日志所说的:
10:38:51
START RequestId: 0965c2ef-94a2-439e-b61b-83f17b5ad3d3 Version: $LATEST
10:38:51
2019-04-22T10:38:51.737Z 0965c2ef-94a2-439e-b61b-83f17b5ad3d3
Fetching data from database...
10:38:53
2019-04-22T10:38:53.364Z 0965c2ef-94a2-439e-b61b-83f17b5ad3d3
Sending file to S3...
10:39:21
END RequestId: 0965c2ef-94a2-439e-b61b-83f17b5ad3d3
10:39:21
REPORT RequestId: 0965c2ef-94a2-439e-b61b-83f17b5ad3d3 Duration: 30030.33 ms Billed Duration: 30000 ms Memory Size: 512 MB Max Memory Used: 95 MB
10:39:21
2019-04-22T10:39:21.760Z 0965c2ef-94a2-439e-b61b-83f17b5ad3d3 Task timed out after 30.03 seconds
内存:2048MB超时:60秒
编辑:更新后的代码仅将要创建的字符串作为文本文件发送到S3上,但该字符串也不起作用。
答案 0 :(得分:0)
检查在lambda脚本上设置的超时。将其增加到2分钟。