无法使用 aws serverless offline 从另一个 lambda 调用 lambda

Unable to invoke a lambda from another lambda using aws serverless offline

我想从 serverless-offline 中的另一个 lambda 调用 lambda。我想制作一个无服务器离线 web application.But 我收到此错误:

{ UnknownError: Unsupported Media Type at Object.extractError (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\protocol\json.js:51:27) at Request.extractError (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\protocol\rest_json.js:55:8) at Request.callListeners (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:106:20) at Request.emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:14:12) at C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:38:9) at Request. (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at Request.emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:78:10) at Request.emit (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:683:14) at Request.transition (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:22:10) at AcceptorStateMachine.runTo (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:14:12) at C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\state_machine.js:26:10 at Request. (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:38:9) at Request. (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\request.js:685:12) at Request.callListeners (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:116:18) at callNextListener (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\sequential_executor.js:96:12) at IncomingMessage.onEnd (C:\Users\gourabkonar\Desktop\sls-demo\node_modules\aws-sdk\lib\event_listeners.js:307:13) at IncomingMessage.emit (events.js:203:15) at IncomingMessage.EventEmitter.emit (domain.js:448:20) at endReadableNT (_stream_readable.js:1129:12) at process._tickCallback (internal/process/next_tick.js:63:19) message: 'Unsupported Media Type', code: 'UnknownError', time: 2019-11-06T17:02:27.641Z, requestId: undefined, statusCode: 415, retryable: false, retryDelay: 6.643720301281952 }


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-hello

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

serverless.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

请帮忙,任何帮助都会很棒,因为我是新手serverless.Thanks。

你唯一应该在这里改变的是你的 lambda 实例配置中的 endpoint 指向基础 serverless-offline 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

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