CloudFront + S3 static website hosting, issues with contact form submission

0

Hi folks,

I have a simple html landing page hosted on AWS S3 and fronted by CloudFront (for https communication). The site contains a Contact form that uses PHP code for the Submit button's logic. Unfortunately I receive the error below when I attempt to submit the form:

<?xml version encoding?>

<Code>MethodNotAllowed</Code> <Message>The specified method is not allowed against this resource.</Message> <Method>POST</Method> <ResourceType>OBJECT</ResourceType>

I have set my CloudFront distribution behavior to allow http methods GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE (as opposed to the default GET, HEAD), however, I am still receiving the error above (405). I also confirmed that us-east-1 (where the S3 bucket lives) supports Signature Versions 2 and 4; as I understand it, some of the newer regions only support version 4, for which the POST method is not supported. I believe that while CloudFront is returning the 405 error, it is S3 that is not liking the POST method, since it supports PUT instead.

Is anyone familiar with this issue and/or the solution? I would like to stay away from creating a restful API + Lambda function to address something that is supposed to be simple, if possible.

Thanks!

1 Answer
1

Hi There,

I understand that you’re noticing a '405 Method not allowed' response after performing a POST request against your Cloudfront-distributed website with S3 Static Website as your origin. You're seeking confirmation on this behavior as you already implemented the POST, PUT, PATCH and DELETE options for your cache behavior.

You're correct to assume that the S3 Static Website only supports GET and HEAD requests; write operations are not supported through the S3 Static Website Endpoint [1].

This would explain why you're experiencing the '405 Method not allowed' requests when attempting to POST through your Cloudfront distribution. You will expect the same results if you are to attempt the POST against the S3 Static Website endpoint as well. If you want to perform POST requests against your S3 origin through your CloudFront distribution, you'll need to use the S3 REST endpoint, other than the S3 Website endpoint [2]. You can identify the S3 REST endpoint as bucket-name.s3.amazonaws.com , whereas the S3 Static Website endpoint includes s3-website-region.amazonaws.com . There are some key differences between the two endpoint types, one of them being that the S3 Website endpoint doesn't support SSL connections (see [3] for additional details).

Please let me know if you have any additional questions or concerns, I'll be happy to assist you. Till then, stay safe

========== Resources ==========

[1] Website endpoints - Key differences between a website endpoint and a REST API endpoint - https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteEndpoints.html#WebsiteRestEndpointDiff 

[2] Using Amazon S3 Origins, MediaPackage Channels, and Custom Origins for Web Distributions - Using Amazon S3 Buckets for Your Origin - https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DownloadDistS3AndCustomOrigins.html#concept_S3Origin 

[3] Website endpoints - Key differences between a website endpoint and a REST API endpoint - https://docs.aws.amazon.com/AmazonS3/latest/userguide/WebsiteEndpoints.html#WebsiteRestEndpointDiff

Please note that I personally value your feedback, please accept this answer if you find it helpful to you.

Mfanelo
answered 2 years ago
  • Edit: thank you very much for your thorough answer. After reading your response and additional documentation, I implemented my hosted landing page a bit differently than as advised by the following document, which is what I originally used: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/getting-started-cloudfront-overview.html#getting-started-cloudformation-create-s3-www-bucket

    1. Instead of pointing my CloudFront distribution to the website endpoint, I ended up using the REST API endpoint. Also, instead of having two distributions (one for the root domain and another for the subdomain), I used a single distribution and specified two CNAMEs within (the root domain CNAME and the subdomain CNAME).
    2. Given the configuration from #1 above, the need to have a second empty S3 root domain bucket redirecting to the S3 subdomain bucket (hosting the landing page) was eliminated, and therefore my solution is using a single subdomain bucket.
    3. To finish off the simpler solution, both Route 53 records (root domain record and subdomain record) point to the same CloudFront distribution (for HTTPS communication).

    One thing that I found very interesting is that it appears that Route 53 does not need a root domain record and knows how to redirect to the subdomain record; with and without the root domain record the solution works. I left it in place just to be thorough.

    Thanks again for your response.

  • Hi There EG83, I am glad you have found the solution to the issue. If you have found my answer helpful in anyway, please accept and give it a thumbs up, I would appreciate that very much, thank you.

  • Done! Thanks again! :)

  • You are most welcome,

    Please do mark the answer as accepted as that can be useful to the next people that search.

  • Interesting, I don't see/have an option to accept your answer. In fact, somehow, when I log in and go into My Dashboard, my own question does not appear under Questions, but I can see my subsequent comments (such as this one) under Comments. Probably a glitch as apparently my own question is not my question. Go figure!

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