首页 > 解决方案 > 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 };
    };                                                                                                    

标签: node.jsamazon-web-servicesaws-lambdaamazon-ses

解决方案


尝试创建一个 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


推荐阅读