How to configure AWS ALB for a chalice lambda project

323 views Asked by At

I am trying to configure My existing python lambda function which is deployed using chalice. I tried configuring ALB with lambda but it seems ALB is unable to communicate Chalice deployed projects. It seems the Chalice structure that we use for API Gateway cannot be used for ALB. Is there any way to configure the existing Chalice project to respond to ALB without much code change?

1

There are 1 answers

0
beryu On

I had the same problem, and I could not find a suitable answer.

So, I decided to define the other lambda project to proxy the connection between ALB and API Gateway.
My architecture is [ALB -> new lambda -> API Gateway -> existing lambda]. "new lambda"'s implements is below.

I think it's not the best solution but the easiest way to achieve the problem.

def lambda_handler(event, context):
    path = event['path'] + '?'
    
    if "queryStringParameters" in event.keys():
        dict = event['queryStringParameters']
        for key in dict:
            path += key + '=' + dict[key] + '&'
    if event['httpMethod'] == "GET":
        return {
            'statusCode': 301,
            'headers': {
                 'Location': 'https://foo.execute-api.region-name-1.amazonaws.com/bar' + path
            }
        }
    else:
        return {
            'statusCode': 308,
            'headers': {
                 'Location': 'https://foo.execute-api.region-name-1.amazonaws.com/bar' + path
            },
            'body': event['body']
        }