I am using serverless to deploy aws lambdas. Currently my set up is that api gateway triggers one lambda then I async invoke another lambda.
serverless.yml
service: scraper
useDotenv: true #added this because deprecation notice
frameworkVersion: '2'
provider:
name: aws
runtime: nodejs14.x
lambdaHashingVersion: 20201021
stage: prod
region: us-east-2
functions:
scraperRunner:
handler: handler.scraperRunner
provisionedConcurrency: 0
timeout: 90
events:
- http:
path: process/run
method: post
async: true
runTargetSite:
handler: handler.runTargetSite
plugins:
- serverless-offline
then I have a handler.js file where I define both handler functions:
'use strict';
const startScraper = require('./crawler/runner');
const AWS = require('aws-sdk');
AWS.config.region = 'us-east-2';
const lambda = new AWS.Lambda();
function processResponse(msg, statusCode, event) {
return {
statusCode: statusCode,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Content-Type',
"Access-Control-Allow-Methods": "OPTIONS,POST,GET"
},
body: JSON.stringify({
message: msg,
input: event
})
}
}
module.exports.scraperRunner = async (event) => {
if (event) {
const data = event;
if (data.hasOwnProperty('target_id')) {
const prov = [<array of objects>]
for (const p of prov) {
data['target'] = p.name
const params = {
FunctionName: 'provider-scraper-prod-runTargetSite',
InvocationType: 'Event',
// LogType: 'Tail',
Payload: JSON.stringify(data)
};
await lambda.invoke(params).promise()
}
return processResponse(
'Event successfully received',
200, event)
}
}
return processResponse(
'Invalid body payload',
402, event)
};
module.exports.runTargetSite = async function(event, context) {
await startScraper(event)
return processResponse(
'Event successfully received for runTargetSite',
200, event)
}
I've also added the necessary roles (I think):
Using the command line, I'm able to call the first lambda which then calls the second:
serverless invoke local --function scraperRunner --data '{"target": "_tpp_"}'
However when submitting a post request to the API Gateway endpoint, the first lambda (scraperRunner) triggers but the second one is not invoked.