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
asked a year ago550 views
1 Answer
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
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions