I’m learning more about terraform and AWS. I’ve seen a code in Stack overflow- Outputs not displaying when using modules
Main.tf
module "identity-provider" {
source = "./modules/identity-provider"
}
module "saml-role1" {
source = "./modules/saml-roles/"
}
Module file
resource "aws_iam_role" "role1" {
name = "saml-role1"
description = "Blah Blah"
path = "/"
assume_role_policy = "${data.aws_iam_policy_document.assume_role.json}"
permissions_boundary = ""
max_session_duration = 43200
resource "aws_iam_role_policy_attachment" "Read-Only" {
role = "${aws_iam_role.role1.name}"
policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}
Output.tf
output "Role1-ARN" {
value = "${module.saml-role1.arn}"
}
My doubt is
What is the significance of output.tf file and how will it affect our code if such a file doesn’t exist??
2
Answers
Why output is used (my view):-
Output values have several uses:
Output declarations can appear anywhere in Terraform configuration files. However putting them into a separate file called outputs.tf to make it easier for users to understand your configuration and what outputs to expect from it.
When you apply you can see those values on your terminal, they will be also present in your project’s state, use the terraform output command to query all of them using
terraform output
commandIt will simple not output resource info on command line or a module won’t be able to use it, if child module is referencing from parent module
Putting outputs in a file called
outputs.tf
is just a convention. You don’t have to do it. You may as well put your outputs inmain.tf
. But usingoutputs.tf
can be convenient if your tf scripts are large. Its easy to find and inspects your outputs.