gpt4 book ai didi

amazon-web-services - 无法使用 aws serverless offline 从另一个 lambda 调用 lambda

转载 作者:行者123 更新时间:2023-12-04 13:17:33 25 4
gpt4 key购买 nike

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

{ 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

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

最佳答案

您唯一应该在此处更改的是 lambda 实例配置中的 endpoint 以指向基本 serverless-offline url。此外,您根本不需要使用 serverless-offline-lambda 连接。

将 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 变量。

关于amazon-web-services - 无法使用 aws serverless offline 从另一个 lambda 调用 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com