Infrastructure as Code with Terraform [Part 4]

Aaqib AhmadAaqib Ahmad
3 min read

When it comes to building infrastructure as code using Terraform, understanding the concepts of attribute and output is important. In this blog, we will discuss what attributes and outputs are in Terraform and how they are used.

Attributes in Terraform

In Terraform, an attribute is a piece of data associated with a resource that is managed by Terraform. It is a way to specify what values should be used to configure a resource. Attributes are defined within resource blocks and are used to set up the configuration for a given resource. For example, the following is an example of a resource block that creates an Amazon Web Services (AWS) EC2 instance:

resource "aws_instance" "test" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = "subnet-12345678"
}

In this example, ami, instance_type, and subnet_id are all attributes that are being set for the aws_instance resource. These attributes specify which Amazon Machine Image (AMI) to use, which instance type to create, and which subnet the instance should be placed in.

Attributes in Terraform can be either required or optional. Required attributes must be set for the resource to be created, while optional attributes can be left out or set to a default value if desired.

Official doc:

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance

For instance:


Outputs in Terraform

Outputs in Terraform are a way to export data from a Terraform configuration to be used by other parts of the system. Outputs can be used to provide information about a resource that was just created or to provide access to data that was created in a previous Terraform run.

Outputs are defined within an output block and can be set to any value that can be represented in Terraform. For example, the following is an example of an output block that exports the public IP address of an AWS EC2 instance:

output "public_ip" {
  value = aws_instance.test.public_ip
}

In this example, the value of the output is set to the public IP address of the aws_instance resource named test.

Now that we have defined this output, we can use it in other parts of our Terraform configuration or by external systems that are integrating with Terraform. For example, we might want to use the public IP address of the instance in a script that is configuring the instance after it has been created.

To access this output, we can run the terraform output command, which will print the value of the public_ip output:

$ terraform output public_ip
54.123.456.789

In this example, the terraform output command is returning the value of the public_ip output, which is the public IP address of the EC2 instance that we created earlier.

Overall, outputs in Terraform provide a flexible way to export data from a Terraform configuration and can be useful for providing information about resources that have been created, or for sharing data between different parts of a Terraform configuration.


Today's top 3 books to read



“One thing we have to remind ourselves is that the people who think they know everything are the biggest losers in life.”

― Darius Foroux, Do It Today: Overcome Procrastination, Improve Productivity, and Achieve More Meaningful Things


Aaqib Ahmad
DevOps Engineer

0
Subscribe to my newsletter

Read articles from Aaqib Ahmad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aaqib Ahmad
Aaqib Ahmad

Passionate SRE / DevOps Engineer with a zeal to learn, explore and share.