简体   繁体   English

如何使 AWS ALB 将请求源发送到 lambda

[英]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. 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. ALB 事件似乎不包含Origin header。 Is there a way to configure ALB to make it forward a request's Origin header?有没有办法配置 ALB 以使其转发请求的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这是到达 Lambda https://docs.aws.amazon.com/lambda/latest/dg/services-alb.ZFC35FDC70D5FC69D269883A822C7A53的 ALB 事件的示例

{
    "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
}

You can configure an ALB to forward requests that contain a specific header.您可以配置 ALB 以转发包含特定 header 的请求。

To do this去做这个

  • Go to Listeners tab in the ALB Go 到 ALB 中的 Listeners 选项卡
  • Click rules for the listener you want to configure要配置的监听器的点击规则
  • Then, choose insert rule, and add condition to forward customer header.然后,选择插入规则,并添加条件转发客户 header。

More details are on this doc .有关此文档的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM