Technogise
Published in

Technogise

Terraform Zero to Hero: Part 1 Variables

In the previous article of this series — Part 0 — we briefly went through the basics of Terraform. You can check it out here and its accompanying video here.
In this article, we will take a deeper look at variables in Terraform.

We have 3 types of variables:

  1. Input variables: Sends values to Terraform.
  2. Output variables: Retrieves values from Terraform.
  3. Local variables: Assigns names to an expression.

Input Variables

Example Resource

Consider the resource above. We are trying to create a string with 16 characters. It does not have any special or upper case characters. If we had to make any change to any of the values here, it would require us going into the code and making a change, modifying our commit history too.

In any programming language, variables play a central role while building software. Why should Terraform be left out?! 🙂

Let us replace the above snippet with some variables.

Resource replaced with variables

Now the next time you run a terraform plan , it will prompt you to input the values of the 3 variables random_string_length , include_special , and include_upper. You can alternatively pass a variable values file, affectionately known as a tfvars file. Terraform will look for a file in the current folder named as terraform.tfvars for values of the variables. You can pass other files using the --var-file=<file_name>.tfvars argument while executing terraform plan/apply.

Don’t worry if you have not passed in the values of all the variables. For any variable whose value cannot be found either in a tfvar file or as a default value in variable definition, Terraform will prompt you to enter a value at runtime while executing a plan or apply.

Let us look at the other types of variables in Terraform

Output Variables

Relating back to programming languages, a function or a method generally has a return type. It can be void, int, string or an object. Terraform allows us to do that too. Almost every resource has some outputs. They can be found under the “attributes” section of the said resource’s documentation. Any of these attributes can be used as output variable.

Output variables allow us to view and export the results to other resources or programs. On running terraform apply, it will print the output variables values on the console. This is especially useful when you would require the ARN (Amazon Resource Name) or the IDs of some specific resources.

Output variables

The value can be referenced using resource_type.resource_name.attribute_name as seen in the example above. Here we output the ID and the ARN of the S3 Buckets that we had created earlier.

Sample output in console

With output variables out of the way, let’s move on to the local variables

Local Variables

Relating back to programming languages, we have the concept of local variables. Local variables are variables whose scope is limited to the current function only. In Terraform, local variables’ scope is limited to the current context and all the resources defined in current context. They are generally used to build out values. Consider the use case where you would like to define the general tags that all resources in the current context should have. It would be cumbersome to write the value at every resource. Instead one could define a local variable block which builds out the tags depending on the environment, project, resource etc. They can be later referenced using HCL syntax with ease throughout the resources.

Notice how we can use other resources’ outputs inside the local block.

Up next

In the next part of this series (i.e. Part 2), we will explore Data Sources in Terraform. Watch this space… 🙂

Video and code

Each blog in this series is accompanied with a corresponding YouTube video and code on GitHub. Here is the GitHub code and you can find the video below:

Follow me on LinkedIn and Github.

Follow Technogise on LinkedIn, Twitter, YouTube, Meetup for more such insights.

--

--

--

An energetic startup building world class software solutions for global clients.

Recommended from Medium

This awesome CloudFormation feature is not talked about enough!

All in one custom and comprehensive Docker Image for the data engineering developer on Apache Spark

Shader programming 101

What’s the solvable problem?

GCP computing architectures

GEICO turbocharges insurance innovation in the cloud

Fluent Interface Pattern — a simple way to reduce verbose code

INTRODUCING KEPLERSWAP

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Arko Chakraborti

Arko Chakraborti

More from Medium

Terraform+Ansible, Get Dynamic Inventory of EC2

How to Configure Terraform on Windows?

Automate Alibaba Cloud infra building with Terraform (step 2)

OCI Terraform Provider for Apple M1