[AWS - DA] Lambda Demo: Multi-headers value with ALB

1. Create a Lambda function, because we want to work with Application Load Balancer, we need to give a different response from Lambda

Check: https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html

复制代码
exports.handler = async (event) => {
    console.log('event', event);
    // TODO implement
    const response = {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": false,
        "headers": {
            "Content-Type": "text/html"
        },
        "body": "<h1>Hello from Lambda!</h1>"
    };
    return response;
};
复制代码

 

2. Create "Target Groups", because Lambda must be inside a Target Group in order to work with ALB.

 

3. After Target Group becomes `Active`, we can open DNS name in broswer and we should see `Hello from Lambda!` in the page

 

4. Enable "Multi-values header" in Target Group

 

5. in browser, we can enable url like `http://myalb-labmda-xxxxxx.us-east-1.elb.amazonaws.com/?name=foo&name=bar`

Noice that it downs the response, but actually we don't want that, we need to modify Lambda response according to:

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#multi-value-headers-response

复制代码
exports.handler = async (event) => {
    console.log('event', event);
    // TODO implement
    const response = {
        "statusCode": 200,
        "statusDescription": "200 OK",
        "isBase64Encoded": false,
        "headers": {
            "Content-Type": "text/html"
        },
        "multiValueHeaders": {
              "Set-cookie": ["cookie-name=cookie-value;Domain=myweb.com;Secure;HttpOnly","cookie-name=cookie-value;Expires=May 8, 2019"],
              "Content-Type": ["text/html"]
        },
        "body": "<h1>Hello from Lambda!</h1>"
    };
    return response;
};
复制代码

 

6. Check from CloudWatch:

复制代码
2021-06-03T05:43:17.696Z    5f01fe7f-b7bb-4906-8af7-77579b189a55    INFO    event {
  requestContext: {
    elb: {
      targetGroupArn: 'arn:aws:elasticloadbalancing:us-east-1:xxxxxxxx:targetgroup/myTG-ALB/03b5bf1b78f9e5ac'
    }
  },
  httpMethod: 'GET',
  path: '/',
  multiValueQueryStringParameters: { name: [ 'foo', 'bar' ] },
  multiValueHeaders: {
    accept: [
      'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'
    ],
    'accept-encoding': [ 'gzip, deflate' ],
    'accept-language': [ 'en-GB,en;q=0.9' ],
....
}, body: '', isBase64Encoded: false }
复制代码

 

posted @   Zhentiw  阅读(124)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-06-03 [CSS] Resest CSS
2020-06-03 [Python] Making a package
2019-06-03 [RxJS] RxJS Advanced Patterns Operate Heavily Dynamic UIs
2016-06-03 [Redux] Supplying the Initial State
2015-06-03 [D3] 6. Color Scale
2015-06-03 [D3] 5 .rangeBands
2015-06-03 [D3] 4. d3.max
点击右上角即可分享
微信分享提示