OCI Terraform Part 2 - Terraform variables

Understanding and Using Variables in Terraform

Introduction to Variables and Why They're Important

In Terraform, variables play a crucial role in making your configuration flexible, reusable, and easy to manage. Without variables, every piece of information, like resource IDs or network configurations, would need to be hardcoded into your scripts, making them difficult to adapt to different environments or use cases. By using variables, you can separate the configuration from the code logic, allowing for easy changes without needing to modify the underlying code.

Types of Variables

Terraform supports various types of variables that you can use to parameterize your configurations. Here are the primary types:

String


variable "cms_string" {
  description = "A simple string variable"
  type        = string
  default     = "Hello, Terraform!"
}
    

Number


variable "cms_number" {
  description = "A simple number variable"
  type        = number
  default     = 42
}
    

Boolean


variable "cms_boolean" {
  description = "A simple boolean variable"
  type        = bool
  default     = true
}
    

List


variable "cms_list" {
  description = "A list of strings"
  type        = list(string)
  default     = ["value1", "value2", "value3"]
}
    

Map


variable "cms_map" {
  description = "A map of values"
  type        = map(string)
  default     = {
    key1 = "value1"
    key2 = "value2"
  }
}
    

How to Define, Use, and Override Variables

Scenario 1: Defining and Using Variables

Let's consider a scenario where you have variables defined in a variables.tf file, and you use these variables in your Terraform configuration:

variables.tf


variable "compartment_id" {
  description = "Root Compartment ID"
  type        = string
}

variable "network_compartment" {
  description = "Network compartment"
  type        = string
}

variable "cmsvcn" {
  description = "My VCN"
  type        = string
}
    

Usage in Configuration


resource "oci_identity_compartment" "compartment" {
  compartment_id = var.compartment_id
  name           = "cmsterraform"
  description    = "Compartment for compute"
}

resource "oci_core_vcn" "cmsvcn" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.0.0/16"
  display_name   = var.cmsvcn
}

resource "oci_core_subnet" "subnet1" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.1.0/24"
  vcn_id         = var.cmsvcn
}

resource "oci_core_subnet" "subnet2" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.2.0/24"
  vcn_id         = var.cmsvcn
  display_name   = "subnet2"
}
    

Scenario 2: Directly Referencing Resources

Sometimes, you may want to directly reference a resource’s attribute without defining a variable for it. Terraform automatically handles dependencies, so you can directly use resource attributes in your configuration:


resource "oci_core_vcn" "cmsvcn" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.0.0/16"
  display_name   = "cmsvcn"
}

resource "oci_core_subnet" "subnet1" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.1.0/24"
  vcn_id         = oci_core_vcn.cmsvcn.id
  display_name   = "subnet1"
}

resource "oci_core_subnet" "subnet2" {
  compartment_id = var.network_compartment
  cidr_block     = "10.0.2.0/24"
  vcn_id         = oci_core_vcn.cmsvcn.id
  display_name   = "subnet2"
}
    

Best Practices for Organizing Variables

Here are some best practices to consider when organizing your variables:

  • Group Related Variables Together: Keep related variables together in the same file or section to make them easier to manage.
  • Use Descriptive Names: Always use meaningful and descriptive names for your variables so that anyone reading the code can easily understand their purpose.
  • Provide Descriptions: Include descriptions for all variables. This helps others (or your future self) understand the role of each variable without needing to guess.
  • Set Defaults Where Possible: If a variable is likely to have the same value across multiple environments, consider setting a default value.
  • Separate Sensitive Variables: Keep sensitive variables (like passwords or API keys) in a separate file and use Terraform’s sensitive attribute to prevent them from being exposed.

Conclusion

Understanding and effectively using variables in Terraform is crucial for writing scalable, reusable, and maintainable infrastructure-as-code. By leveraging variables, you can easily manage different environments, configurations, and resources without hardcoding values, making your code more adaptable and less prone to errors.

In this post, we've explored the various types of variables, how to define and use them, and best practices for organizing them in your Terraform projects. As you continue working with Terraform, incorporating these practices will enhance your ability to manage complex infrastructures efficiently.

In the next part of this series, we will delve into more advanced Terraform topics, further expanding your knowledge and capabilities. Stay tuned, and don't hesitate to share your thoughts or questions in the comments below!

No comments:

Post a Comment