Reputation: 2181
We are trying to follow a micro-server architecture in our approach. We have our front end in our S3 bucket and Apis in the API gateway connected to the Lambda function.
so the request flow would be something similar to this:
S3 -> API -> Lambda -> DB
The concern that I have is, how do I know if my API has triggered a lambda function? there are monitoring options available for lambda but those are post invocation of lambda function.
Is there a way I can know if my lambda function is triggered or not from the API? also send the notification on same?
Upvotes: 1
Views: 373
Reputation: 194
Another approach that easier than cloud trail, When your lambda invoked by APIGW, on the lambda event you have some details you can use match your use cases. Event schema: https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html
For example, NodeJS Lambda:
const handler = async (event, context) => {
if(event["requestContext"] && event["httpMethod"]) {
console.log("This is probably an API-GW event");
} else {
console.log("This is definitely not an API-GW event");
}
};
Another cool way is to use some of the monitoring tools that give you these abilities out of the box: Like this tool
Full disclosure, The tool I just showed called lumigo, the company I work for. This is a great tool and I'm using it for my personal projects also
Upvotes: 1
Reputation: 238497
I checked the CloudTrial's Trial event for lambda invocation on my own API gateway with lambda. It has the form:
{
"eventVersion": "1.07",
"userIdentity": {
"type": "AWSService",
"invokedBy": "apigateway.amazonaws.com"
},
"eventTime": "2020-10-30T12:03:17Z",
"eventSource": "lambda.amazonaws.com",
"eventName": "Invoke",
"awsRegion": "us-east-1",
"sourceIPAddress": "apigateway.amazonaws.com",
"userAgent": "apigateway.amazonaws.com",
"requestParameters": {
"xxxx": "arn:aws:lambda:us-east-1:xxxx:function:fff",
"sourceArn": "arn:aws:execute-api:us-east-1:xxx:84j28c7zga/test/ANY/test"
},
"responseElements": null,
"additionalEventData": {
"functionVersion": "arn:aws:lambda:us-east-1:xxxx:function:fff:$LATEST"
},
"requestID": "bc5f574e-58d8-4a2b-978b-5ec32aba447e",
"eventID": "2345b878-4998-4317-a0c4-1005df40d873",
"readOnly": false,
"resources": [
{
"accountId": "xxxx",
"type": "AWS::Lambda::Function",
"ARN": "arn:aws:lambda:us-east-1:xxx:function:fff"
}
],
"eventType": "AwsApiCall",
"managementEvent": false,
"recipientAccountId": "xxxx",
"sharedEventID": "1906ed81-6835-4046-943d-f2ca9e5b9d40",
"eventCategory": "Data"
}
As you can see above, when the lambda is invoked, you get information that it was API gateway which invoked it:
"userIdentity": {
"type": "AWSService",
"invokedBy": "apigateway.amazonaws.com"
},
Upvotes: 1