Reputation: 789
I am building an on the fly image optimizer using CloudFront, S3, API Gateway, and Lambda, but right now I am still in the process of getting my CloudFront origin to fallback to my s3 custom error path.
Here's the flow I'm looking for:
If I try to access an object that doesn't exist through CloudFront, It correctly follows the origin provided, but if the object doesn't exist in S3, I am given the classic Access Denied
XML response as shown below. My Bucket definitely has the correct write permissions, and has public read access (for the moment)
Here is my CF Origins/Behaviors. Originally I just had the default origin, and one default behavior, which is really all i need, in my fiddling around i added a top level and a deep resizer/*
level.
And here is my s3 Static Website Hosting section and redirection rules
I feel like I shouldn't even need the redirection rules because on error is should redirect to hellowrld.html
(not a typo) which is just a fairly blank test html page currently.
Any help would be greatly appreciated! I know this otf image resizer is a very common use case for firing a Lambda upon not finding an object, but I cant find any examples where they put CloudFront in front of S3 in front of API Gateway/Lambda
Upvotes: 1
Views: 953
Reputation: 422
Seems like you have added wrong http error codes in above Redirection rule. It should be 403 and 307. I have added full redirection rule which you can use :
<RoutingRules>
<RoutingRule>
<Condition>
<KeyPrefixEquals/>
<HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
</Condition>
<Redirect>
<Protocol>https</Protocol>
<HostName>'your_api_gateway_url'</HostName>
<ReplaceKeyPrefixWith>prefix_name'?key=</ReplaceKeyPrefixWith>
<HttpRedirectCode>307</HttpRedirectCode>
</Redirect>
</RoutingRule>
</RoutingRules>
Upvotes: 1