node.js - AWS::Lambda 无服务器调用本地函数不反映我的新 wcode
问题描述
不确定我的代码发生了什么。它永远不会在我的本地执行我更新的代码。如果我更新我的代码并运行 sls invoke local ,它仍然运行旧代码。此外,它不会发送 SES 电子邮件。
出于某种原因,它总是执行已经部署到 AWS 平台的代码,而不是执行我的本地代码。这令人困惑。
下面是我的 serverless.yml:
service: lambda-test
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: ap-southeast-1
iamRoleStatements:
- Effect: Allow
Action:
- lambda:InvokeFunction
- lambda:InvokeAsync
Resource: "*"
functions:
hello:
handler: handler.hello
environment:
events:
- http:
path: /
method: get
trulyYours:
handler: handler.trulyYours
environment:
events:
- http:
path: /trulyYours
method: get
sendRegistrationEmail:
handler: handler.sendRegistrationEmail
environment:
events:
- http:
path: /sendRegistrationEmail
method: get
plugins:
- serverless-offline
我不确定是否应该继续在 AWS Web 控制台本身中编辑代码或尝试设置本地开发环境。从前两天开始一直在捆绑,但结果是花时间没用。
'use strict';
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();
module.exports.hello = async (event, context) => {
console.log("executing lambda function 'hello' XX...");
// return {
// statusCode: 200,
// body: JSON.stringify({
// message: 'v1.0',
// }),
// };
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
//
exports.trulyYours = async (event, context, callback) => {
console.log('Lambda trulyYours Received event:', JSON.stringify(event, null, 2));
//context.succeed('Hello from' + event.name);
return {
statusCode: 200,
body: JSON.stringify({
message: 'hello from trulyYours' + event.name,
}),
};
}
/*function trulyYours (foo, bar) {
// MyLambdaFunction logic here
}*/
module.exports.sendRegistrationEmail = (event, context) => {
console.log("Executing sendRegistrationEmail...");
var lambda = new aws.Lambda({
region: 'ap-southeast-1' //change to your region
});
var params = {
FunctionName: 'lambda-test-dev-trulyYours', // the lambda function we are going to invoke
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: '{ "name" : "Alexa" }'
};
lambda.invoke(params, function (err, data) {
if (err) {
context.fail(err);
} else if (data.Payload) {
context.succeed('Lambda trulyYours ' + data.Payload);
}
});
//
// return {
// statusCode: 200,
// body: JSON.stringify({
// message: 'sent email successfully',
// }),
// };
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
解决方案
尝试创建一个 serverless-local.yml,例如:
config:
region: eu-west-1
environment:
VAR: local
database:
hostname: localhost
port: 8000
username: root
password: toor
database: db
url: http://localhost:3000
并在提供者的 serverless.yml 中添加以下行:
stage: ${opt:stage, 'dev'}
然后在你的终端尝试这个cli:
sls invoke local -f functionName -s local
推荐阅读
- javascript - app.js:6 Uncaught TypeError: Cannot read property 'addEventListener' of null
- python - Django ValueError:字段“id”需要一个数字但得到“S”
- spartacus-storefront - Spartacus Angular 搜索框建议不适用于 iPad 设备
- java - Java.net.SocketException:连接或出站已关闭
- java - 在意图android中出现错误有人尝试解决这个错误并帮助我,检查SS,我尝试了三种方法来解决但没有用
- python - 如何在不换行的情况下打印新值?(计时器)
- azure-cosmosdb - 请帮助解决将 TTL 插入 Comsos DB 的问题
- c - Intel-TSX:为什么 rtm 表现得很奇怪?
- r - R-从 PurpleAir 传感器读取 csv 文件并跳过某些行末尾包含非标准字符的错误位置
- api - 无法使用 Vue 和 Axios 从 API 获取数据