Jun
Jun

Reputation: 3044

How to make AWS ALB send request origin to lambda

For API Gateway + Lambda, the event that is sent to Lambda contains Origin header which can be used to set the "Access-Control-Allow-Origin" header of the response.

ALB event doesn't seem to contain the Origin header. Is there a way to configure ALB to make it forward a request's Origin header?

This is an example of an ALB event that arrives at Lambda https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

{
    "requestContext": {
        "elb": {
            "targetGroupArn": "arn:aws:elasticloadbalancing:us-east-2:123456789012:targetgroup/lambda-279XGJDqGZ5rsrHC2Fjr/49e9d65c45c6791a"
        }
    },
    "httpMethod": "GET",
    "path": "/lambda",
    "queryStringParameters": {
        "query": "1234ABCD"
    },
    "headers": {
        "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
        "accept-encoding": "gzip",
        "accept-language": "en-US,en;q=0.9",
        "connection": "keep-alive",
        "host": "lambda-alb-123578498.us-east-2.elb.amazonaws.com",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36",
        "x-amzn-trace-id": "Root=1-5c536348-3d683b8b04734faae651f476",
        "x-forwarded-for": "72.12.164.125",
        "x-forwarded-port": "80",
        "x-forwarded-proto": "http",
        "x-imforwards": "20"
    },
    "body": "",
    "isBase64Encoded": false
}

Upvotes: 1

Views: 1053

Answers (1)

choichul
choichul

Reputation: 21

You can configure an ALB to forward requests that contain a specific header.

To do this

  • Go to Listeners tab in the ALB
  • Click rules for the listener you want to configure
  • Then, choose insert rule, and add condition to forward customer header.

More details are on this doc.

Upvotes: 1

Related Questions