Clicking through the AWS console to create servers works for a first experiment, but it does not scale. Nobody remembers every setting, two environments slowly drift apart, and rebuilding after a mistake takes hours. Terraform solves this by letting you describe your infrastructure in code, review it like any other code, and create the same setup again with one command.
In this beginner guide you will learn how Terraform works and write your first configuration that creates a real AWS resource.
What Is Infrastructure as Code?
Infrastructure as Code (IaC) means you define servers, networks, databases and storage in text files instead of creating them by hand. The files are stored in Git, so you get history, code review and the ability to roll back. Terraform, made by HashiCorp, is one of the most widely used IaC tools, and it works with AWS, Azure, Google Cloud and hundreds of other services through plugins called providers.
Key Terraform Concepts
- Provider — the plugin that talks to a platform, such as AWS.
- Resource — one thing you want to exist, such as an S3 bucket or an EC2 instance.
- Variable — an input that makes your code reusable, such as a region or an instance size.
- Output — a value Terraform prints after it finishes, such as a bucket name or an IP address.
- State — a file where Terraform records what it has created, so it knows what to change next time.
- Module — a reusable package of Terraform code.
Terraform is declarative: you describe the end result you want, and Terraform works out the steps to get there.
Before You Start
- An AWS account. Many resources have a free tier, but always check pricing and delete what you create when you finish.
- The AWS CLI installed and configured with
aws configure, using an IAM user with limited permissions rather than your root account. - Terraform installed. Download it from the official HashiCorp website or use your package manager, then check it with:
Your First Terraform Project
Create an empty folder and add a file called main.tf. This configuration creates one S3 bucket:
S3 bucket names are shared by every AWS customer in the world, so choose something unique such as yourname-terraform-demo-2026.
The Core Terraform Workflow
Almost everything you do with Terraform follows the same four commands:
Always read the plan
The plan is Terraform’s safety net. It lists every resource it will add (+), change (~) or destroy (-). Read it carefully before you type yes, especially in a shared or production account.
After apply, open the S3 service in the AWS console and you will see your bucket. Run terraform plan again and Terraform will report that nothing needs to change, because the real world already matches your code. Change a tag in the file, run apply again, and Terraform updates only that tag.
Understanding the State File
After the first apply you will see a terraform.tfstate file. It is Terraform’s memory of what it manages. Treat it carefully:
- Never edit it by hand and never delete it while resources still exist, or Terraform will lose track of them.
- Never commit it to Git. State can contain sensitive values. Add
*.tfstateand.terraform/to your.gitignore. - Use remote state for teams. Store state in an S3 bucket with encryption and locking so two people cannot change the same infrastructure at once. Locking can use a DynamoDB table or the newer S3-native locking in recent Terraform versions.
Using Variables Files
Typing -var every time is tedious. Create a file called terraform.tfvars:
Terraform loads this file automatically. Keep separate variable files for each environment, such as dev.tfvars and prod.tfvars, and pass the one you want with -var-file.
Beginner Mistakes to Avoid
- Running
applywithout reading the plan. - Hard-coding passwords or access keys in
.tffiles. Use environment variables, IAM roles or a secrets manager. - Changing resources manually in the console after Terraform created them. This causes drift, and the next apply may undo your change.
- Forgetting to run
terraform destroyon practice resources. - Putting everything in one giant file. Split code into
main.tf,variables.tfandoutputs.tfas your project grows.
What to Learn Next
Once the basics feel comfortable, try creating an EC2 instance with a security group, then move on to modules, workspaces and remote state. Terraform pairs naturally with CI/CD: you can run plan on every pull request and apply after approval. See how pipelines work in our GitHub Actions tutorial, and follow the full DevOps roadmap for 2026 to see where Terraform fits.
Learn Terraform and AWS with Live Projects
Our DevOps program includes Terraform, AWS, Docker, Kubernetes and CI/CD, with mentor support.
Apply for the Program →