[AWS

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[AWS相关的知识,希望对你有一定的参考价值。

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 DNA 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 }

 

以上是关于[AWS的主要内容,如果未能解决你的问题,请参考以下文章

AWS S3工具类

DynamoDB 流 和 AWS Lambda 触发器

Flink`textInputFormat`不处理来自aws S3`Source`文件系统的GZ压缩文件

SBT找不到hadoop-aws 3.1.1

如何为 AWS::ElasticLoadBalancingV2::Listener 设置多个证书

使用 AWS MediaTailor 替换 VOD 广告