Terraform Google Modules: How It Help

Pratik Yadav
Niveus Solutions
6 min readJul 18, 2024

--

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle. Terraform can manage low-level components like compute, storage, and networking resources, as well as high-level components like DNS entries and SaaS features.

What are Modules in Terraform?

Modules in Terraform are like building blocks. They are small pieces of reusable code that can be used to create parts of your cloud setup. For example, if you need to set up a virtual machine, you can use a module instead of writing all the details from scratch. This makes your work faster, easier, and more organized.

How does Terraform work?

Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs). Providers enable Terraform to work with virtually any platform or service with an accessible API.

What are Google modules?

Google Modules are specific types of Terraform modules designed to work with Google Cloud Platform (GCP). These modules help you quickly set up various GCP resources like virtual machines, storage buckets, and databases.

What is versioning in modules?

Versioning in Terraform modules refers to the practice of specifying and managing different versions of Terraform modules to ensure stability, compatibility, and reliability in infrastructure as code (IaC) deployments. It typically refers to managing different versions of code within a Terraform module using a version control system like Git. Developers can specify the desired version of a module within their Terraform configuration files.

Steps to Copy and Use a Google Terraform Module

Step 1: Locate the Module

First, find the Google Terraform module you need. Visit Terraform Registry https://registry.terraform.io/namespaces/terraform-google-modules and search for the desired Google Cloud module, for example, the Google Cloud Storage module.

Step 2: Copy the Module Source

Once you locate the module, you’ll find the source URL, which usually looks like this:

Copy code

source = “terraform-google-modules/cloud-storage/google”

This specifies the module repository and path.

Step 3: Create Your Terraform Configuration File

Create a new directory for your project and navigate into it. Then, create a main.tf file where you will define your Terraform configuration.

copy code

mkdir terraform-project

cd terraform-project

vi main.tf

Step 4: Add the Module to Your Configuration

Open the main.tf file and add the module block. Here is an example of the Google Cloud Storage module:

Copy code

provider “google” {

project = “project-id”

region = “us-central1”

}

module “gcs_bucket” {

source = “terraform-google-modules/cloud-storage/google”

version = “2.8.0” # Use the latest version or a specific version as required

name = “my-example-bucket”

location = “US”

project_id = “project-id”

}

Step 5: Initialize the Terraform Project

In your terminal, navigate to your project directory and run the initialization command. This will download the specified module and set up the environment.

terraform init

Step 6: Apply the Configuration

Apply the configuration to create the resources defined in your main.tf file. Terraform will prompt you to confirm before proceeding.

terraform apply

Type yes when prompted to confirm. Terraform will then provision the resources according to your configuration.

Step 7: Verify the Resources

After the apply command completes, verify that your resources have been created in the Google Cloud Console. You should see the new storage bucket or other resources as defined in your module.

Difference between Google module and Custom module

Both Google modules and custom modules play crucial roles in Terraform-based infrastructure management. Google modules provide a quick, reliable way to implement standard Google Cloud resources, while custom modules offer the flexibility to meet unique requirements and complex workflows.

Where do we save the terraform modules?

1.Google Cloud Storage (GCS): Terraform modules are best stored in Google Cloud Storage because of their durability, availability, and interoperability with other GCP services.

2.Cloud Source Repositories: Cloud Source Repositories provide Git-based version management for your code. You may keep your Terraform modules in a Cloud Source Repository and use Git features like branching, merging, and version control.

3.GitHub or Other Git-Based Repositories: While not specific to GCP, GitHub or other Git-based repositories are commonly used to store and manage Terraform modules. They offer extensive version control features and easy integration with CI/CD pipelines.

4.Terraform Registry: The Terraform Registry allows for the distribution of modules that can be publicly shared or privately managed within an organization.

You can store Terraform modules in various places like Google Cloud Storage, version control systems (like Git), or a public registry to easily manage, track changes, and reuse your infrastructure code across projects. Also, Terraform itself retrieves the module code during the initialization process. Ensure the chosen location is accessible to the Terraform environment you’re using.

Advantages of using Terraform modules

  1. Reusability: Terraform modules enable you to create reusable building blocks of infrastructure code. This saves time and effort by eliminating redundant code for common infrastructure components.

2. Organization: Modules promote better code organization by separating concerns. Complex infrastructure designs can be divided into smaller, modular units, making them easier to comprehend, manage, and change.

3. Consistency: Using the same module for similar infrastructure components across projects ensures that your infrastructure configuration is consistent. This lowers the chance of errors and streamlines management.

4. Maintainability: Changes to modules can be made in one place and propagated to all uses of the module, simplifying maintenance and updates.

5. Security and Compliance: Google modules adhere to security best practices, ensuring that your infrastructure meets security requirements.

Potential drawbacks of Terraform modules

  1. Complexity: Modules can be complex, especially if they are nested or highly parameterized. Modules of this level of complexity might be challenging to understand and maintain, especially for larger teams and projects.

2. Versioning: Module versioning can be challenging. When a module is changed, you must carefully manage the versions used in your various environments to avoid unintended modifications.

3. Lack of Real-Time Updates: Terraform does not provide real-time monitoring or continuous deployment features. You need to run Terraform commands explicitly to apply changes, which might be different from the behavior of some other IaC tools.

Lessons learned with Terraform modules

  1. Importance of Versioning: Always specify and manage module versions explicitly.

a.Experience: Managing module versions and ensuring compatibility can be difficult, particularly when modules are constantly updated.

b.Solution: To prevent unexpected changes, use semantic versioning and provide version limits in your Terraform configuration.

2. Encapsulation and Abstraction: Encapsulate complexity and expose only necessary inputs and outputs.

a.Experience: Modules that expose too many details can be hard to use and maintain.

b.Solution: Design modules to abstract complexity and provide a simple interface.

3. Security Best Practices: Incorporate security best practices in your modules.

a.Experience: Security misconfigurations can expose your infrastructure to vulnerabilities.

b.Solution: Follow the security best practices for GCP and Terraform, and evaluate and update your modules on a regular basis.

Conclusion

Utilizing Google Cloud Platform (GCP) Terraform modules significantly simplifies the management and deployment of cloud resources. These modules provide a standardized, reusable, and community-vetted approach to infrastructure as code, ensuring consistency, reducing manual configuration errors, and accelerating project setup. By leveraging Terraform modules, you can efficiently manage GCP resources, allowing you to focus more on innovation and less on infrastructure management.

Thanks Diksha K for all the support for help in drafting this blog.

Reference:-

1.https://cloud.google.com/docs/terraform/blueprints/terraform-blueprints

2.https://github.com/terraform-google-modules

--

--