This guide walks you through deploying a static website on Amazon S3 using Terraform.
- AWS Account
- Terraform installed on your machine
-
Create a Directory: Open your terminal and run:
mkdir terraform-s3-website && cd terraform-s3-website
-
Initialize Terraform:
terraform init
-
Create a Terraform configuration file named
main.tf
in your project directory and open it in your text editor. -
Add the AWS provider and specify your region:
provider "aws" { region = "us-east-1" # You can choose any region }
-
Define the S3 bucket resource for hosting your website:
resource "aws_s3_bucket" "my_bucket" { bucket = "my-unique-bucket-name-12345" # Bucket name must be unique acl = "public-read" # Makes the bucket publicly readable website { index_document = "index.html" } }
-
Configure the S3 bucket policy for public access:
resource "aws_s3_bucket_policy" "bucket_policy" { bucket = aws_s3_bucket.my_bucket.id policy = jsonencode({ Version = "2012-10-17", Statement = [ { Action = "s3:GetObject", Effect = "Allow", Principal = "*", Resource = "${aws_s3_bucket.my_bucket.arn}/*" }, ] }) }
-
Apply your Terraform configuration to create the S3 bucket:
terraform apply
Type
yes
when prompted to proceed. -
Upload your
index.html
file to your S3 bucket using the AWS Management Console or AWS CLI. -
Access your website via the URL provided by S3, formatted as
http://<your-bucket-name>.s3-website-<region>.amazonaws.com
.
Congratulations! You've now deployed a static website on Amazon S3 using Terraform. This is a basic setup. Terraform allows for much more customization and complex deployments.
--
The code within this project is dual-licensed under the GLINCKER LLC proprietary license and the MIT License. This means it is open for reference and educational purposes, allowing for use, modification, and distribution in accordance with the MIT License's terms, while also respecting the proprietary rights and restrictions under the GLINCKER LLC license.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.