Setup Amazon S3 Connection
Introduction
Amazon S3 (Simple Storage Service) is one of the most reliable storage solutions for cloud computing, providing high availability, scalability, and security. Stacking Cloud offers an interface to integrate AWS services and simplify the process of managing cloud storage.
Prerequisites
- An AWS account with access to S3
- AWS Access Key ID and Secret Access Key (Generated in AWS IAM)
Step 1: Generate AWS Access Keys
- Open AWS IAM (Identity and Access Management).
- Navigate to Users and select your user. (or generate a new one for this purpose)
- Click on Security Credentials.
- Under Access Keys, click Create Access Key.
- Copy both Access Key ID and Secret Access Key (you will not see the secret again).
Step 2: Set Up AWS Connection on Stacking Cloud
- Log in to Stacking Cloud.
- Go to Storage Integrations.
- Click Add New Connection.
- Select Amazon S3.
- Enter the following details:
- Access Key ID (From AWS IAM)
- Secret Access Key (From AWS IAM)
- Region (e.g.,
us-east-1
) - Bucket Name
- Click Save and Test Connection.
How to Enable HTTPS and Redirect HTTP to HTTPS on an Amazon S3 Bucket
Attention
Currently, the Stacking Cloud deployment overwrites the bucket settings each time, particularly the redirect settings. Therefore, if you want to make changes to the published site, you will need to repeat the redirect setup step 4.
Prerequisites
- An Amazon S3 bucket already hosted publicly with Stacking Cloud.
Step 1: Create CloudFront Distribution
- Go to CloudFront Console.
- Click Create Distribution.
- Under Origin -> Origin domain, select your bucket (ignore the message This S3 bucket has static web….) and select the
http://xxxxx.s3-us-east-1.amazonaws.com
). - Name leave default (the name of the bucket)
- Set Origin Access to Public (Bucket must allow public access).
- Under Default Cache Behavior -> Viewer protocol policy , select Redirect HTTP to HTTPS.
- Cache Policy: CachingDisabled UseOriginCacheControlHeaders-QueryStrings
- under Web Application Firewall (WAF) select Do not enable security protections
- under Settings Set Default root object to
index.html
. - Click Create distribution .
- Wait until the deployment is complete (a publish date will be displayed).
Step 2: Configure CloudFront Function to Serve index.html Automatically
- In CloudFront, select Functions from the sidebar.
- Click Create function:
- Name:
redirect-to-index
- Event Type:
Viewer request
- Name:
- Insert this function code:
function handler(event) { var request = event.request; var uri = request.uri; if (uri.endsWith('/')) { request.uri += 'index.html'; } else if (!uri.includes('.')) { request.uri += '/index.html'; } return request; }
- Click Save and then Publish.
Step 3: Attach CloudFront Function to Distribution
- Go back to CloudFront distributions.
- Select your newly created distribution and open the Behaviors tab.
- Edit the Default (*) behavior.
- Under Function associations (at the bottom of the page), set:
- Function Type: Viewer request
- Function ARN / Name: select redirect-to-index
- Click Save changes.
Step 4: Redirect S3 HTTP Requests to CloudFront*
📌 Problem: The S3 bucket HTTP endpoint is still accessible and may be indexed by search engines.
Solution: Redirect HTTP Requests from S3 to CloudFront
- From the Cloudfront Distributions, click the ID of your newly created distributio, under Details -> Distribution domain name
copy the url example:xxxxx.cloudfront.net
- Now go to your AWS S3 Bucket list eg: https://us-east-1.console.aws.amazon.com/s3/buckets?region=us-east-1
- Click the bucket name from the list
- Go to the Properties tab of the S3 bucket.
- Edit Static Website Hosting.
- Change to Redirect requests instead of “Host a static website”.
- Set the destination to your CloudFront URL e.g.,
xxxxx.cloudfront.net
(without the https://) - Ensure Protocol is set to HTTPS.
- Click Save.
Now, all traffic to the S3 website endpoint will be redirected to CloudFront in HTTPS