How to Create a Storage Bucket in GCP with Terraform?

close up photo of programming of codes
Reading Time: 3 minutes

In this article, we are going to provision a Cloud Storage Bucket and maintain it with the help of terraform.

Let us first understand cloud storage first

What is Cloud Storage(Buckets)?

Cloud Storage is a google cloud platform’s Object storage system. Here objects can be of any type either may be a file or it can also be a large binary object. These objects are organized into buckets, It is important to remember that cloud storage is not a file system. It can be called a service that receives, stores, and retrieves files or objects. It is also accessible from a VM.

Each stored object is uniquely addressable by a URL also you can grant IAM roles to enable any application to read and write to buckets.

Creating a Bucket?

Creating a Bucket in Google cloud is quite simple and there are various ways through which you can create a bucket such as:

  1. Through Console
  2. Through Gcloud Cli
  3. IAC

In this blog, we are going to use terraform which is an Infrastructure as a code tool and we will be learning how you can create a Bucket with it.

Let’s Create a Main.tf file first

Add Provider for terraform

Whenever we want to use terraform, the first thing we do is define a provider, which in our case today is google.

provider "google" {

credentials = file("~/gcp/access-keys.json")

project = var.project_id


}

In the provider, we have also defined the path of the credentials that will be used for authentication and also mentioned the project ID.

Create a Bucket resource variables file

Before creating a Bucket resource let’s create a few variables that we will be using

variable "bucket_name" {

  type = string

  description = "Bucket name"

}

variable "bucket_location" {

  type = string

  default = "us-east1"

}

variable "project_id" {

  type = string

}

variable "storage_class" {

  type = string

}

We have defined these four variables i.e

  1. bucket name – which will be used for defining the bucket name
  2. Bucket location – location of the GCS
  3. project id – which defines the project id we will use
  4. storage class – it will define the storage class for the bucket.

Create a Google storage Bucket resource

resource "google_storage_bucket" "default" {

  name = var.bucket_name

  storage_class = var.storage_class

  location = var.bucket_location

}

Here you can also enable versioning or encryption and lifecycle for this bucket You can explore these resources in the official terraform documentation. The storage class we will be using here is REGIONAL, If you don’t provide a storage class then google by default gives STANDARD as a storage class. It also supports other options as well such as MULTI_REGIONAL, NEARLINE, COLD LINE, and ARCHIVE. You can select any of them as your use case describes.

Let’s add another file terraform.tfvars and pass the values for the variables we created

bucket_name = "my-bucket-tf"

storage_class = "REGIONAL"

project_id = "neon-semiotics-351410"

Let’s provision these resources with the help of terraform

terraform init
Terraform init

Run terraform Plan

Terraform Plan

The terraform plan looks fine, Now we can run terraform apply

Terraform apply

The terraform Plan is also successful, As you can see it has created resources Now to verify it we can log in to the console and check if it has created this resource.

Console output

You can see it has created your GCS resource and now you can use it for storing purposes.

Conclusion

In this blog we saw, how we can provision a GCS bucket with the help of Terraform, You can also use this for storing your tfstate , if you liked this blog please do comment and share. If you want to explore more on this resource, You can visit this official documentation from terraform itself.

Written by 

Passionate about Technology and always Interested to Upskill myself in new technology, Working in the field of DevOps

Leave a Reply