Updated: Aug 9, 2020

Hello Readers, This blog is the Part-3 of our Terraform course series. In case you missed previous blogs please check out parts one and two of it before going further.


  • If in case we need to display values of the resources and components that are created with Terraform, we can output them o screen instead of going to the console and manually fetching the information.

  • An output can be an input to another resource.

  • All resource attributes can be outputted

  • Below is an example of such a scenario.

On doing a terraform apply, attributes will be output

Referencing values in terraform code

We can reference an attribute value from a resource to be used somewhere else

Here we will discuss a scenario where an ec2 instance will be created, an elastic IP will be created as well and eip will be associated with the ec2 instance.


To keep the things dynamic in terraform configuration and not hardcoding the values, we can make use of variables.

We can define variables in a separate file and refer them to the code.

Approaches of variable Assignment

  • Variables can be defined and mentioned in a number of ways.

  • Default file is (To define variables and default values)

  • To specify explicit values, we have another file called terraform.tfvars

  • terraform.tfvars is the default file name, if we have a custom file name, we can mention it with command

terraform plan -var-file="custom-tfvars"

Variable data types

We can explicitly define the data type of a variable; this will restrict the variable to accept only that specific type of value.

variable "region-name" {
  default = "us-east-1"
  type = string

Here region-name variable can only accept string data type and if provided with any other data type, it will throw an error.

Variable data types supported by terraform

Terraform supports a variety of data types like string, map, number, bool, list tuple.

To read in detail, please follow the below link

  1. string

  2. map

  3. number

  4. bool

  5. list

  6. any

  7. tuple


A conditional expression uses the value of a bool expression to select one of two values.

Syntax of Conditional expression:

condition ? true_val : false_val

If the condition is true then the result is true_val. If the condition is false then the result is false_val.

Let’s assume that there are two resource blocks as part of Terraform configuration.

Depending on the variable value, one of the resource blocks will run.


A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it.

  • Local values can be helpful to avoid repeating the same values or expressions multiple times in a configuration.

  • If overused they can also make a configuration hard to read by future maintainers by hiding the actual values used

  • Use local values only in moderation, in situations where a single value or result is used in many places and that value is likely to be changed in the future.


  • The Terraform language includes a number of built-in functions that you can use to transform and combine values.

  • The general syntax for function calls is a function name followed by comma-separated arguments in parentheses:

function (argument1, argument2)


> max(5, 12, 9)

The Terraform language does not support user-defined functions, and so only the functions built into the language are available for use.

  • Numeric

  • String

  • Collection

  • Encoding

  • Filesystem

  • Date and Time

  • Hash and Crypto

  • IP Network

  • Type Conversion

There is practically a large number of functions that terraform supports, to check how these work, we can make use of terraform console command and test them out.

Code is available at our Github repository CLICK HERE

Thanks for reading. Hope it's beneficial for you.

769 views0 comments

Recent Posts

See All