Terraform Tutorial — Installation Practice Exercises

3 minute read

Verify your Terraform installation and cloud credentials with these hands-on exercises.


Exercise 1: Version Check

Task: Run the appropriate command to display your Terraform version, then answer: what operating system and architecture does the output show?

Answer
$ terraform version
Terraform v1.7.5
on linux_amd64

The second line shows your OS and CPU architecture:

  • linux_amd64 — Linux on Intel/AMD 64-bit
  • darwin_arm64 — macOS on Apple Silicon (M1/M2/M3)
  • darwin_amd64 — macOS on Intel
  • windows_amd64 — Windows on Intel/AMD 64-bit

Exercise 2: Command Discovery

Task: Use terraform -help to discover all available subcommands. List the five commands you think you’ll use most frequently and what each one does.

Answer

The five most frequently used commands:

Command Purpose
terraform init Initialize the working directory — downloads providers and modules
terraform plan Preview changes — shows what will be created, updated, or destroyed
terraform apply Apply changes — creates or updates real infrastructure
terraform destroy Destroy all resources managed by this configuration
terraform fmt Format .tf files to canonical style

Other commonly used: validate, output, state, import.


Exercise 3: Credential Verification

Task: Verify your AWS credentials are working by running the appropriate AWS CLI command. What information does it return?

Answer
$ aws sts get-caller-identity
{
    "UserId": "AIDAIOSFODNN7EXAMPLE",
    "Account": "123456789012",
    "Arn": "arn:aws:iam::123456789012:user/terraform-user"
}

This returns:

  • UserId — Your IAM user or role ID
  • Account — Your AWS account number
  • Arn — The full ARN of the IAM identity Terraform will use

If this command fails, your credentials are not configured correctly. Review the Setting Up Cloud Credentials page.


Exercise 4: Project Scaffold

Task: Create a new Terraform project directory with the standard file layout. Create empty files for: main.tf, variables.tf, outputs.tf, providers.tf, versions.tf, and a .gitignore with the recommended Terraform ignores.

Answer
$ mkdir my-first-terraform-project && cd my-first-terraform-project

$ touch main.tf variables.tf outputs.tf providers.tf versions.tf

$ cat > .gitignore << 'EOF'
**/.terraform/*
*.tfstate
*.tfstate.*
crash.log
crash.*.log
*.tfvars
*.tfvars.json
override.tf
override.tf.json
*_override.tf
*_override.tf.json
.terraformrc
terraform.rc
EOF

$ ls -la
total 0
drwxr-xr-x  8 user user  256 Jan 15 10:00 .
drwxr-xr-x  5 user user  160 Jan 15 10:00 ..
-rw-r--r--  1 user user  200 Jan 15 10:00 .gitignore
-rw-r--r--  1 user user    0 Jan 15 10:00 main.tf
-rw-r--r--  1 user user    0 Jan 15 10:00 outputs.tf
-rw-r--r--  1 user user    0 Jan 15 10:00 providers.tf
-rw-r--r--  1 user user    0 Jan 15 10:00 variables.tf
-rw-r--r--  1 user user    0 Jan 15 10:00 versions.tf

Exercise 5: Init and Explore

Task: In your new project directory, add this minimal provider config to providers.tf and run terraform init. Then explore the .terraform/ directory — what files were created?

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
  required_version = ">= 1.7.0"
}

provider "aws" {
  region = "us-east-1"
}
Answer
$ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding hashicorp/aws versions matching "~> 5.0"...
- Installing hashicorp/aws v5.31.0...
- Installed hashicorp/aws v5.31.0 (signed by HashiCorp)

Terraform has been successfully initialized!

Exploring the .terraform/ directory:

$ find .terraform -type f
.terraform/providers/registry.terraform.io/hashicorp/aws/5.31.0/linux_amd64/terraform-provider-aws_v5.31.0_x5

$ ls -la .terraform.lock.hcl
-rw-r--r--  1 user user  1234 Jan 15 10:01 .terraform.lock.hcl

The .terraform/ directory contains the downloaded AWS provider binary. The .terraform.lock.hcl file records the exact version and checksums.


Chapter Summary

In this chapter, you:

  • Installed Terraform on your operating system (Linux, macOS, or Windows)
  • Learned about tfenv for managing multiple Terraform versions
  • Configured cloud credentials for AWS (and optionally GCP or Azure)
  • Set up VS Code with the HashiCorp Terraform extension for syntax highlighting and auto-complete
  • Created a project with the standard directory layout and .gitignore
  • Ran terraform init for the first time and explored the generated files

Next up: HCL Syntax — the language you write Terraform configurations in.


← Project Directory Layout HCL Syntax →