Redirecting HTTP request based on User-Agent -- which service?

Hi all,

I have a single-page AJAX application that is currently is served from S3 with CloudFront in front of it (providing SSL via ACM). Now, I want to redirect some crawlers accessing the site to pretender.io or similar to get the nice SEO goodness as well as the proper Facebook and Twitter sharing meta tags. Given that those crawlers don’t speak JS, how can I redirect based on HTTP User-Agent header within the AWS infrastructure?

  • Obviously I could run Apache or Nginx on a small EC2 instance serving the JS files and doing a redirect, but the whole reason for hosting them on S3/Cloudfront was to stay serverless and not have to worry about multiple servers, patching, etc.

  • As far as I know It is not possible to rewrite requests based on user-agent in S3, the way you can rewrite keys

  • CloundFront won’t route based on User-Agent from what I know

  • An ELB won’t route based on HTTP headers

  • I through of using a WAF since that can parse HTTP headers, but that will only ALLOW or BLOCK a request, not rewrite/redirect it

Any thoughts on an AWS-native way to do this?


Related discussions:

You could have an alias for an API Gateway instead of your CF distribution. Have the gateway forward headers to a lambda that sets the returned location header based on user agent. The gateway will then respond back with a 302 and header location for where you want it to actually go.

You probably have to use API Gateway with lambda except for very limited cases where you could use API Gateway directly.

I have a SPA as well that gets all data from API Gateway. I have API Gateway endpoints that return HTML with substitutions from lambda for open graph tags. It works really well

lambda is expensive and if it is evaluating every request this could cost alot of money.

How is it pricy? Your first million requests per month are free. And even after that it’s only $.20 per million. That’s super cheap IMO.