skip to Main Content

I have an AWS API Gateway (HTTP) with the following routes:

https://<API_HOST>/prod/account/list 
https://<API_HOST>/prod/account/edit
https://<API_HOST>/prod/account/delete

Based on each routing, I want different AWS Lambda functions to execute. What’s the most optimized way to route the traffic at the AWS Lambda function?

The following code works but, it is probably ridiculously lame.

import json

def lambda_handler(event, context):
    
    # Get Path [Routing]
    path = event['path']
    path = path.split("/")

    #e.g for <HOST>/prod/account/list -> Path: ["", "prod", "account", "list"]

    # Condition 1
    if (path[2] == "account" and path[3] == "list"):

        return {
            'statusCode': 200,
            'body': json.dumps("list function")
        }

    # Condition 2
    if (path[2] == "account" and path[3] == "edit"):
        return {
            'statusCode': 200,
            'body': json.dumps("edit function")
        }

    # Condition 3
    if (path[2] == "account" and path[3] == "delete"):
        return {
            'statusCode': 200,
            'body': json.dumps("delete function")
        }

    # All other options
    return {
        'statusCode': 401,
        'body': json.dumps("Invalid Option")
    }

Below, is the original HTTP API request:

{
    "version": "1.0",
    "resource": "/account/list",
    "path": "/prod/account/list",
    "httpMethod": "GET",
    "headers": {
        "Content-Length": "0",
        "Host": "xxxxx",
        "Postman-Token": "7806db7c-8936-4d65",
        "User-Agent": "PostmanRuntime/7.29.2",
        "X-Amzn-Trace-Id": "Root=1-xxx",
        "X-Forwarded-For": "xxx.xxx.xxx.xxx",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br"
    },
    "multiValueHeaders": {
        "Content-Length": ["0"],
        "Host": ["xxxxx.execute-api.eu-west-1.amazonaws.com"],
        "Postman-Token": ["7806db7c-xxx-xxx-ab5c"],
        "User-Agent": ["PostmanRuntime/7.29.2"],
        "X-Amzn-Trace-Id": ["Root=1-xxx"],
        "X-Forwarded-For": ["xxx.xxx.xxx.xxx"],
        "X-Forwarded-Port": ["443"],
        "X-Forwarded-Proto": ["https"],
        "accept": ["*/*"],
        "accept-encoding": ["gzip, deflate, br"]
    },
    "queryStringParameters": null,
    "multiValueQueryStringParameters": null,
    "requestContext": {
        "accountId": "xxx",
        "apiId": "xxx",
        "domainName": "xxx.execute-api.eu-west-1.amazonaws.com",
        "domainPrefix": "xxx",
        "extendedRequestId": "xxx=",
        "httpMethod": "GET",
        "identity": {
            "accessKey": null,
            "accountId": null,
            "caller": null,
            "cognitoAmr": null,
            "cognitoAuthenticationProvider": null,
            "cognitoAuthenticationType": null,
            "cognitoIdentityId": null,
            "cognitoIdentityPoolId": null,
            "principalOrgId": null,
            "sourceIp": "xxx.xxx.xxx.xxx",
            "user": null,
            "userAgent": "PostmanRuntime/7.29.2",
            "userArn": null
        },
        "path": "/prod/account/list",
        "protocol": "HTTP/1.1",
        "requestId": "xxxx=",
        "requestTime": "29/Sep/2022:09:41:01 +0000",
        "requestTimeEpoch": 1664444461700,
        "resourceId": "GET /account/list",
        "resourcePath": "/account/list",
        "stage": "prod"
    },
    "pathParameters": null,
    "stageVariables": null,
    "body": null,
    "isBase64Encoded": false
}

2

Answers


  1. This question might be outdated, but I will add my response in case anyone has the same question. You are using a proxy rousource with a greedy path at the moment in the sense that it appears you are routing all calls from a single API Gateway endpoint using the any method to a single Lambda function, and sorting the path logic there. At least that is what I am gathering based on your question.
    enter image description here

    An alternative method is to have API Gateway handle the logic for you, by creating multiple methods and resources, with each one mapped to a Lambda function.

    enter image description here

    Login or Signup to reply.
  2. Yes in python handler check "path" and then execute logic. so multiple GET go to same function:

    def handler(event, context):
        # Log the event argument for debugging and for use in local development.
        #print(json.dumps(event))
        mystr = json.dumps(event. Get('requestContext',{}).get('path'))
        print ( "Path passed in = " + mystr)
        if mystr == '"/list"':
            print (mystr)
        
        if mystr == '"/edit"':
            print (mystr)
        
        if mystr == '"/delte"':
            print (mystr)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search