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??
CodePudding user response:
Why output is used (my view):-
- Terraform output values allow you to export structured data about your resources.
- Outputs are also necessary to share data from a child module to your root module.
- to get information about the resources you have deployed.
- Output values are similar to return values in programming languages.
What is the significance of output.tf file, According to docs
Output values have several uses:
- A child module can use outputs to expose a subset of its resource attributes to a parent module.
- A root module can use outputs to print certain values in the CLI output after running terraform apply.
- When using remote state, root module outputs can be accessed by other configurations via a terraform_remote_state data source.
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
command
how will it affect our code if such a file doesn't exist??
It 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
CodePudding user response:
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 in main.tf
. But using outputs.tf
can be convenient if your tf scripts are large. Its easy to find and inspects your outputs.