Terraform variables - Day 2
In Terraform, variables are a fundamental concept that allows you to parameterize your infrastructure code. Variables enable you to define values that can be passed into your Terraform configurations, making them more flexible and reusable.
Variables are declared in Terraform using the variable
block in a file named variables.tf
Variables in Terraform can be assigned values in multiple ways
Environment variables
Command line flags
From a file (.tfvars)
Variable defaults
In production environment, it is recommended to have both variables.tf
and .tfvars
files. Below is a sample variables.tf file.
type
argument in a variable block allows you to restrict the type of value that will be accepted as the value for a variable. If no type constraint is set then a value of any type is accepted.
description
argument in a variable block allows you to provide a description about the variable.
The best way to define values for the variables is in terraform.tfvars
file. If you have different name, then you should explicitly specify the .tfvars file in cli, like, terraform plan -var-file="customfile.tfvars"
. Below is a sample terraform.tfvars file.
Once a variable is declared, you can use it in your Terraform configuration as below.
Output Variables: Output variables are used to expose certain values from your infrastructure so that they can be easily consumed by other Terraform configurations or scripts.
The output variable's value will be displayed in the later stage when executing the terraform apply
command.
You can also use terraform output
command to extract the value of an output variable from the state file.
Here is the EC2 instance that was created in the AWS console using the Terraform configuration above.
Difference between resource
and data source
in terraform
Resource:
Purpose: Resources in Terraform represent infrastructure components that you want to manage, such as servers, networks, or databases. They are entities that Terraform can create, update, or delete.
Declaration: Resources are declared using the
resource
block in your Terraform configuration files.Usage: After declaring a resource, Terraform can use it to provision and manage the corresponding infrastructure.
resource "aws_instance" "dev" { ami = "ami-05fb0b8c1424f266b" instance_type = "t2.micro" }
Data:
Purpose: Data sources in Terraform are used to fetch and use information from existing infrastructure or external sources. They do not create or manage resources but provide a way to incorporate external data into your Terraform configuration.
Declaration: Data sources are declared using the
data
block. Examples include fetching information about an AWS AMI, VPC, or subnet.
data "aws_ami" "latest_amazon_linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
- Usage: Once the data source is defined, you can reference its attributes in other parts of your configuration. For example, using the AMI ID fetched from the data source you can use it in the
aws_instance
resource as below.
resource "aws_instance" "dev" {
ami = data.aws_ami.latest_amazon_linux.id
instance_type = "t2.micro"
}