skip to Main Content

I have an Express app on a Lambda function behind APIGateway. I’m able to access the website and https://example.com.com/robots.txt . When I put a Cloudfront distribution in front of API Gateway I can still access the website but not the robots.txt file: https://example.com/robots.txt
How can I make this content-type discoverable when using Cloudfront with API Gateway?

So far, in addition to my default behaviour in my Cloudfront Distribution I’ve created a behaviour for /*.txt with a response header policy and attached it to my distribution. However it still doesn’t allow me to access the robots.txt file on the site.

Behaviour for /*.txt

Behaviour for /*.txt

Response header policy for Content-Type: text/plain

Response header policy for Content-Type: text/plain

Behaviour attached to distribution

Behaviour attached to distribution

2

Answers


  1. This worked for me.

    Create an Origin with API Gateway endpoint, look into the below screenshot.

    Origin:
    Cloudfront Origin

    and In Behaviours

    • Add Path Pattern /*.txt
    • Compress objects automatically: No
    • Viewer protocol policy Redirect HTTP to HTTPS
    • Allowed HTTP methods GET, HEAD
    • Restrict viewer access: No
    • Created and Attached Response headers policy same as yours.
    • Didn’t modify any Additional settings

    Refer to the screenshot below.
    Behavior:
    Cloudfront Behaviour

    With the above config, I’m able to access robots.txt successfully, which is being fetched from Lambda.

    If you’re still getting the issues, then it should be a problem from API Gateway or Lambda, definitely, it’s not from Cloudfront.

    Login or Signup to reply.
  2. To make the content-type of the robots.txt file discoverable when using CloudFront with API Gateway, you need to ensure that the necessary configurations are in place both in CloudFront and API Gateway.

    Here are the steps you can follow:

    1. In the API Gateway console, go to your API and select the specific resource that corresponds to the robots.txt file.

    2. Under the "Integration Response" section, select the appropriate
      HTTP status code (e.g., 200) and click on the "Mapping Templates"
      link.

    3. Add a mapping template for the "Content-Type" header. Use
      the following template code:

      #set($context.responseOverride.header.Content-Type = ‘text/plain’)

    This template sets the "Content-Type" header to "text/plain" for the response.

    1. Deploy the API Gateway changes.

    2. In the CloudFront console, select your distribution and go to the
      "Behaviors" tab.

    3. Create a new behavior by clicking on "Create
      Behavior" or edit the existing behavior for your API Gateway
      integration.

    4. Set the following configurations for the behavior: –
      Path Pattern: /*.txt – Origin or Origin Group: Select your API
      Gateway integration as the origin. – Cache Based on Selected Request
      Headers: Whitelist the "Accept" header.

    5. In the "Response Headers" section of the behavior, add a custom header: – Header Name: Content-Type – Value: Leave it empty (CloudFront will forward the
      value from the API Gateway)

    6. Save the behavior and wait for the
      CloudFront distribution to deploy the changes. By following these
      steps, CloudFront will forward the "Accept" header from the client
      request to API Gateway, and API Gateway will respond with the
      correct "Content-Type" header for the robots.txt file. CloudFront
      will then pass that header along in the response to the client. Make
      sure to clear your CloudFront cache and test accessing the
      robots.txt file again. It should now have the correct "Content-Type"
      header and be accessible through CloudFront.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search