🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Variable Naming convention

In Terraform, the widely accepted and recommended naming convention for variables is snake_case, which means:

  1. All letters are lowercase.
  2. Words are separated by underscores.
  3. Names should be descriptive and indicate the variable’s purpose or usage.

Following this convention ensures consistency, readability, and easier collaboration, especially in a team environment.

Here are some guidelines and examples:

Guidelines:

  1. Descriptive Names: The variable name should clearly indicate its purpose. It’s often better to have a slightly longer, descriptive name than a short, cryptic one.Good: database_instance_sizeBad: db_sz
  2. Avoid Redundancy: If the context of a variable is clear, avoid using redundant prefixes or suffixes.Good: instance_countBad: ec2_instance_count (if it’s clear that you’re working with EC2 resources)
  3. Plural Names for Lists/Sets: If a variable is expected to be a list or a set, use a plural name.Good: security_group_ids
  4. No Uppercase or CamelCase: Stick to lowercase letters and avoid using uppercase or CamelCase.Bad: InstanceSize, instanceSize

Terraform variable names are not case-sensitive, but it is generally considered best practice to use snake case, with all words separated by underscores. This is the most common naming convention used in Terraform, and it is also consistent with the Terraform configuration language itself.

Here are some examples of good Terraform variable names:

  • my_variable
  • vpc_id
  • instance_type
  • key_name
  • security_groups

You should also avoid using reserved words, single letters, and special characters in your variable names. It is also a good idea to use descriptive names that are easy to understand.

Here are some tips for naming Terraform variables:

  • Use all lowercase letters.
  • Use underscores to separate words.
  • Avoid using special characters.
  • Use descriptive names that are easy to understand.
  • Avoid using reserved words.
  • Avoid using single letters.
  • Use abbreviations for common terms.
  • Use prefixes to indicate the type of variable. For example, you could use the prefix account_ for variables that are related to AWS accounts.
https://gist.github.com/devops-school/ea9d36cbbffe0248eef47430ebfcac04
# Good variable names
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
variable "security_group_ids" {
description = "List of security group IDs"
type = list(string)
default = []
}
variable "database_username" {
description = "Username for the database"
type = string
}
# Bad variable names
variable "dbUsr" {
description = "Username for the database"
type = string
}
variable "sgID" {
description = "Security group ID"
type = string
}
# Here are some additional examples of good Terraform variable names:
variable "account_id" {
type = string
}
variable "vpc_id" {
type = string
description = "The ID of the VPC in the ${var.account_name} account."
}
variable "instance_type" {
type = string
description = "The type of instance to launch in the ${var.account_name} account."
}
variable "key_name" {
type = string
description = "The name of the SSH key pair to use for SSH access to the instances."
}
variable "security_groups" {
type = list(string)
description = "A list of security group IDs to associate with the instances."
}
Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x