【问题标题】:AWS::Lambda serverless invoke local function not reflecting my new wcodeAWS::Lambda 无服务器调用本地函数不反映我的新 wcode
【发布时间】:2019-03-07 09:49:42
【问题描述】:

不确定我的代码发生了什么。 它永远不会在我的本地执行我更新的代码。如果我更新我的代码并运行 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.js amazon-web-services aws-lambda amazon-ses


    【解决方案1】:

    尝试创建一个 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 的 provider 中添加这一行:

    stage: ${opt:stage, 'dev'}
    

    然后在你的终端试试这个 cli:

    sls invoke local -f functionName -s local

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 2019-02-24
      • 2015-05-20
      • 1970-01-01
      • 2018-01-21
      • 2019-01-21
      • 1970-01-01
      • 2021-01-07
      • 2017-10-07
      相关资源
      最近更新 更多