
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 the Google Cloud platform’s object storage system. Objects here can be of any type, either files or large binary objects. These objects are organized into buckets. Please note that cloud storage is not a file system. A service that receives, stores, and retrieves files and objects. You can also access it from your VM.
Each stored object is uniquely addressable via a URL. You can also assign an IAM role to allow your application to read and write to your bucket.
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:
- Through Console
- Through Gcloud Cli
- 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 = "The name of our bucket"
}
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
- bucket name – which will be used for defining the bucket name
- Bucket location – location of the GCS
- project id – which defines the project id we will use
- 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



Run terraform Plan



The terraform plan looks fine, Now we can run 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.



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.