Terraform Output
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform data source.
https://cloudaffaire.com/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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Syntax output " { value = " #The value of the output. This can be a string, list, or map. depends_on = [" #resource dependencies that Terraform can"t automatically infer. #required only if dependent resource is not accessed by the resource description = " #A human-friendly description for the output. sensitive = true #when true, output value replaced by sensitive } |
Output demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
##----------------------- ## Terraform: Output ## ##----------------------- ## 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 = " secret_key = " region = "ap-south-1" } # Declare a data source data "aws_availability_zones" "availability_zones" {} resource "aws_vpc" "myvpc" { cidr_block = "10.0.0.0/16" 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" } ---------------- :wq ## Format code terraform fmt ## Initialize 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 ## Cleanup cd .. && rm -rf terraform |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform provisioner.
To get more details on terraform, please refer below terraform documentation
https://www.terraform.io/docs/index.html