AWS::Lambda serverless invoke local function not reflecting my new wcode

时间:2018-10-02 09:17:12

标签: node.js amazon-web-services aws-lambda amazon-ses

Not sure what is happening with my code. It never executes my updated code on my local. If I update my code and run sls invoke local , it still runs the old code. ALso, it does not send out SES EMail.

For some reason, it always executes the code which was already deployed to AWS platform rather than executing my local code. This is confusing.

Below is my 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

I am not sure if I should continue to edit code in AWS web console itself or try setting up local dev environment. Been tying since last two days, but turning out to be useless to spend time.

    '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 };
    };                                                                                                    

1 个答案:

答案 0 :(得分:0)

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