Day 14 - Static Website Hosting using Terraform
Today I worked on my first mini project in my AWS Terraform journey. The goal was to deploy a static website using S3 and CloudFront.
Instead of manually creating resources in AWS, I used Terraform to automate the entire setup.
Architecture
User requests first hit CloudFront. CloudFront securely fetches content from a private S3 bucket using Origin Access Control and delivers it globally.
User → CloudFront → Private S3 Bucket
Project Setup
I organized my Terraform code into multiple files for clarity. Variables, provider configuration, and main resources are separated.
S3 Bucket
The S3 bucket stores the website files. Public access is completely blocked.
This ensures that the bucket is not exposed directly to the internet.
Uploading Files
Terraform automatically uploads all files from the local www folder.
This includes index.html, style.css, and script.js.
I used a loop with fileset to avoid writing multiple resource blocks.
CloudFront Distribution
CloudFront acts as the delivery layer. It provides HTTPS and caches content globally.
Security Setup
Instead of making the bucket public, I used Origin Access Control.
This allows only CloudFront to access the S3 bucket.
This is a more secure and production-ready approach.
Deployment
I used terraform init, plan, and apply to deploy the infrastructure.
Terraform created all resources and returned the website URL.
Testing
After deployment, I opened the CloudFront URL. The website loaded successfully with styling and interactivity.
Challenges
One challenge was understanding how CloudFront accesses a private S3 bucket.
The combination of Origin Access Control and bucket policy solved this.
Another challenge was ensuring correct MIME types so the browser renders files properly.
Key Learnings
This project helped me understand how S3, CloudFront, and Terraform work together.
I learned how to securely host a static website using a private S3 bucket.
I also understood how Terraform simplifies infrastructure creation.
Cleanup
To avoid charges, I used terraform destroy to remove all resources.
Final Thoughts
This project showed me how simple components can form a real-world cloud architecture.
It was a small step, but an important one.
Comments
Post a Comment