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
- Basic bucket setup
- 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.
- 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,
Pingback: Szumma #105 – 2018 15. hét – ./d/fuel