AWS Lambda :: 如何在我的本地 ubuntu 机器上测试我的代码?
AWS Lambda :: How to test my code on my local ubuntu machine?
我有这个由 SQS 调用的 Lambda 函数代码。
SQS 触发了我的 Lambda 函数(在 nodeJS 中)。
Lambda 还将发送一封 SES 电子邮件。有没有一种方法可以让我在本地 Ubuntu 上进行测试,而不是总是使用 AWS Web 控制台?
感谢任何帮助。
这是我的 Lambda NodeJS 代码:此代码仅适用于 AWS Lambda。当我 运行
$node index.js ,它不发送 SES 电子邮件。
var aws = require("aws-sdk");
var nodemailer = require("nodemailer");
aws.config.loadFromPath('aws_config.json');
var ses = new aws.SES();
var s3 = new aws.S3();
// Set the region
aws.config.update({region: 'us-west-2'});
exports.handler = function (event, context, callback) {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'SQS event processed.',
input: event,
}),
};
console.log('event: ', JSON.stringify(event.Records));
result = JSON.stringify(event.Records)
result = result.replace(/(^\[)/, '');
result = result.replace(/(\]$)/, '');
var resultObj = JSON.parse(result);
var idCustomer = resultObj.body;
console.log('===SENDING EMAIL====');
// Create sendEmail paramssd
var params = {
Destination: {
/* required */
CcAddresses: [
'XXXXX@gmail.com',
/* more items */
]
},
Message: {
/* required s*/
Body: {
/* required */
Html: {
Charset: "UTF-8",
Data: "BODY:"
},
Text: {
Charset: "UTF-8",
Data: "TEXT_FORMAT_BODY"
}
},
Subject: {
Charset: 'UTF-8',
Data: idCustomer
}
},
Source: 'xxxx@eeeee.com', /* required */
ReplyToAddresses: [
'wwwwww@wwwwwwwww.com',
/* more items */
],
};
// Create the promise and SES service object
var sendPromise = new aws.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states s
sendPromise.then(
function (data) {
console.log("Successfully sent using SES");
console.log(data.MessageId);
}).catch(
function (err) {
console.log("An Error occured while senting using using SES");
console.error(err, err.stack);
});
};
你一定要看看 SAM LOCAL。是AWS团队开发的专门用于测试lambdas的工具
https://github.com/awslabs/aws-sam-cli
Publishes a version of your function from the current snapshot of
$LATEST. That is, AWS Lambda takes a snapshot of the function code and
configuration information from $LATEST and publishes a new version.
The code and configuration cannot be modified after publication. For
information about the versioning feature, see
它很容易使用,你只需要输入
sam local invoke --event event.json
在幕后,它将 运行 为您的 lambda 提供一个 docker 容器并调用它。
关于您的 SES,您应该在代码中放置一个小的 if(SAM_LOCAL) 条件,并且仅当不在本地模式下时才调用真正的条件。请注意,SAM_LOCAL 是 SAM LOCAL 工具在您 运行 本地函数时设置的环境变量。
祝你好运!
如果您想使用 aws 作为后端 - 无服务器框架可能就是您要找的 https://serverless.com/ If you want to test your code without executing lambda on aws backend take a look at localastack framework https://github.com/localstack/localstack
我有这个由 SQS 调用的 Lambda 函数代码。 SQS 触发了我的 Lambda 函数(在 nodeJS 中)。
Lambda 还将发送一封 SES 电子邮件。有没有一种方法可以让我在本地 Ubuntu 上进行测试,而不是总是使用 AWS Web 控制台? 感谢任何帮助。
这是我的 Lambda NodeJS 代码:此代码仅适用于 AWS Lambda。当我 运行 $node index.js ,它不发送 SES 电子邮件。
var aws = require("aws-sdk");
var nodemailer = require("nodemailer");
aws.config.loadFromPath('aws_config.json');
var ses = new aws.SES();
var s3 = new aws.S3();
// Set the region
aws.config.update({region: 'us-west-2'});
exports.handler = function (event, context, callback) {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'SQS event processed.',
input: event,
}),
};
console.log('event: ', JSON.stringify(event.Records));
result = JSON.stringify(event.Records)
result = result.replace(/(^\[)/, '');
result = result.replace(/(\]$)/, '');
var resultObj = JSON.parse(result);
var idCustomer = resultObj.body;
console.log('===SENDING EMAIL====');
// Create sendEmail paramssd
var params = {
Destination: {
/* required */
CcAddresses: [
'XXXXX@gmail.com',
/* more items */
]
},
Message: {
/* required s*/
Body: {
/* required */
Html: {
Charset: "UTF-8",
Data: "BODY:"
},
Text: {
Charset: "UTF-8",
Data: "TEXT_FORMAT_BODY"
}
},
Subject: {
Charset: 'UTF-8',
Data: idCustomer
}
},
Source: 'xxxx@eeeee.com', /* required */
ReplyToAddresses: [
'wwwwww@wwwwwwwww.com',
/* more items */
],
};
// Create the promise and SES service object
var sendPromise = new aws.SES({apiVersion: '2010-12-01'}).sendEmail(params).promise();
// Handle promise's fulfilled/rejected states s
sendPromise.then(
function (data) {
console.log("Successfully sent using SES");
console.log(data.MessageId);
}).catch(
function (err) {
console.log("An Error occured while senting using using SES");
console.error(err, err.stack);
});
};
你一定要看看 SAM LOCAL。是AWS团队开发的专门用于测试lambdas的工具
https://github.com/awslabs/aws-sam-cli
Publishes a version of your function from the current snapshot of $LATEST. That is, AWS Lambda takes a snapshot of the function code and configuration information from $LATEST and publishes a new version. The code and configuration cannot be modified after publication. For information about the versioning feature, see
它很容易使用,你只需要输入
sam local invoke --event event.json
在幕后,它将 运行 为您的 lambda 提供一个 docker 容器并调用它。
关于您的 SES,您应该在代码中放置一个小的 if(SAM_LOCAL) 条件,并且仅当不在本地模式下时才调用真正的条件。请注意,SAM_LOCAL 是 SAM LOCAL 工具在您 运行 本地函数时设置的环境变量。
祝你好运!
如果您想使用 aws 作为后端 - 无服务器框架可能就是您要找的 https://serverless.com/ If you want to test your code without executing lambda on aws backend take a look at localastack framework https://github.com/localstack/localstack