Terraform Tutorial — Outputs Practice Exercises
Practice declaring and querying Terraform outputs.
Exercise 1: Basic Output
Task: Write an output block that displays the ARN of an S3 bucket resource named aws_s3_bucket.logs.
Answer
output "logs_bucket_arn" {
value = aws_s3_bucket.logs.arn
description = "ARN of the logs S3 bucket"
}
Exercise 2: Sensitive Output
Task: You have a resource aws_db_instance.main with a password attribute that came from a sensitive variable. Write an output for the connection string.
Answer
output "db_connection_string" {
value = "postgresql://admin:${var.db_password}@${aws_db_instance.main.endpoint}/mydb"
sensitive = true
}
The sensitive = true is required because the output references var.db_password, which is a sensitive variable.
Exercise 3: Querying Outputs
Task: You need to get the bucket name from Terraform output and use it in a shell script to upload files. Write the bash commands.
Answer
# Get the bucket name without quotes
BUCKET=$(terraform output -raw bucket_name)
# Upload files
aws s3 sync ./build "s3://${BUCKET}"
The -raw flag strips the surrounding quotes from string outputs.
Exercise 4: Module Outputs
Task: Your VPC module exposes vpc_id and subnet_ids. Write the root module code that calls the VPC module and creates an EC2 instance in the first subnet.
Answer
module "vpc" {
source = "./modules/vpc"
cidr_block = "10.0.0.0/16"
environment = "dev"
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
subnet_id = module.vpc.subnet_ids[0]
tags = {
Name = "web-server"
}
}
output "vpc_id" {
value = module.vpc.vpc_id
}
Chapter Summary
In this chapter, you learned:
- The
output {}block withvalue,description, andsensitive terraform outputto display values,-rawfor scripts,-jsonfor automation- Sensitive outputs hide secrets from plan/apply but are accessible with
-jsonand-raw - Modules communicate through outputs — child exposes, parent consumes with
module.<name>.<output> - Practical output patterns for S3, EC2, RDS, VPC, and aggregate summaries
Next up: State — understanding how Terraform tracks your infrastructure.