Building AWS Infrastructure with Terraform: S3 Bucket Creation

If you are going to be working with any cloud provider it is highly suggested that you script out the creation/maintenance of your infrastructure.  In the AWS word you can use the native CloudFormation solution, but honestly I find this painful and the docs very lacking.  Personally, I prefer Terraform by Hashicorp.  In my experience the simplicity and easy of use, not to mention the stellar documentation make this the product of choice.

This is the initial post in what I hope to be a series of post about how to use Terraform to setup/build AWS Infrastructure.

Terrform Documentation on S3 Creation -> Here

In this post I will cover 2 things

  1. Basic bucket setup
  2. Bucket setup as Static website

Setting up a basic bucket we can use the following

resource "aws_s3_bucket" "my-bucket" {
    bucket = "my-bucket"
    acl    = "private"

    tags {
        Any_Tag_Name = "Tag value for tracking"
    }    
}

When looking at the example above the only 2 values that are required are bucket and acl.

I have added the use of Tags to show you can add custom tags to your bucket

Another way to setup an S3 bucket is to act as a Static Web Host.   Setting this up takes a bit more configuration, but not a ton.

resource "aws_s3_bucket" "my-website-bucket" {
    bucket = "my-website-bucket"
    acl    = "public-read"

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

    policy = <<POLICY
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AddPerm",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::my-website-bucket/*"
        }
    ]
}
    POLICY
    tags {
          Any_Tag_Name = "Tag value for tracking"
    }
}

The example above has 2 things that need to be pointed out.

  1. The website settings.  Make sure you setup the correct pages here for index/error

The Policy settings.  Here I am using just basic policy.  You can of course setup any policy here you want/need.

As you can see, setting up S3 buckets is very simple and straight forward.

*** Reminder: S3 bucket names MUST be globally unique ***

Till next time,

One thought on “Building AWS Infrastructure with Terraform: S3 Bucket Creation

  1. Pingback: Szumma #105 – 2018 15. hét – ./d/fuel

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s