skip to Main Content

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


  1. 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. A detail key in the payload means EventBridge sent it.

    Login or Signup to reply.
  2. 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):

    def lambda_handler(event, context):
    
        # Check if triggered by API Gateway
        if 'requestContext' in event and 'apiId' in event['requestContext']:
            print("Invoked by API Gateway")
            # Your logic for API Gateway trigger
        
        # Check if triggered by EventBridge (CloudWatch Events)
        elif 'source' in event and event['source'] == 'aws.events':
            print("Invoked by EventBridge")
            # Your logic for EventBridge trigger
    
        else:
            print("Invoked by another source")
            # Your logic for other sources
    
        return {
            'statusCode': 200,
            'body': 'Function execution completed!'
        }
    

    This code examines the event object:

    • For an API Gateway trigger, the event object usually contains a requestContext key which contains an apiId key, among other information.

    • For an EventBridge trigger, the event object should contain a source key with the value aws.events.

    By examining these keys and their associated values, you can determine the triggering service.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search