2

I have 3 webhooks that calls my API Gateway which calls my Lambda Function.

  1. url/webhook/....

I want each webhook to call its own python method

 startDelivery --> def start_delivery(event, context):
 UpdateStatus--> def update_status(event, context):
 EndDelivery--> def end_delivery(event, context):

I understand most likely one method will be executed via "url/webhook" which calls the appropriate python method. def Process_task to call one of the three

What is the ideal way to set up this structure?

Creating different urls for Webhooks and API Gateway captures it and somehow calls the handler?

url/webhook/start
url/webhook/status
url/webhook/end

Sending a different query string for each webhook? and in the lamba parse the query string and call the right python method?

AAA
  • 87
  • 1
  • 1
  • 13

2 Answers2

4

Keep in mind that a Lambda function has one handler (=> 1 invocation = 1 method called).

You can achieve the 1 route <-> 1 method by doing one of the following:

  1. You have a single Lambda function triggered by your 3 APIGW routes. You can then add a simple router to your function which parse the event['path'] and call the appropriate method.
def lambda_handler(event, context):
   path = event['path']

   if path == '/webhook/start':
       return start_delivery(event, context)
   elif path == '/webhook/status':
       return update_status(event, context)
   elif path == '/webhook/end':
       return end_status(event, context)
   else:
       return { "statusCode": 404, "body": "NotFound" }
  1. Create 1 Lambda function by route:
  • webhook/start triggers the StartDelivery Lambda function with start_delivery as handler
  • webhook/status triggers the UpdateDelivery Lambda function with update_delivery as handler
  • webhook/end triggers the EndDelivery Lambda function with end_delivery as handler

You can use Infrastructure as Code (Cloudformation) to easily manage these functions (SAM: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html)

acorbel
  • 1,300
  • 12
  • 14
1

acorbel's answer was helpful for me. The latest version is Format 2.0. Many fields have changed. For example event['path'] doesn't exist with Format 2.0. Please check the below link for correct key names of event structure.

Working with AWS Lambda proxy integrations for HTTP APIs

Velu
  • 53
  • 1
  • 6