Bootstrap FreeKB - Terraform - Print output from resource block
Terraform - Print output from resource block

Updated:   |  Terraform articles

output can be used to output text to the console.

Typically:

  • data block is used to get JSON of a resource that was NOT created by Terraform (not in the terraform.tfstate file)
  • resource block is used to get JSON of a resource what was created by Terraform (in the terraform.tfstate file)

For example, let's say you have the following root and child modules.

  • /usr/local/terraform/aws/main.tf (main root module)
  • /usr/local/terraform/aws/outputs.tf (outputs root module)
  • /usr/local/terraform/aws/vpc/resources.tf (child resources module)

output must be in the same directory as the root module. If you only have output in child modules, you'll probably get something like No outputs found.

Let's say you have the following files on your Terraform server.

├── main.tf (root module)
├── data.tf
├── locals.tf
├── outputs.tf
├── providers.tf
├── terraform.tfstate
├── variables.tf
├── child (directory)
│   ├── main.tf (child module)
│   ├── data.tf
│   ├── outputs.tf
│   ├── resources.tf

 

In this scenario, let's say you have the following in resources.tf in your child module to create/update/delete Amazon Web Services (AWS) Virtual Private Clouds (VPC).

resource "aws_vpc" "my-vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "my-vpc"
  }
}

 

And outputs.tf in your child module defines the "my_vpc" output.

output "my_vpc" {
  value = aws_vpc.my-vpc
}

 

AVOID TROUBLE

Before you can output the JSON for a resource, you must create a resource using the terraform apply command.

Only output from the root module will be displayed by commands such as terraform refresh, terraform plan, terrafrom apply and terraform output.

And perhaps outputs.tf in the same directory as your main root module (main.tf) gets the "my_vpc" output from the child module.

output "my-vpc-output" {
  value = module.child.my_vpc
}

 

Something like this should be returned.

my-vpc-output = {
  "arn" = "arn:aws:ec2:us-east-1:713542074252:vpc/vpc-0f38b9ae9695053f5"
  "assign_generated_ipv6_cidr_block" = false
  "cidr_block" = "10.0.0.0/16"
  "default_network_acl_id" = "acl-0e5bd2f2af55a2b02"
  "default_route_table_id" = "rtb-0e216f663a8a28bad"
  "default_security_group_id" = "sg-097a6ffba98122cf3"
  "dhcp_options_id" = "dopt-017f0a715e4ce2fc9"
  "enable_classiclink" = false
  "enable_classiclink_dns_support" = false
  "enable_dns_hostnames" = false
  "enable_dns_support" = true
  "id" = "vpc-0f38b9ae9695053f5"
  "instance_tenancy" = "default"
  "ipv4_ipam_pool_id" = tostring(null)
  "ipv4_netmask_length" = tonumber(null)
  "ipv6_association_id" = ""
  "ipv6_cidr_block" = ""
  "ipv6_cidr_block_network_border_group" = ""
  "ipv6_ipam_pool_id" = ""
  "ipv6_netmask_length" = 0
  "main_route_table_id" = "rtb-0e216f663a8a28bad"
  "owner_id" = "713542074252"
  "tags" = tomap({
    "Name" = "my-vpc"
  })
  "tags_all" = tomap({
    "Name" = "my-vpc"
  })
}

 

Or just output the ID of the VPC.

output "my-vpc-id" {
  value = aws_vpc.my-vpc.id
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 6d132b in the box below so that we can be sure you are a human.