Infrastructure as Code (IaC) has revolutionized how we manage cloud resources, and Terraform stands at the forefront of this revolution. In this blog post, we'll explore the essential Terraform commands with practical examples that will help you get started with your IaC journey.
Prerequisites
Terraform installed on your machine
An AWS account
AWS credentials configured
Creating Your First Terraform Configuration
Let's start by creating a simple AWS EC2 instance. Create a new directory and add a file named main.tf
:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}
Essential Terraform Commands in Action
1. terraform init
First, initialize your Terraform working directory:
$ terraform init
This command will output something like:
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws...
...
Terraform has been successfully initialized!
2. terraform init -upgrade
If you want to upgrade your providers to the latest version:
$ terraform init -upgrade
You'll see similar output, but it will specifically check for and download newer versions:
Upgrading provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws...
...
3. terraform validate
Let's check if our configuration is valid:
$ terraform validate
If everything is correct, you'll see:
Success! The configuration is valid.
4. terraform fmt
To ensure our code is properly formatted:
$ terraform fmt
This command will automatically format your files. Let's say you had poorly formatted code:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "terraform-example"
}
}
After running terraform fmt
, it will be properly formatted as shown in our first example.
5. terraform plan
Before making any changes, let's see what Terraform will do:
$ terraform plan
You'll see output like:
Terraform will perform the following actions:
# aws_instance.example will be created
+ resource "aws_instance" "example" {
+ ami = "ami-0c55b159cbfafe1f0"
+ instance_type = "t2.micro"
+ tags = {
+ "Name" = "terraform-example"
}
...
}
Plan: 1 to add, 0 to change, 0 to destroy.
6. terraform apply
Now, let's create our infrastructure:
$ terraform apply
After reviewing the plan and typing "yes", you'll see:
aws_instance.example: Creating...
aws_instance.example: Creation complete after 40s [id=i-0123456789abcdef0]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
7. terraform destroy
When you're done, you can destroy the infrastructure:
$ terraform destroy
After confirming with "yes", you'll see:
aws_instance.example: Destroying...
aws_instance.example: Destruction complete after 30s
Destroy complete! Resources: 1 destroyed.
Advanced Example
Let's look at a slightly more complex example with multiple resources:
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "terraform-vpc"
}
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "terraform-subnet"
}
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
subnet_id = aws_subnet.main.id
tags = {
Name = "terraform-example"
}
}
This configuration creates:
A VPC
A subnet within the VPC
An EC2 instance in the subnet
The same commands we learned earlier work exactly the same way with this more complex configuration.
Best Practices
Always run
terraform plan
beforeterraform apply
Use version control (like Git) for your Terraform configurations
Use
terraform fmt
before committing codeRegularly run
terraform validate
to catch errors earlyConsider using
-auto-approve
with caution (e.g.,terraform apply -auto-approve
)
Conclusion
These essential Terraform commands form the backbone of infrastructure as code workflows. While we've covered the basics here, Terraform offers many more advanced features like workspaces, state management, and modules that you can explore as you become more comfortable with these fundamental commands.
Remember to always review the changes Terraform plans to make before applying them, especially in production environments. Infrastructure as Code is powerful, but with great power comes great responsibility!