Cloudfront 502 when using an ALB as custom origin via Lambda@Edge

0

Hi there,

I filter requests to have crawlers and bots consuming a dedicated origin. This origin is an express webserver using puppeteer on ECS, behind an ALB.

Calling directly the ALB using public DNS name works - the required content is served as expected.

I use two Lambda@Edge functions to filter the incoming requests:

  • first one at viewer request to "tag" the request by adding a header,
  • second one at origin request to change the origin of the request, from S3 to custom origin (the ALB).

Everything works fine, the lambda are triggered, but the result is a 502 from cloudfront:

<H1>502 ERROR</H1>
	<H2>The request could not be satisfied.</H2>
	<HR noshade size="1px">
	CloudFront wasn't able to connect to the origin.
	We can't connect to the server for this app or website at this time. There might be too much traffic or a
	configuration error. Try again later, or contact the app or website owner.

The origin is changed as in the examples:

   request.origin = {
    custom: {
     domainName: 'XXX.eu-west-3.elb.amazonaws.com',
     port: 3000,
     protocol: 'https',
     path: '',
     // querystring: request.querystring,
     sslProtocols: ["TLSv1", "TLSv1.1", "TLSv1.2"],
     readTimeout: 5,
     keepaliveTimeout: 5,
     customHeaders: {}
    }
   };
   request.headers['host'] = [{ key: 'host', value: 'XXX.eu-west-3.elb.amazonaws.com' }];

As said previously, calling XXX.eu-west-3.elb.amazonaws.com:3000 directly using Postman works perfectly fine.

Am I missing something ?! Thanks for the help,

EDIT: I checked the protocol, added the custom origin in the "Origins" of Cloudfront without change of the result

Vincent
已提問 1 年前檢視次數 567 次
1 個回答
0

You can check why the 502 is the failing which should be one of the following conditions:

SSL/TLS negotiation failure between CloudFront and a custom origin server Origin is not responding with supported ciphers/protocols SSL/TLS certificate on the origin is expired, invalid, self-signed, or the certificate chain is in the wrong order: Origin is not responding on specified ports in origin settings

Reference : https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-502-bad-gateway.html

I would first configure the ELB as Origin in the Cloudfront and route the request to see everything is working and then emulate the Origin request using lambda using the below Event structure for Origin request : https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#example-origin-request

AWS
已回答 1 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南