Accessing AWS S3 via CloudFront on the Web
Serve AWS S3 to the web via AWS CloudFront. This approach supports HTTPS with a certificate generated by AWS.
Here's how you can set it up:
1. Go to Amazon S3 and Create bucket, let's say the name is bucket-test.
2. Keep Block all public access on, and create the bucket.
3. Optional. Configure the bucket for static website hosting: enable static website hosting in the bucket's properties and set up your index and error documents.
4. Go to Amazon CloudFront > Distributions and Create distribution, let's say the name is serve-bucket-test.
-
Step 1 Get started:
- Single website or app,
- Press Next and skip the domain configuration for now.
-
Step 2 Specify origin:
- Origin type: Amazon S3,
- In the Origin section, click Browse S3 and choose the bucket,
- Optionally select the path (subfolder) that will be served to the public,
- Make sure the option Allow private S3 bucket access to CloudFront - Recommended is enabled,
- Press Next.
-
Step 3 Enable security: It is a matter of your choice to use WAF or not. In this case, I'm intending to serve only images (pictures) from the bucket, so I'm choosing Do not enable security protections.
-
Step 4 Review and create: review the selected parameters and press Create distribution.
5. At the Distribution page, choose Add domain:
- Step 1 Configure domains: for the domain to serve, enter your real FQDN
real.domain-name.com. - Step 2 Get TLS certificate:
- The service will provide a Name/Value pair for a CNAME record,
- Create the DNS record in your DNS provider, disable any proxy options if there are any, and set the TTL to 1 min,
- Wait a while until the certificate is validated, then click
Next.
- Step 3 Review changes: if everything looks okay, press Add domain.
6. At the Distribution page, now you need to select Route domains to CloudFront. It will ask you to create A and AAAA records that point to a FQDN instead of an IP address, which is not allowed by most DNS providers. If that is your case, just create a CNAME record instead of these.
7. Now you can upload some-test-image.jpg to your bucket and try to access https://real.domain-name.com/some-test-image.jpg. Note: if you selected a path (subfolder) within the CloudFront setup, you will need to upload the image to that folder, but access it via the root path in the web.
