The problem with outputs created within a child module is that they are not immediately accessible from the root module. So, a terraform apply (run from the root directory) will not output the values to the CLI by default.
To accomplish that we would need a second output—one written in the root module that calls on the output in the child module.
This video shows how to configure it so that the output in the child module is displayed in the terminal. See below for video and code.
I'm working with IAM users here because they build quickly and cost nothing (at least in my region as of the writing of this post).
Directory/file structure
./tf-test
main.tf
/users_module
users.tf
main.tf
# main.tf (within the main working directory/root module)
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.3.6"
}
provider "aws" {
region = "us-east-2"
}
module "users_module" {
source = "./users-module"
}
output "user_names" {
value = module.users_module.name_of_all_users
}
users.tf
# users.tf (within the users_module child module/directory)
resource "aws_iam_user" "test_user" {
name = "person-${count.index}"
count = 3
tags = {
time_created = timestamp()
department = "OPS"
}
}
output "name_of_all_users" {
value = aws_iam_user.test_user[*].name
}
root module output > child module output > CLI
Excellent.
🌎 Terraform Video Course
It's finally published! The HashiCorp Certified Terraform Associate (003) video course is ready for viewing on InformIT & O'Reilly*. Check it out at the links below.
InformIT
O'Reilly*