Terraform Cloud Storage Provisioning with HashiCorp and Google Cloud

Terraform is an infrastructure-as-code tool that lets you provision and manage cloud infrastructure. Terraform provides plugins called providers that let you interact with cloud providers and other APIs. You can use the Terraform provider for Google Cloud to provision and manage Google Cloud resources, including Cloud Storage. This integration allows teams to describe storage infrastructure declaratively and apply it consistently across environments.

Google Cloud Platform offers an exemplary service through its 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 a code tool that manages resources with its declarative configuration approach. Using Terraform users can define the desired state of infrastructure, including Storage Buckets, in a concise and reproducible manner.

How Terraform Works with Cloud Storage

Terraform has a declarative and configuration-oriented syntax, which you can use to describe the infrastructure that you want to provision in your Google Cloud project. After you author this configuration in one or more Terraform configuration files, you can use the Terraform CLI to apply this configuration to your Cloud Storage resources.

The following steps explain how Terraform works:

  • You describe the infrastructure you want to provision in a Terraform configuration file. You don't need to write code describing how to provision the infrastructure. Terraform provisions the infrastructure for you.
  • You run the terraform plan command, which evaluates your configuration and generates an execution plan. You can review the plan and make changes as needed

Each resource block describes one or more infrastructure objects, such as virtual networks or compute instances. The provider for Google Cloud exposes Cloud Storage resources through Terraform resource types that map directly to GCP APIs.

The following table lists the Terraform resources available for Cloud Storage:

Service Terraform resources Data sources
Cloud StorageTerraform service
Storage IntelligenceTerraform service
Storage batch operations feature of Storage IntelligenceTerraform service
-
Storage Insights feature of Storage IntelligenceTerraform service

Cloud Storage Bucket Fundamentals 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 types 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,

  • Durability
  • Scalability
  • Security
  • Multi-region support
  • Object Versioning
  • Lifecycle Management
  • Integration with other GCP services

In Cloud Computing, it is very crucial to provide efficient storage solutions. Google Cloud Platform 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.

Prerequisites and Setup for Terraform

Before creating a bucket with Terraform, the local development environment must be prepared.

Step 1: Setup Terraform

To use Terraform, you need to make sure that Terraform is installed in your computer system. To check if Terraform is installed or not, run the following command in Command Prompt.

terraform --version

Output:
As Terraform is installed in my system, you can see it is showing it's version. If you don't see this output then do the followings:

  • Download the Terraform installation file.
  • Unzip the downloaded file.
  • Copy the path from the directory.
  • In environment variable settings, make a entry using this under Path.

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 services using command line.

The SDK provides authentication, provider credential helpers, and CLI tools that Terraform uses when communicating with Google Cloud APIs.

Terraform-Based Guides for Cloud Storage

The following table lists Terraform-based how-to guides and tutorials for Cloud Storage:

Guide Details
Quickstart: use Terraform to create a bucket and upload an object This quickstart shows how to get started with Terraform by creating a Terraform configuration file that provisions a storage bucket and uploads a sample object to the bucket.
Create a bucket This guide explains how to create a bucket.
Get bucket metadata This guide explains how to view the metadata of a bucket.
Upload objects This guide explains how to upload objects to a bucket from your local file system.
Get object metadata This guide explains how to view the metadata of an object.
Manage object lifecycles This guide explains how to set the lifecycle configuration for a bucket.
Configure Pub/Sub notifications This guide explains how to configure your bucket to send notifications about object changes to a Pub/Sub topic

Creating a Storage Bucket in GCP Using Terraform

Step 1 is setup Terraform as described above. Step 2 is Install Google Cloud SDK.

Now to initialize Terraform and download the necessary plugins, run the following command

terraform init

Output:

Now run, the following command to preview the changes Terraform will make.

terraform plan

Output:

After successful review of the plan, go ahead and execute the following line of command

terraform apply

This will show you the plan again for review and ask for your confirmation. Enter yes to continue and the Storage Bucket will be created.

Output:

As you can observe from the above picture, we the Storage bucket is created successfully.

Step 6: Verify Storage Bucket Creation

Now to verify the creation of the Storage Bucket,

  • Open GCP Cloud Console in your web browser.
  • Select Cloud Storage and then click on buckets.

In the Buckets page, you should be able to see the Cloud storage bucket we have created.

As you can observe, the bucket with same unique name is present in cloud console with the configurations we have made earlier. Hence, we have successfully created a Storage Bucket in Google Cloud Platform using Terraform.

When provisioning a bucket and uploading an object, the workflow typically completes with an apply output similar to the following:

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

View your storage bucket and uploaded object

In the Google Cloud console, go to the Cloud Storage Buckets page. The new bucket appears, containing the sample_file.txt object. Note that the resources might take a few minutes to get provisioned after you run terraform apply.

Clean Up and Lifecycle Management

In order to avoid incurring unexpected charges from the Google Cloud resources you created during this quickstart, complete the following steps to clean up the resources:

In your terminal, set the terraform folder as the current working directory:

cd ~/terraform

Delete the Cloud Storage resources that you created based on your Terraform configuration file:

terraform destroy

If successful, Terraform returns output similar to the following:

Terraform used the selected providers to generate the following execution plan

Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value:

Type
yes
and press Enter.

Using Terraform to provision storage buckets in Google Cloud Platform provides a convenient and efficient way to manage infrastructure resources. You can further upload data in the bucket or integrate the storage bucket with you application and use the cloud storage services.

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.

Terraform's declarative model removes manual repetition and drift risk for Cloud Storage. The provider for Google Cloud exposes bucket creation, object upload, metadata retrieval, lifecycle rules, and Pub/Sub notifications as codifiable resources. Combined with the inherent durability, scalability, security, multi-region support, object versioning, lifecycle management, and integration with other GCP services offered by Cloud Storage Buckets, teams gain reproducible storage foundations that can be reviewed, planned, and applied with terraform plan and terraform apply.

Initialization with terraform init, planning with terraform plan, and applying with terraform apply form a consistent operational loop. Verification in the Google Cloud console confirms the bucket exists with the intended configuration, and cleanup with terraform destroy ensures no unexpected charges remain. This workflow aligns with infrastructure-as-code principles and supports scalable GCP storage delivery.

Sources

  1. docs.cloud.google.com/storage/docs/terraform-for-cloud-storage
  2. www.geeksforgeeks.org/devops/how-to-create-storage-bucket-in-gcp-using-terraform/
  3. docs.cloud.google.com/storage/docs/terraform-create-bucket-upload-object

Related Posts