In this walkthrough, we’ll be creating an S3 bucket in Amazon Web Services (AWS) using Terraform. This is a pretty straightforward walkthrough but you will change a bit of information in your .tf
files.
Prerequisites
AWS account
Terraform CLI
IDE (Pycharm or Cloud9 for example)
Step 1
Create a new IAM user with full S3 access. I’m going to grant my IAM user Administrator Access and S3 Full access.
Using the aws configure
command, input your new IAM user’s credentials.
Step 2
Head over to Github and fork this repository. Once you’ve done that, git clone
the repo in your CLI.
cd
into your repository.
Head over to your variables.tf
file as we are going to make a few updates.
The first thing you’ll want to do is change the region of your AWS account. In this case, my account is in us-east-1
. Next, you’re going to change the name of your S3 bucket making sure it is globally unique.
There isn’t anything we have to change in the main.tf
file but let’s review a few main pieces of the code inside the file and what it’s going to do.
aws_s3_bucket_acl
: This part defines whether the bucket access will be private or not. In the AWS console, this is what it looks like.
aws_s3_bucket_public_access_block
: This line of code defines if the contents of the bucket can be publicly accessed or not. Setting this to “true” will block public access.
Step 3
Now that we’ve set up our configuration code, let’s initialize terraform with terraform init
Let’s run our code with terraform apply
and watch our S3 bucket be deployed.
To confirm that your S3 bucket has been created, head over to the AWS management console.
Don’t forget to terraform destroy
your resources so you don’t incur any additional AWS charges outside of the free tier.