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
Response header policy for Content-Type: text/plain
Behaviour attached to distribution
2
Answers
This worked for me.
Create an Origin with API Gateway endpoint, look into the below screenshot.
Origin:
and In Behaviours
/*.txt
Redirect HTTP to HTTPS
GET, HEAD
Refer to the screenshot below.
Behavior:
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.
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:
In the API Gateway console, go to your API and select the specific resource that corresponds to the robots.txt file.
Under the "Integration Response" section, select the appropriate
HTTP status code (e.g., 200) and click on the "Mapping Templates"
link.
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.
Deploy the API Gateway changes.
In the CloudFront console, select your distribution and go to the
"Behaviors" tab.
Create a new behavior by clicking on "Create
Behavior" or edit the existing behavior for your API Gateway
integration.
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.
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)
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.