πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform Output Variable Tutorials with Example

Terraform will store hundreds or even thousands of attribute values for all the defined resources in our infrastructure in state file.

An outputed attributes can not only be used for the user reference but it can also act as an input to other resources being created via Terraform.

Output variables provide a convenient way to get useful information about your infrastructure. Terraform output values allow you to export structured data about your resources. You can use this data to configure other parts of your infrastructure with automation tools, or as a data source for another Terraform workspace. Outputs are also necessary to share data from a child module to your root module.

Resource instances managed by Terraform each export attributes whose values can be used elsewhere in configuration. Output values are a way to expose some of that information to the user of your module.

We can use output variables to organize data to be easily queried and shown back to the Terraform user.

While Terraform stores hundreds or thousands of attribute values for all our resources, we are more likely to be interested in a few values of importance, such as a load balancer IP, VPN address, etc.

Output values are like the return values of a Terraform module, and have several uses:

  • A child module can use outputs to expose a subset of its resource attributes to a parent module.
  • A root module can use outputs to print certain values in the CLI output after running terraform apply.
  • When using remote state, root module outputs can be accessed by other configurations via a terraform_remote_state data source.

Terraform example code for output value of string type

String Type – Terraform example code for output value of aws_instance public_dns


output "instance_public_dns" {
  value = aws_instance.example.public_dns
}

Map Type – Terraform example code for output value of aws_instance root_block_device


output "instance_root_block_device" {
  value = aws_instance.example.root_block_device[0].volume_id
}

List Type – Terraform example code for output value of aws_instance security group


output "instance_security_group" {
  value = aws_instance.example.security_groups[0]
}

resource "aws_instance" "example3" {
ami = "ami-007855ac798b5175e"
instance_type = "t2.micro"
}
output "pubip" {
value = aws_instance.example3.public_ip
}
output "dns" {
value = aws_instance.example3.public_dns
}
output "blockdev" {
value = aws_instance.example3.root_block_device[0].volume_id
}
view raw aws.tf hosted with ❀ by GitHub
provider "aws" {
region = "us-east-1"
access_key = "AKIAQ3MZANMSAGUBLIPNMN"
secret_key = "cCALzrQG7RMII7GosylEC70di3D1NBbAOEGAsWhgm"
}
resource "aws_instance" "web" {
ami = "ami-08f3d892de259504d"
instance_type = "t2.micro"
tags = {
Name = "Prod_instance"
}
}
# Let's define an output to show public DNS address
output "address" {
value = aws_instance.web.public_dns
}
view raw main.tf hosted with ❀ by GitHub
provider "aws" {
region = "us-east-1"
access_key = "AKIAQ3MZANMSAGUBLIPNMN"
secret_key = "cCALzrQG7RMII7GosylEC70di3D1NBbAOEGAsWhgm"
}
data "aws_ip_ranges" "nvergenia" {
regions = [ "us-east-1"]
services = [ "ec2" ]
}
output "ipranges_list"{
value = data.aws_ip_ranges.nvergenia.cidr_blocks
}
view raw second.tf hosted with ❀ by GitHub
provider "github" {
token = ""
organization = ""
}
variable "names" {
description = "A list of names"
type = list(string)
default = ["rajesh", "kumar", "xyz"]
}
# Alternate of for each using count
resource "github_repository" "repox" {
count = "${length(var.names)}"
name = "rajesh.${count.index}"
description = "My awesome codebase"
private = false
}
# using for-each
output "upper_names" {
value = [for name in var.names : upper(name) if length(name) < 5]
}
# Alternate of for each using count
resource "github_repository" "repox" {
count = "${length(var.names)}"
name = "${var.names[count.index]}"
description = "My awesome codebase"
private = false
}

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.