{"id":2029,"date":"2024-05-29T22:23:27","date_gmt":"2024-05-29T20:23:27","guid":{"rendered":"https:\/\/robermb.com\/blog\/?p=2029"},"modified":"2024-05-29T22:36:59","modified_gmt":"2024-05-29T20:36:59","slug":"variables-in-terraform","status":"publish","type":"post","link":"https:\/\/robermb.com\/blog\/geeks\/variables-in-terraform\/","title":{"rendered":"Variables in Terraform"},"content":{"rendered":"\n<p>In Terraform, variables are a crucial part that allows you to parameterize your infrastructure configuration, making your code more reusable and manageable. Below, I&#8217;ll explain what variables are in Terraform, how to define them, how to use them, and provide some practical examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What are Variables in Terraform?<\/h2>\n\n\n\n<p>Variables in Terraform allow code reuse by abstracting values that can change across different environments, such as credentials, resource names, instance sizes, etc. This makes infrastructure configuration and maintenance easier.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Defining Variables<\/h2>\n\n\n\n<p>To define variables in Terraform, you use the&nbsp;<code>variables.tf<\/code>&nbsp;file. Here, you specify variables with their name, type, description, and default values (if any). The basic syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable \"variable_name\" {\n  type        = data_type\n  description = \"Description of the variable\"\n  default     = default_value\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Common Data Types<\/h2>\n\n\n\n<ul>\n<li><code>string<\/code>: a text string.<\/li>\n\n\n\n<li><code>number<\/code>: a number.<\/li>\n\n\n\n<li><code>bool<\/code>: a boolean value.<\/li>\n\n\n\n<li><code>list<\/code>: a list of values.<\/li>\n\n\n\n<li><code>map<\/code>: a set of key-value pairs.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Variable Definitions<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>variable \"region\" {\n  type        = string\n  description = \"The region where resources will be deployed\"\n  default     = \"us-west-1\"\n}\n\nvariable \"instance_type\" {\n  type        = string\n  description = \"The instance type for the servers\"\n  default     = \"t2.micro\"\n}\n\nvariable \"allowed_ips\" {\n  type        = list(string)\n  description = \"List of IPs allowed to access\"\n  default     = &#91;\"0.0.0.0\/0\"]\n}\n\nvariable \"tags\" {\n  type        = map(string)\n  description = \"Tags for the resources\"\n  default     = {\n    Environment = \"Dev\"\n    Project     = \"TerraformProject\"\n  }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Variables<\/h2>\n\n\n\n<p>To use variables within Terraform configuration files (<code>*.tf<\/code>), you use the syntax&nbsp;<code>${var.variable_name}<\/code>. Here&#8217;s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>provider \"aws\" {\n  region = var.region\n}\n\nresource \"aws_instance\" \"example\" {\n  ami           = \"ami-0c55b159cbfafe1f0\"\n  instance_type = var.instance_type\n\n  tags = var.tags\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Assigning Values to Variables<\/h2>\n\n\n\n<p>There are several ways to assign values to variables in Terraform:<\/p>\n\n\n\n<p>1. <strong>Default Values<\/strong>: If a variable has a default value, it will be used unless overridden.<br>2. <strong><code>terraform.tfvars<\/code>\u00a0or\u00a0<code>*.auto.tfvars<\/code>\u00a0File<\/strong>: You can create a\u00a0<code><strong>terraform.tfvars<\/strong><\/code>\u00a0file to define specific values for your variables.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region       = \"us-east-1\"\ninstance_type = \"m4.large\"<\/code><\/pre>\n\n\n\n<p>3. <strong>Environment Variables<\/strong>: Terraform can read environment variables that start with\u00a0<code>TF_VAR_<\/code>. For example,\u00a0<code>TF_VAR_region=\"us-east-1\"<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ export TF_VAR_filename=\"\/root\/myfile.txt\" \n$ export TF_VAR_content=\"Mr Robot\" \n$ terraform apply<\/code><\/pre>\n\n\n\n<p>4. <strong>Command-Line Flags<\/strong>: You can pass variables directly on the command line when running Terraform:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>terraform apply -var=\"region=us-east-1\" -var=\"instance_type=m4.large\"<\/code><\/pre>\n\n\n\n<p>5. <strong>Interactive Input<\/strong>: If a value is not provided, Terraform will prompt the user for input during execution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete Example<\/h2>\n\n\n\n<p>Imagine you have a&nbsp;<code>main.tf<\/code>&nbsp;file that defines an EC2 instance on AWS:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>provider \"aws\" {\n  region = var.region\n}\n\nresource \"aws_instance\" \"example\" {\n  ami           = \"ami-0c55b159cbfafe1f0\"\n  instance_type = var.instance_type\n\n  tags = var.tags\n}<\/code><\/pre>\n\n\n\n<p>And a&nbsp;<code>variables.tf<\/code>&nbsp;file where you define the necessary variables:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>variable \"region\" {\n  type        = string\n  description = \"The region where resources will be deployed\"\n  default     = \"us-west-1\"\n}\n\nvariable \"instance_type\" {\n  type        = string\n  description = \"The instance type for the servers\"\n  default     = \"t2.micro\"\n}\n\nvariable \"tags\" {\n  type        = map(string)\n  description = \"Tags for the resources\"\n  default     = {\n    Environment = \"Dev\"\n    Project     = \"TerraformProject\"\n  }\n}<\/code><\/pre>\n\n\n\n<p>Finally, you can have a&nbsp;<code>terraform.tfvars<\/code>&nbsp;file to override some values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>region = \"us-east-1\"\ninstance_type = \"m4.large\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Variables in Terraform are essential for creating reusable and manageable configurations. By defining and using variables, you can parameterize your infrastructure, making it easier to manage and deploy across multiple environments. Use the&nbsp;<code><strong>variables.tf<\/strong><\/code>&nbsp;file to define your variables and assign values through&nbsp;<code><strong>.tfvars<\/strong><\/code>&nbsp;files, environment variables, command-line flags, or default values.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Terraform, variables are a crucial part that allows you to parameterize your infrastructure configuration, making your code more reusable &hellip; <a href=\"https:\/\/robermb.com\/blog\/geeks\/variables-in-terraform\/\" class=\"more-link\">More <span class=\"screen-reader-text\">Variables in Terraform<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[103,2],"tags":[112,126,131],"_links":{"self":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2029"}],"collection":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/comments?post=2029"}],"version-history":[{"count":4,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2029\/revisions"}],"predecessor-version":[{"id":2036,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/posts\/2029\/revisions\/2036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media\/2031"}],"wp:attachment":[{"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/media?parent=2029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/categories?post=2029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robermb.com\/blog\/wp-json\/wp\/v2\/tags?post=2029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}