Terraform series : Terraform code to create S3 bucket with static web-hosting and life cycle policy
1 min readNov 3, 2023
This code helps to create a standard S3 bucket with static web-hosting along with life cycle transition. Lets say, we have some objects inside the bucket, if that is being used for next 30 days, it will automatically move it to another storage class.
Below terraform code is developed in terraform 1.5.7 version.
provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
main.tf
resource "aws_s3_bucket" "SSSbucket" {
bucket = "test-buck"
tags = {
Name = "test-buck"
Environment = "dev"
}
}
resource "aws_s3_bucket_website_configuration" "staticwebhost" {
bucket = aws_s3_bucket.SSSbucket.id
index_document {
suffix = "index.html"
}
error_document {
key = "error.html"
}
}
resource "aws_s3_bucket_lifecycle_configuration" "lifecyclerules" {
rule {
id = "lifecycle-rule"
status = "Enabled"
prefix = "" # Apply to all objects in the bucket
transition {
days = 30
storage_class = "STANDARD_IA"
}
}
bucket = aws_s3_bucket.SSSbucket.id
}
This is just a simple straight forward code, we can improvise by using variable.tf and terraform.tfvars.
#aws #terraform #IaC #s3 #staticwebhosting #transistion #lifecycle