Search code examples
amazon-web-servicesamazon-s3aws-lambdaamazon-cloudfrontaws-lambda-edge

AWS CloudFront to S3 Post Support?


A third party program is needing to access static files from our CDN. The issue is, instead of fetching these files via GET, they are making this request via POST.

When testing this POST => cdn.company.com/somefile, I get-

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>MethodNotAllowed</Code>
    <Message>The specified method is not allowed against this resource.</Message>
    <Method>POST</Method>
    <ResourceType>OBJECT</ResourceType>
</Error>

The CloudFront behavior is set to support all methods-

cache behavior

And the CORS configuration includes the POST method-

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <MaxAgeSeconds>2592000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

Not sure what else needs to be done. My suspicion is that S3 (correctly) assumes a POST is trying to add information to the bucket, where it should just return the file at the path. Is this possible with cloudfront to s3? Do I need to forward the request to a lambda which will download the file instead?


Solution

  • S3 only supports GET and HEAD so you can't send a POST request (see related).

    You can write a lambda Edge function that replaces the request POST method with GET before sending it to the origin (s3). No need to download the file locally.