DEV Community

Cover image for How to create s3 bucket using infrastructure as code(terraform)
Frank Promise Edah
Frank Promise Edah

Posted on

How to create s3 bucket using infrastructure as code(terraform)

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.

  1. Attach policy
  2. Add tags(optional)
  3. 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. Create S3 bucket module 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.

2. Define bucket

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.

3. Define variables

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.

4. Add configuration
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"
}

Run Terraform script in your system

  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.

Top comments (0)