[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:
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 }