Terraform interviews questions

How to create multiple GCSbuckets with terraform code in different region locations?

Biswanath Giri
6 min readMay 4, 2023

To create multiple Google Cloud Storage (GCS) buckets with Terraform in different region locations, you can use a for_each loop to iterate over a map variable that contains the bucket names and their respective region locations. Here's an example Terraform code:

variable "buckets" {
type = map(string)
default = {
"bucket1" = "us-east1"
"bucket2" = "us-west1"
"bucket3" = "europe-west1"
}
}

resource "google_storage_bucket" "gcs_bucket" {
for_each = var.buckets

name = each.key
location = each.value
}

In this example, we define a variable buckets that is a map with keys representing the bucket names and values representing the region locations. The google_storage_bucket resource block uses the for_each loop to iterate over the buckets map, creating a GCS bucket for each key-value pair in the map.

The name attribute is set to each.key, which is the current bucket name being iterated over, and the location attribute is set to each.value, which is the current region location being iterated over.

You can modify the buckets map variable to include additional bucket names and region locations as needed, and Terraform will automatically create the GCS buckets in the specified region locations.

What are meta arguments in terraform?

In Terraform, meta-arguments are special arguments that can be used to configure the behaviour of a resource or module. They do not directly correspond to any attribute of the resource, but instead, control how Terraform itself handles the resource during execution.

Some common meta-arguments include:

  • depends_on: Specifies a dependency relationship between resources. Use this to ensure that one resource is created before another.
  • count: Specifies the number of resource instances to create. Use this to create multiple instances of a resource with similar configuration.
  • lifecycle: Controls the lifecycle behaviour of a resource, such as preventing a resource from being destroyed or updated under certain conditions.
  • provider: Specifies the provider configuration for a resource. Use this to select a specific provider instance for a resource when using multiple providers for the same resource type.
  • for_each: Similar to count, this argument allows you to create multiple instances of a resource with unique configuration, using a map or set of values.

Meta-arguments are typically specified in the resource or module block and can be used to customize the behaviour of Terraform in various ways.

What is Provisioner in terraform ?

In Terraform, a provisioner is a mechanism that allows you to run scripts or commands on a resource after it has been created or updated. Provisioners are often used to perform tasks such as configuring software, initializing databases, or setting up networking components.

Terraform supports several types of provisioners, including:

  • local-exec: Executes a command on the machine running Terraform.
  • remote-exec: Executes a command on a remote machine over SSH or WinRM.
  • file: Transfers a file to the machine running Terraform or to a remote machine.
  • chef: Configures a machine using Chef.
  • ansible: Configures a machine using Ansible.
  • puppet: Configures a machine using Puppet.

Provisioners are specified in a resource block and are typically used in combination with other resource types, such as aws_instance, google_compute_instance, or azurerm_virtual_machine.

Here’s an example of using a local-exec provisioner to run a script after creating a GCE instance:

Q) If you have created a resource manually outside of Terraform in Google Cloud Platform (GCP), and you want to add it to your Terraform state file without destroying and recreating it.

you can use the terraform import command.

Here are the general steps to import an existing resource into Terraform:

  1. Define a new Terraform resource block in your Terraform code with the same resource type and identifier as the existing resource you want to import
  2. Run the terraform import command with the resource type and identifier of the existing resource you want to import.
  3. Verify that the imported resource is correctly represented in your Terraform state file by running the terraform show command.

Here is an example of importing an existing GCP Compute Engine instance into Terraform:

  1. Define a new Compute Engine instance resource block in your Terraform code with the same identifier as the existing instance:
resource "google_compute_instance" "example" {
name = "my-instance"
machine_type = "n1-standard-1"
zone = "us-central1-a"
# ...
}
terraform import google_compute_instance.example my-project/us-central1-a/my-instance

Note that the identifier of a GCP resource includes the project ID, location (e.g. zone or region), and resource name. You can find the identifier of an existing resource in the GCP console or by using the gcloud command-line tool.

  1. Verify that the imported resource is correctly represented in your Terraform state file by running the terraform show command.

Note that not all resources can be imported into Terraform, and some resources may require additional configuration or modification to work properly with Terraform. It’s important to review the Terraform documentation and resource provider documentation for the specific resource you want to import to ensure that it is supported and properly configured.

What is the type of variables in terraform?

  1. String variables: String variables represent a string of characters and can be defined using the string type. They are commonly used to define text values, such as names, labels, and descriptions.

Example:

variable "region" {
type = string
default = "us-west1"
}
  1. Number variables: Number variables represent numeric values and can be defined using the number type. They are commonly used to define numerical values, such as port numbers, timeouts, and disk sizes.

Example:

variable "instance_count" {
type = number
default = 1
}
  1. Boolean variables: Boolean variables represent either true or false and can be defined using the bool type. They are commonly used to enable or disable certain features or options.

Example:

variable "enable_https" {
type = bool
default = true
}

In addition to these basic variable types, Terraform also supports complex variable types, such as lists, maps, and objects. These complex types can be used to define more complex data structures, such as arrays of values or nested configurations.

Example:

variable "tags" {
type = map(string)
default = {
environment = "production"
owner = "john.doe@example.com"
project = "my-project"
}
}

In this example, the tags variable is defined as a map of strings, where each key represents a tag name and each value represents a tag value. This allows you to define a set of tags to apply to your resources.

How to craete mutiple gce vm with terraform code in gcp?

  1. Create a new Terraform configuration file (e.g. main.tf) and add the necessary provider information:
provider "google" {
project = "my-project"
region = "us-central1"
}
  1. Define the necessary variables to parameterize your configuration. For example:
variable "instance_count" {
type = number
default = 2
}

variable "machine_type" {
type = string
default = "n1-standard-1"
}

variable "image" {
type = string
default = "ubuntu-os-cloud/ubuntu-2004-lts"
}

variable "network" {
type = string
default = "default"
}
  1. Create a google_compute_instance resource block for each VM that you want to create. You can use Terraform's count feature to create multiple instances using a single resource block:
resource "google_compute_instance" "vm_instance" {
count = var.instance_count

name = "vm-${count.index + 1}"
machine_type = var.machine_type
boot_disk {
initialize_params {
image = var.image
}
}

network_interface {
network = var.network
}
}

In this example, the count parameter is used to create multiple instances based on the instance_count variable. The name parameter is set dynamically based on the current index of the loop, so that each instance has a unique name.

  1. Initialize the Terraform configuration by running terraform init.
  2. Preview the changes that Terraform will make by running terraform plan.
  3. Apply the changes by running terraform apply.

This will create the specified number of GCE VM instances in the specified region, using the specified machine type, image, and network. You can modify the configuration as needed to customize the VM instances to your specific requirements.

--

--

Biswanath Giri
Biswanath Giri

Written by Biswanath Giri

Cloud & AI Architect | Empowering People in Cloud Computing, Google Cloud AI/ML, and Google Workspace | Enabling Businesses on Their Cloud Journey

No responses yet