Outputting Resource Properties
Learn more about Terraform output and output resource properties.
We'll cover the following
The first example is pretty basic and, in the real world, probably not very useful. Outputs are much more useful when used to output the values of resources that have been created as part of a Terraform run.
Terraform output project example#
Let’s create another Terraform project and output the values of resources as a part of a Terraform run:
Let’s walk through the above code.
-
The
provider
andresource
should be familiar to you. We are simply defining the AWS provider to be used with theeu-west-1
region and setting up an S3 bucket. -
Feel free to change the name of the bucket to whatever you wish.
-
Next, we define an output called
bucket_name
. -
In the
bucket_name
, we are going to output the name of the bucket by using the attribute of the S3 bucket resource that we create. -
We use the same technique to output the ARN of the bucket that we will create in the output
bucket_arn
. -
Because we are directly using the attribute in both of those examples, we can just set it equal to
value
without any quotes. -
The last output
bucket_information
prints an interpolated string which will give us the bucket name and bucketarn
. Since this value is a string with interpolated values, we have to surround it in quotes and${ }
.
Project output#
Clicking the terminal will run terraform init
and then terraform apply
. When prompted, enter yes
to run the project.
Terraform runs, creates the S3 bucket
and gives the following output under the Outputs:
heading:
📝Note: Kindly do change the
bucket
name before running the project.
/
- main.tf
Terraform got the values from the S3 bucket that it created and outputted them when the run completed. Note that Terraform prints the outputs in alphabetical order, not the order that you define them in your project.
Terraform does not care which order you define the
blocks in your project. Try reordering them and running terraform apply
again. You will notice
that Terraform will say that there is nothing to do.
📝Note: Once you are done with the project, do not forget to run
terraform destroy
and then confirm with yes to delete all of the resources in this project.