We are considering configuring Lambda to send requests from EventBridge and APIGateway.
In that case, I do not know how to determine the process from APIGateway in the Lambda process.
I have looked into using custom headers, etc., but since I have set the use of Lambda proxy integration to True, I am thinking in the direction of not being able to use custom headers.
2
Answers
Your Lambda handler code can check the event payload to determine the sending service. The Lambda event objects for EventBridge and API Gateway have different formats. For instance, a payload with a
rawPath
key is from API Gateway. Adetail
key in the payload means EventBridge sent it.When an AWS Lambda function is triggered by an AWS service, it receives an event object with information about the source event. The structure and content of this event object differ based on the service that triggers the Lambda function. By inspecting this event object, you can determine which service invoked the Lambda function.
Here’s a simple way to determine if the Lambda was triggered by API Gateway or EventBridge (formerly known as CloudWatch Events):
This code examines the
event
object:For an API Gateway trigger, the event object usually contains a
requestContext
key which contains anapiId
key, among other information.For an EventBridge trigger, the event object should contain a
source
key with the valueaws.events
.By examining these keys and their associated values, you can determine the triggering service.