Terraform 4 of 10: Output Values using AWS Cloud9

Background

This tutorial is about Terraform Output Values. This tutorial is run over an AWS Cloud9 environment.

1 of 9. Open the Terraform documentation for "Output Values" and "Interpolate variables in strings"

Output Values

Interpolate variables in strings

2 of 9. Open your AWS Cloud9 environment

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

3 of 9. Update resources.tf

Make resources.tf look like the below. Here, we will use a random string to generate the S3 bucket name.

resource "random_string" "bucket_name_suffix" { length = 5 special = false upper = false } resource "aws_s3_bucket" "example1" { bucket = "humangov-tf-test-bucket-${random_string.bucket_name_suffix.result}" tags = { Application = var.application ProjectId = var.projectid } }

4 of 9. Apply the configuration, to create a random bucket.

You know the drill by now. We re-introduced random, so we should init.

terraform init terraform plan terraform apply

5 of 9. Create an outputs.tf to show output values

This will be used to show the name of the S3 bucket. Run terraform apply.

output "bucket_name" { value = aws_s3_bucket.example1.bucket } terraform apply

6 of 9. Update outputs.tf and mark the "bucket_name" as sensitive.

We can mark it as sensitive. This is useful if we don't want to show particular output value(s) in output. Run the terraform apply again.

output "bucket_name" { value = aws_s3_bucket.example1.bucket sensitive = true } terraform apply

7 of 9. Update outputs.tf to concatenate some text with the bucket name.

This is useful if you want to add context to your output, but the interpolation technique can be used elsewhere.

output "bucket_name" { value = "The bucket name is ${aws_s3_bucket.example1.bucket}" } ​ terraform apply

8 of 9. Run "terraform show" and "terraform output"

"show" with no path provided will show the contents of the state file. "output" will show the output.

terraform show terraform output

9 of 9. Cleanup

Run "terraform destroy" to eliminate the resources.

terraform destroy

Reference

Creating An Integrated Developer Environment (IDE) in the Cloud in Two Minutes!: AWS Cloud9(Step-by-Step)

Output Values

Interpolate variables in strings

Command: show

Command: output

Comments

Popular posts from this blog

Orphaned No More: Adopting AWS Lambda

Containing the Chaos! | A Three-Part Series Demonstrating the Usefulness of Containerization to HumanGov