Terraform Output

Terraform Output

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed terraform data source.


In this blog post, we will discuss terraform output.

What is Terraform output?

Outputs define values that will be highlighted to the user when Terraform applies, and can be queried easily using the output command. Terraform knows a lot about the infrastructure it manages. Most resources have attributes associated with them, and outputs are a way to easily extract and query that information.

Output Syntax:

output "<name>"
  value = "<output_value>" 
    #The value of the output. This can be a string, list, or map.
  depends_on = ["<resource_type>.<resource_local_name>"]
    #resource dependencies that Terraform can"t automatically infer.
    #required only if dependent resource is not accessed by the resource
  description = "<output_description>"
    #A human-friendly description for the output.
  sensitive = true 
    #when true, output value replaced by sensitive

Output demo:

#create a directory and get inside it
mkdir terraform && cd terraform

#create main.tf
vi main.tf
#Create a VPC with subnets in each availability zones
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "ap-south-1"
# Declare a data source
data "aws_availability_zones" "availability_zones" {}
resource "aws_vpc" "myvpc" {
cidr_block = ""
tags {
Name = "myvpc"
resource "aws_subnet" "myvpc_subnets" {
count = "${length(data.aws_availability_zones.availability_zones.names)}" #returns number of AZ's
vpc_id = "${aws_vpc.myvpc.id}"
cidr_block = "${cidrsubnet("${aws_vpc.myvpc.cidr_block}",8,count.index+1)}"
availability_zone = "${element(data.aws_availability_zones.availability_zones.names,count.index)}" #returens AZ's name
tags {
   Name = "myvpc_subnet_${count.index+1}"
output "message" {
value = "VPC and subnets created successfully!"
output "owner_id" {
value = "${aws_vpc.myvpc.owner_id}"
sensitive = true #if ommited, will print aws account id
depends_on = ["aws_vpc.myvpc"]
description = "Returns AWS account details"

#format code
terraform fmt

#initialized terraform
terraform init

#create the resource
terraform apply

#observe output section at the end of apply

#list outputs
terraform output

#destroy the resources
terraform destroy

cd .. && rm -rf terraform

Hope you have enjoyed this article. In the next blog post, we will create a LAMP stack using terraform.

To get more details on terraform, please refer below terraform documentation



Leave a Reply

Close Menu