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
질문됨 일 년 전568회 조회
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
답변함 일 년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인