Creating the Provider Block

First we are going to need to create the provider code block in our main.tf.

provider "aws" {  
  version = "~> 2.0"  
  region  = var.region
}

Here we made sure to set region to var.region so that we can specify the region in our child modules.

Creating the S3 Bucket

Now we need to add in the code block for our S3 Bucket.

resource "aws_s3_bucket" "prod_website" {  
  bucket_prefix = var.bucket_prefix  
  acl    = "public-read"   

  website {    
    index_document = "index.html"    
    error_document = "error.html"   

  }
}

Now in this block you can see that we set a variable for bucket, but just set public-read for our acl. We want to make sure that we can set a value for bucket_prefix in the child module which is why we set a variable here.

For website we are going to keep the classic index.html and error.html, but feel free to change these if your use case calls for it.

Creating the Bucket Policy

Last we need to create a bucket policy. We are going to allow public get for all of the objects in our bucket, so we will use this code for our policy.

resource "aws_s3_bucket_policy" "prod_website" {  
  bucket = aws_s3_bucket.prod_website.id   

policy = <<POLICY
{    
    "Version": "2012-10-17",    
    "Statement": [        
      {            
          "Sid": "PublicReadGetObject",            
          "Effect": "Allow",            
          "Principal": "*",            
          "Action": [                
             "s3:GetObject"            
          ],            
          "Resource": [
             "arn:aws:s3:::${aws_s3_bucket.prod_website.id}/*"            
          ]        
      }    
    ]
}
POLICY
}

For the policy we need to set the resource addressing as above so it targets our bucket. Then set the policy itself which is going to allow public read and get object on all contents inside of the bucket that is defined by var.bucket.

Creating the variables.tf File

It is time to create our variables file. We just need to create variables for everything we set variables for in the main.tf. That would be **var.bucket_prefix **and var.region.

variable "bucket_prefix" {  
  type        = string  
  description = "Name of the s3 bucket to be created."
} 
variable "region" {  
  type        = string  
  default     = "us-east-1"  
  description = "Name of the s3 bucket to be created."
}

I set the default region as us-east-1, but you can set it as whatever works best for you.

Creating outputs.tf File

The outputs will only need one output in order for this module to work.

output "s3_bucket_id" {
  value = aws_s3_bucket.prod_website.id
}

Since we are referencing the id for the s3 bucket in the child modules we want to include it here so that the parent module is able to read the output from the child module.

Usage

module prod_website { 
source = “github.com/jakeasarus/terraform/s3_website_no_cloudfront” 
bucket_prefix = “this-is-only-a-test-bucket-delete-me-”
}

Your usage may vary in source depending on where you put your files. Also do not forget to set your provider block!

Conclusion

I hope you enjoyed this article and got some value out of it! Soon I will add another article that covers adding in a cloudfront distribution!

If you are interested in learning more about Terraform I have a Free Terraform Course for getting started and a course to help you study for your HashiCorp Certified: Terraform Associate.

I also highly suggest checking out Terraform Up & Running by Yevgeniy Brikman.

Happy learning!

#devops #hashicorp #terraform #terraform-modules #infrastructure-as-code

Using Terraform to Create an S3 Website Bucket
1.60 GEEK