Reputation: 177
I want to invoke a lambda from another lambda inside serverless-offline .I want to make a serverless offline web application.But I am getting this error:
{ 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
Please help ,Any help would be great as I am new to serverless.Thanks in advance.
Upvotes: 3
Views: 3411
Reputation: 56
Only thing you should change here is endpoint in your lambda instance configuration to point to base serverless-offline url. Also you do not need to use serverless-offline-lambda pluing at all.
Change Lambda options to something like this:
const lambda = new AWS.Lambda({
region: "ap-south-1",
endpoint: process.env.IS_OFFLINE ? "http://localhost:3000" : "https://lambda.ap-south-1.amazonaws.com",
});
Additionally, what this means
process.env.IS_OFFLINE
? Means you are running lambda locally using serverless-offline plugin which actually sets this env variable.
Upvotes: 4