Open In App

How To Create Storage Bucket In Gcp Using Terraform ?

In Cloud Computing, it is very crucial to provide efficient storage solutions. Google Cloud Platform (GCP) offers an exemplary service through it’s Storage Bucket. Storage Buckets are versatile containers for securely storing data objects, while providing scalability, durability, and global accessibility. However, manually configuring these type of services can be tedious and prone to error. Here comes Terraform in the picture. Terraform is an infrastructure as code tool that manages resource with it’s declarative configuration approach. Using Terraform users can define the desired state of infrastructure, including Storage Buckets, in a concise and reproducible manner. In this article, we will be learning about creating storage bucket using Terraform.

What Is Cloud Storage Bucket In GCP?

A Cloud Storage Bucket in Google Cloud Platform is a globally unique named storage container used for storing data objects. Objects can be anything such as images, videos, documents, and other type of files. It provides high durability and high scalability. Cloud Storage Buckets can be accessed securely from anywhere on the internet. Each Cloud Storage Bucket belongs to a particular Google Cloud Project and can be configured with various settings such as access control, versioning, etc.



Some Key features of Google Cloud’s Cloud Storage Bucket are,

What Is Terraform ?

Terraform is an Open-sourced, infrastructure as Code (IaC) tool developed by HashiCorp. Terraform enables users to define and provision infrastructure resources such as virtual machines, networks, storage, and more using declarative configuration files. With Terraform, infrastructure can be managed as code which enables versioning, collaboration, and automation of provisioning tasks across various cloud providers and on-premise infrastructure.



Some important features of Terraform includes as follows:

Create Storage Bucket In GCP Using Terraform: A Step-By-Step Guide

Step 1: Setup Terraform

terraform --version

Output

Step 2: Install Google Cloud SDK

Cloud SDK stands for Google Cloud Software Development kit. It is a set of tools and libraries that enables developers to interact with Google Cloud Platform (GCP) services using command line. Using Cloud SDK we can manage GCP resources, deploy applications, and perform other tasks directly from Terminal. To use terraform with Google Cloud Platform we need this Google Cloud SDK to be installed in our computer system.

Step 3: Generate JSON Key

Step 4: Write Terraform Configuration

provider "google" {
credentials = file("<PATH_TO_YOUR_GCP_SERVICE_ACCOUNT_KEY_JSON>")
project = "<YOUR_GCP_PROJECT_ID>"
region = "<YOUR_GCP_REGION>"
}

resource "google_storage_bucket" "my_bucket" {
name = "my-unique-bucket-name"
location = "<BUCKET_LOCATION>"
}

Provider Block:

Resource Block:

Step 5: Execute Terraform Configurations

terraform init

Output:

terraform plan

output:

terraform apply

Output:

Step 6: Verify Storage Bucket Creation

Now to verify the creation of the Storage Bucket,

Conclusion

Using Terraform to provision storage buckets in Google Cloud Platform provides a convenient and efficient way to manage infrastructure resources. We have use Terraform to easily create, update, and delete storage buckets while maintaining version control and consistency. You can further upload data in the bucket or integrate the storage bucket with you application and use the cloud storage services.

Create Storage Bucket In GCP Using Terraform – FAQ’s

Can I Create Storage Bucket Using Terraform In GCP ?

Yes, you can perform any resource provisioning services in GCP including Storage Bucket using Terraform. You need to have Google SDK installed in your system, and you should have service account keys for your GCP project.

How Can I Enable Versioning For My Storage Bucket Using Terraform?

You can enable versioning for a storage bucket by adding the ‘versioning’ block to your Terraform configuration and setting the enabled attribute to true.

Can I Create Multiple Storge Buckets In One Configuration File In Terraform?

Yes, You can create multiple storage buckets using one configuration file in Terraform. Each resource block in terraform should have a unique name and configuration.

What Is The Maximum Object Size To Be Uploaded In Storage Bucket?

You can upload any object to GCP bucket which is less than 5TB in size.

What Will Happen If Error Occurs During Execution Of Terraform Apply ?

If error occurs during execution, Terraform will halt the process and display error messages. The execution will not make any changes to resources until errors are resolved.


Article Tags :