I'm working on an amplify react app. From within the app, I poll an S3 bucket that I expect to be populated after a couple of minutes. For clarity the flow would be:
- A user uploads a text file to
S3and the app gets the file url in the response - The app then sends a request to
aws api-gatewaywith theS3 bucketuri, which triggers alambdathat then callsaws textract, which in turn triggers a secondlambdawhich writes to anS3 bucket. The firstlambdareturns a jobId to the app. - The app will then POLL an
S3 bucketin order to get the response file using the jobId and display that file to the user.
Initially when polling the bucket, I would use the results bucket url in my api call.
This works fine when running the app locally, as the bucket url uses the http protocol, returning a status code 404 until the file is created.
I then created a cloudfront distribution for the app, and the polling request return the error: xhr.js:178 Mixed Content: The page at '<>my cloudfront url>' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint '<my endpoint>'. This request has been blocked; the content must be served over HTTPS. which makes perfect sense.
However changing the viewer protocol behaviour in the app's cloudfront distribution from Redirect HTTP to HTTPS to HTTP and HTTPS didn't seem to work.
So I thought I could create a second cloudfront distribution for the S3 results bucket as that uses https.
Yet, when I now run the api call, I get a 403 status code and no longer the 404.
So I tried setting up a custom error response, mapping the 403 error to 404. I waited a good while as cloudfront can take some time, but this still doesn't seem to have made any difference.
Changing the code in my app to expect a 403 instead of a 404 works and after a while once the file has been written to the results S3 bucket I get the file and display it on the app. But I don't want to expect a 403 as this is entirely the wrong code for something that doesn't exist.
I have a several questions here:
- Is this the correct approach (having a
cloudfrontdistribution for the resultsS3 bucket)? - Why would I get a
403when using thecloudfronturl instead of the404I was getting when using theS3results url? - If point 1 is the correct approach, what do I do to fix point 2?