Terraform script to configure S3 and Cloudfront for static website hosting. Restricted access to Cloudfront via referer header, custom domain, 404 page routing, and so on.
Niedziela Solutions, LLC's website was made by and is managed using this tool!
- Terraform
- AWS Account
- S3 Bucket with server-side encryption enabled. This is to store the Terraform state remotely in an S3 Bucket.
- A website you want to setup infrastructure for!
terraform init -backend-config="profile=YOUR_AWS_PROFILE"
. It will prompt for the S3 Bucket you want to use to store your Terraform state and the AWS region to use.cp environment.tfvars.example environment.tfvars
and change as needed (see Settings section)- Verify:
terraform plan -var-file=environment.tfvars
- Invoke:
terraform apply -var-file=environment.tfvars
- Import S3 Bucket:
terraform import -var-file=environment.tfvars aws_s3_bucket.static BUCKET_NAME
- Import Cloudfront Distribution:
terraform import -var-file=environment.tfvars aws_cloudfront_distribution.s3_distribution CLOUDFRONT_ID
- Destroy:
terraform destroy -var-file=environment.tfvars
region = AWS_REGION
profile = AWS_PROFILE
s3_bucket = BUCKET_NAME_YOU_WANT
s3_origin_id = BUCKET_ORIGIN_ID (also used for referer header)
aliases = LIST_OF_DOMAINS
acm_certificate_arn = ACM CERTIFICATE ARN
viewer_protocol_policy = VIEWER_PROTOCOL_POLICY