What is Terraform Resources, Variables & Outputs

What is Terraform Resources, Terraform Variables & Output

This post will help you to understand what is terraform resources, terraform variables and terraform outputs. Each topic is explained with examples. In the previous post, we have already covered Introduction to Terraform and Terraform Basics and Initial Configuration.

If you are interested in learning, Request you to go through the below recommended tutorial.
Watch this tutorial demo on our YouTube Channel

What is Terraform Resources?

Terraform resources are the important section that we define in terraform manifest files. These terraform resources will be used to manage specific services or applications.
resource "resource_name" "name1" {
 key1 = value1
 key2 = value2
}
Lets take an example. If we want to create EC2 instance in AWS cloud.  Then we have to use the terraform resources which will help us to manage AWS EC2 on AWS along with attributes like AMI ID, Instance type, Subnet ID, Security Group and so on.
resource "aws_instance" "web1" {
  ami                         = "ami-0e86e20dae9224db8"
  instance_type               = "t2.micro"
  subnet_id                   = "subnet-0105b1aef1e7755cd"
  key_name                    = "demov1"
  associate_public_ip_address = "true"
  vpc_security_group_ids      = ["sg-04dd813e22c5a0b2f"]
  tags = {
    Name = "Web Server1"
  }
}
Where, 
1. "aws_instance" is the resource type provided by terraform to manage AWS EC2 instance. Similarly we may have more resources types to manage different resources like AWS S3, AWS EBS, AWS VPC, etc,.

We have to find the correct resource types that is suitable to create the terraform resources as per the request. These resources can be find in terraform registry.

2. "web1" is the unique resource name should be used, this will be useful for terraform to manage the resources. For example, if we want to create multiple AWS EC2 instance, we cannot provide name like just "web". We have to call each resource with unique names like web1, web2, user1, user2.

3. Within resource section, lot of attributes are defined that are required to create the specific resources. 

Lets say, we want to create AWS S3 bucket. On this case, we have to use the terraform resources called "aws_s3_bucket". We found these resource name from terraform registry and its supporting attributes.
resource "aws_s3_bucket" "my_bucket1" {
  bucket = "my-unique-bucket-name-123456"
  acl    = "private"

  tags = {
    Name = "MyBucket"
  }
}
Once we have defined all resources that we need to manage, then we can go for "terraform plan" or "terraform apply" command to proceed further.

Lets move on to Terraform Variables.

Terraform Variables

Generally, variables are used to define a key value pair that will make our codes more flexible and reusable. Once variable is defined, we can refer the variable many times in all the terraform manifests files whereever we want. 

For example, if i create 10 AWS ec2 instances with multiple resource sections with different EBS volume size and AMI ID for our applications. And all our instance types are same type like t2.micro. On this case, we dont want to specify t2.micro in all resource section. Instead. we can refer the variable name that we create for instance type. 

Even this will help us to change the instance type only on variable if we want to modify it. If we dont have such variable option. We have to modify in all resource sections.

Lets see how to define terraform variables. In order to define variables in terraform, we have to create a variable section in any manifests file or create a dedicated variable.tf file.
variable "instance_type" {
  description = "Our basic AWS EC2 instance type"
  type        = string
  default     = "t2.micro"
}
Where,
"instance_type" is the variable name. This can be used in any resources sections whereever we want.
description is used to provide some information about the variable.
type is used to define the type of the resources. Eg, it may be string, integer, number, boolean, list, tuples.
default is the actual value where we define the value for the variable. This will be default value used for the variable, if no variable values is overridden. because, we can override variables using multiple ways, will talk about this in later section.
resource "aws_instance" "web1" {
  ami                         = "ami-0e86e20dae9224db8"
  instance_type               = var.instance_type
  subnet_id                   = "subnet-0105b1aef1e7755cd"
  key_name                    = "demov1"
  associate_public_ip_address = "true"
  vpc_security_group_ids      = ["sg-04dd813e22c5a0b2f"]
  tags = {
    Name = "Web Server1"
  }
}
In the above example, if you see the instance_type attribute value, we used "var.instance_type". If you want to refer variable names, then we have to use the format like "var." then variable name.

As I said before, variable section that we specify in manifest file is the default value will be used. There are different ways to define variables also it has priority in order to override the variables. I will list the same in ascending order.

1. Command-Line Flags: We can pass terraform variables while running the terraform commands like below. This has the high priority.
terraform apply -var="instance_type=t2.micro"
2. Environment Variables: We can set values for variables using environment variables like below. As we use environment variables features of operating system, we have to use the prefix like TF_VAR to differentiate the terraform variables from other environment variables.
export TF_VAR_instance_type="t2.micro"
3. Terraform variables (.tfvars): We can create dedicated file with extension .tfvars where we can define variables in key value pair format like below.
instance_type = "t2.micro"
Lets move on to Terraform output.

Terraform Output

Terraform outputs are used to display the attribute value of any resource after it is created or updated. For example, if we create AWS EC2 isntance using terraform. We are not aware of the the attributes like public ip, private ip before creating the resources. These values will be created on AWS provider. So these values will be issued to terraform only after it is applied. So I want to know the such attributes, we have to login into AWS console and get the information. Instead we can use terraform output block to get the attribute.
output "instance_id" {
  value = aws_instance.web1.id
}

output "instance_public_ip" {
  value = aws_instance.web1.public_ip
}
Above output block will just display the instance ID and Public IP of the resources on the terminal itself.

That's it for this post. Keep practicing and have fun. Leave your comments if any.

Post a Comment

0 Comments