
Let's say you issue the terraform output command and something like this is being returned.
Warning: No outputs found
This should mean the outputs key in your terraform.tfstate file is empty.
~]$ cat terraform.tfstate
{
"version": 4,
"terraform_version": "1.2.6",
"serial": 1,
"outputs": {},
I first happened upon this when I had a root module that contained a child module (vpc in this example).
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
}
}
}
provider "aws" {
access_key = var.access_key
secret_key = var.secret_key
}
module "vpc" {
source = "./vpc"
}
And the child module had outputs.
]$ cat vpc/outputs.tf
output "my_vpcs" {
value = data.aws_vpc.my_vpcs
}
outputs must be in the root module. Thus, what I ended up doing was using a directory structure like this.
├── main.tf
├── outputs.tf
├── vpc (directory)
│ ├── vpc.tf
In the vpc.tf file in the child module, I used "data" to get the JSON of the VPC named my-vpc and then used "output" to store the JSON in a variable named my_vpc.
data "aws_vpc" "my-vpc" {
filter {
name = "tag:Name"
values = ["my-vpc"]
}
}
output "my_vpc" {
value = data.aws_vpc.my-vpc
}
And then I had the following in outputs.tf in the same directory as my root module.
output "vpc" {
value = module.vpc.my_vpc
}
And then the terraform refresh command gave me the following.
]$ terraform refresh
module.vpc.data.aws_vpc.my_vpcs: Reading...
module.vpc.data.aws_vpc.my_vpcs: Read complete after 1s [id=vpc-014d2fcfa335d3c01]
Outputs:
vpc = {
"arn" = "arn:aws:ec2:us-east-1:713542074252:vpc/vpc-0521ac4b76ed94d8a"
"cidr_block" = "10.0.0.0/16"
"cidr_block_associations" = tolist([
{
"association_id" = "vpc-cidr-assoc-09ff7264a51c86a4b"
"cidr_block" = "10.0.0.0/16"
"state" = "associated"
},
])
"default" = false
"dhcp_options_id" = "dopt-017f0a715e4ce2fc9"
"enable_dns_hostnames" = false
"enable_dns_support" = true
"filter" = toset([
{
"name" = "tag:Name"
"values" = toset([
"my-vpc",
])
},
])
"id" = "vpc-0521ac4b76ed94d8a"
"instance_tenancy" = "default"
"ipv6_association_id" = ""
"ipv6_cidr_block" = ""
"main_route_table_id" = "rtb-091e5416b1c0d33a6"
"owner_id" = "713542074252"
"state" = tostring(null)
"tags" = tomap({
"Name" = "my-vpc"
})
"timeouts" = null /* object */
}
Did you find this article helpful?
If so, consider buying me a coffee over at