How to create s3 bucket using infrastructure as code(terraform)
Published: 2022-04-16 12:18:03

__WHAT YOU WILL LEARN__

In this tutorial, we are going to create an s3 bucket using terraform.

__WHAT IS TERRAFORM?__

Terraform is an infrastructure as code tool that allows you to develop, update, and version your infrastructure efficiently and keeping it secure.

__PREREQUISITES__
1. AWS IAM role with S3 permissions
2. Access key ID and secret access key of that account.

__How to create an Access key and secret access key__
1. login to your AWS account
2. select IAM
3. On the left side of the panel, select __user__
4. Select __Add users__ and enter details

NOTE: you have to select __programmatic access__ in access type to get access key ID and secret key.

5. Attach policy
6. Add tags(optional)
7. create user
 If your user is successfully created, you will see a message with your access key and secret key.


__Steps to create an s3 bucket using Terraform__

1. <u>__Create S3 bucket module__</u>
Create a module that will have a basic s3 file configuration. And for that, i will create one folder name _"S3"_  which will have two files namely : __bucket.tf and var.tf__.

<u>__2. Define bucket__
</u>
Open bucket.tf and define your bucket in it.


`resource "aws_s3_bucket" "practises3" {
    bucket = "${var.bucket_name}" 
    acl = "${var.acl_value}"   
}
`

__Explanation__
There is a block with the key name __resource__ with resource type __aws_s3_bucket__. It is a fixed value and since terraform is cloud agnostic, this value depends on the provider. In this case, the cloud provider is AWS and s3 is the resource and __practises3__ is the resource name used. 

Bucket and ACL(access control list) are arguments types for our resources. Either we can provide value directly or use the var.tf file to declare the value of an argument.

<u>__3. Define variables__</u>

`variable "bucket_name" {}
variable "acl_value" {
    default = "private"
}`

__Explanation__

The block declares values of variables. We can either provide a default value to be used when needed or ask for value during execution.

<u>__4. Add configuration__</u>
Here, we will create a file named main.tf for keeping configuration in our working directory

`provider "aws" {
    access_key = "${var.aws_access_key}"
    secret_key = "${var.aws_secret_key}"
    region = "${var.region}"
}
module "s3" {
    source = "<path-to-S3-folder>"
    bucket_name = "<Bucket-name>"       
}`


__Explanation__

Here, details of our provider (AWS) access key, secret key, etc is provided. Since are using terraform modules to create s3, we use the keyword __module__ and the name of the folder we created earlier. In argument, we provide a source to the s3 module and bucket name.

__5. Add Access key, secret_key, and region__

Now we will define variable.tf where we will enter our access key, secret key and region.

`variable "aws_access_key" {
default = “<your_access_key>”
}
variable "aws_secret_key" {
default = “<your_secret_key>”
 }
variable "region" {
    default = "region"
}`


<u>__Run Terraform script in your system__
</u>

1. __terraform init__
It is used to initilize the working directory.

2. __terraform plan__
We will use this command for script verification to confirm that there is no error in our configuration.

3. __terraform apply__
we will use this command to create the s3 bucket.