Terraform Tutorial — Querying Outputs

1 minute read

After terraform apply, Terraform stores output values in the state. You can query them anytime without rerunning apply.


terraform output

Display all outputs:

$ terraform output
bucket_arn    = "arn:aws:s3:::my-terraform-bucket-12345"
bucket_name   = "my-terraform-bucket-12345"
bucket_region = "us-east-1"

Display a specific output:

$ terraform output bucket_arn
"arn:aws:s3:::my-terraform-bucket-12345"

Raw Output (No Quotes)

By default, strings include quotes. Use -raw to strip them:

$ terraform output -raw bucket_arn
arn:aws:s3:::my-terraform-bucket-12345

This is useful when piping to other commands:

$ aws s3 ls s3://$(terraform output -raw bucket_name)

JSON Output

Export all outputs as JSON — perfect for scripting:

$ terraform output -json
{
  "bucket_arn": {
    "value": "arn:aws:s3:::my-terraform-bucket-12345",
    "type": "string",
    "sensitive": false
  },
  "bucket_name": {
    "value": "my-terraform-bucket-12345",
    "type": "string",
    "sensitive": false
  }
}

Parse with jq:

$ terraform output -json | jq -r '.bucket_arn.value'
arn:aws:s3:::my-terraform-bucket-12345

Using Outputs in Shell Scripts

#!/bin/bash
# deploy.sh — Deploy app using Terraform outputs

BUCKET=$(terraform output -raw bucket_name)
REGION=$(terraform output -raw bucket_region)

echo "Uploading to s3://${BUCKET} in ${REGION}..."
aws s3 sync ./dist "s3://${BUCKET}" --region "${REGION}"

Using Outputs in CI/CD

In GitHub Actions:

- name: Get Terraform Outputs
  id: tf
  run: |
    echo "bucket_name=$(terraform output -raw bucket_name)" >> "$GITHUB_OUTPUT"

- name: Deploy
  run: |
    aws s3 sync ./dist "s3://${{ steps.tf.outputs.bucket_name }}"

What’s Next?

Some outputs contain secrets. Let’s learn how to mark them as sensitive.


← Output Block Syntax Sensitive Outputs →