AWS CloudFormation Template for a static website on a private AWS s3 bucket and hosted on AWS CloudFront
- Static site hosted on s3 bucket
- Bucket is private
- Global deployment and cache using AWS CloudFront
- AWS Account
- You need to have AWS CLI configured if you are using AWS CLI (optional)
git clone https://github.com/anandshivam44/secure-static-site.git
cd secure-static-site
export STACK_NAME=<STACK NAME>
export BUCKET_NAME=<BUCKET NAME>
export REGION=<REGION NAME>
Replace <STACK NAME>
, <BUCKET NAME>
and <REGION NAME>
with a stack name of your choice, your own bucket name that you want to create and a region name where you want to deploy
Deploy the template using AWS CF Console or use AWS CLI
aws cloudformation create-stack \
--stack-name $STACK_NAME \
--template-body file://static-site-cloudfront.yaml \
--parameters ParameterKey=S3StaticSiteBucketName,ParameterValue=$BUCKET_NAME \
ParameterKey=Region,ParameterValue=$REGION
Once the Template is deployed on AWS CF Copy/Create/Upload your html files to the bucket Here is one for quick deployment.
<head>
<title>My Website Home Page</title>
</head>
<body>
<h1>Welcome to my website</h1>
<p>Now hosted on Amazon S3!</p>
</body>
</html>
or I've added some Samples in StaticSiteExample1
and StaticSiteExample2
Copy file(s) to your bucket
aws s3 cp index.html s3://$BUCKET_NAME
Goto CloudFront and wait until the deployment is ready. It will take some time. Once Ready access your site with the url provided by CloudFront
Empty the bucket before removing CF Stack
aws s3 rm s3://$BUCKET_NAME/* --recursive
Delete CloudFormationt Stack
aws cloudformation delete-stack --stack-name $STACK_NAME