Your First Terraform Configuration
You'll learn: How to create your first Terraform file, initialize a project, and understand the basic workflow that brings infrastructure to life.
The Basic Terraform Workflow
Think of Terraform like following a recipe: you write down what you want (the configuration), gather your ingredients (initialization), and then cook the meal (apply). Every Terraform project follows this three-step pattern:
- Write your configuration in
.tffiles - Initialize the project with
terraform init - Apply the changes with
terraform planandterraform apply
Creating Your First .tf File
A Terraform configuration file uses the .tf extension. Here's the simplest possible example, a file named main.tf:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
This declares which provider you'll use (remember providers from the previous lesson?) and configures it with a region.
Understanding terraform init
Before Terraform can do anything, run terraform init in your project directory. This command:
- Downloads the provider plugins you specified
- Initializes the backend (state storage)
- Prepares your working directory
You only need to run init once per project, or when you add new providers.
The Working Directory
Terraform treats each directory containing .tf files as a project. All .tf files in that directory are automatically combined during execution—you don't need to explicitly reference them.
Key Takeaway: Your first Terraform configuration starts with a .tf file declaring providers, followed by terraform init to download dependencies and set up your workspace—this initialization step is the foundation of every Terraform project.