3

我想从 serverless-offline 中的另一个 lambda 调用一个 lambda。我想制作一个无服务器的离线 Web 应用程序。但是我收到了这个错误:

12) 在 C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:26:10 处请求。(C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:38:9) 在请求。(C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:685:12) 在 Request.callListeners (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\ aws-sdk\lib\sequential_executor.js:116:18) 在 Request.emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:78:10) 在 Request .emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:683:14) 在 Request.transition (C:\Users\gourabkonar\Desktop\sls-demo\ node_modules\aws-sdk\lib\request.js:22:10) 在 AcceptorStateMachine.runTo (C: \Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:14:12) 在 C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine .js:26:10 请求。(C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:38:9) 在请求。(C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:685:12) 在 Request.callListeners (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\ aws-sdk\lib\sequential_executor.js:116:18) 在 IncomingMessage.onEnd 的 callNextListener (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:96:12) (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\event_listeners.js:307:13) 在 IncomingMessage.emit (events.js:203:15) 在 IncomingMessage.EventEmitter.emit (域.js:448:


sls-api-dev-create

   const jobs=[
    { id: 1,title: 'NodeJS Developer'},
    {id: 2, title:'Angular Developer'}
];


var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';

let lambda = new AWS.Lambda({
    region: 'ap-south-1',
    endpoint: 'http://localhost:3000/jobs'
})
module.exports.handler=async(evt,ctx)=>{

    console.log(evt.body);
    jobs.push(JSON.parse(evt.body));

    lambda.invoke({
        FunctionName: 'sls-api-dev-hello',
        InvocationType: 'Event',
        Payload:null    
    },function(err,data){
        console.log('No error');
        if(!err)
        console.log(data);
        else
        console.log(err);

    })
    //console.log(response);
    return {
        statusCode :200,
        body:JSON.stringify({
            jobs
        })
    }
}

sls-api-dev-你好

'use strict';

module.exports.hello = async event => {
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        message: 'Welcome to Serverless',
        input: event,
      },
      null,
      2
    ),
  };

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

无服务器.yml

service: sls-api



provider:
  name: aws
  runtime: nodejs10.x
  region: ap-south-1
  iamRoleStatements:
    - Effect: Allow
      Action:
        - lambda: InvokeFunction
        - lambda: InvokeAysnc
      Resource: "*"

plugins:
  - serverless-offline
  - serverless-offline-lambda

functions:
  hello:
    handler: handler.hello
  jobs:
    handler: src/jobs/handler.handler
  listJobs:
    handler: src/jobs/list.handler
    events:
      - http:
          method: GET
          path: /jobs
  createJobs:
    handler: src/jobs/create.handler
    environment:
      SOME_VARIABLE: http://localhost:3000/jobs
    events:
      - http:
          method: POST
          path: /jobs
  getJob:
    handler: src/jobs/findOne.handler
    events:
      - http:
          method: GET
          path: /jobs/{id}
          request:
            parameters:
              id: true

请帮助,任何帮助都会很棒,因为我是无服务器的新手。在此先感谢。

4

1 回答 1

4

您应该在此处更改的唯一内容是lambda 实例配置中的端点以指向基本无服务器离线url。此外,您根本不需要使用serverless -offline-lambda pluing。

将 Lambda 选项更改为如下所示:

const lambda = new AWS.Lambda({
  region: "ap-south-1",
  endpoint: process.env.IS_OFFLINE ? "http://localhost:3000" : "https://lambda.ap-south-1.amazonaws.com",
});

此外,这意味着什么

process.env.IS_OFFLINE

? 意味着您使用serverless-offline插件在本地运行 lambda,该插件实际上设置了这个 env 变量。

于 2019-11-08T12:13:40.113 回答