Introduction
What if provisioning a server took less time than brewing your morning coffee?
That's the promise of Infrastructure as Code (IaC) and Terraform makes good on it. Instead of clicking through cloud dashboards or memorizing provider specific CLIs, you write a configuration file, run two commands, and your infrastructure exists.
In this guide, we'll build something real: a running EC2 instance on AWS, defined entirely in Terraform. You don't need a background in DevOps or cloud architecture — just a willingness to write a little code and watch it come to life.
1. Setting up your project
Create a directory for your project:
mkdir terraform-first-server
2. Create your terraform file
Inside your directory create a file main.tf. Inside configure a provider you want to use.
provider "aws" {
region = "us-east-2"
}
This tells Terraform that you are going to be using AWS as your provider and that you want to deploy your infrastructure into the us-east-2 region.
3. Add resources to your provider
For each type of provider, there are many different kinds of resources that you can create, such as servers, databases, and load balancers.
resource "aws_instance" "example" {
ami = "ami-0fb653ca2d3203ac1"
instance_type = "t2.micro"
}
4. Initialize Terraform
In your terminal, run:
terraform init
the command tells Terraform to scan the code, figure out which providers you’re using, and download the code for them.
5. Checkout the plan
Run the command below (Optional):
terraform plan
The plan command lets you see what Terraform will do before actually making any changes. This is a great way to sanity-check your code before unleashing it onto
the world.
6. Create instance
To actually create the Instance, run the the command:
terraform apply
You’ll notice that the apply command shows you the same plan output and asks you to confirm whether you actually want to proceed with this plan. So, while plan is available as a separate command, it’s mainly useful for quick sanity checks and during code reviews.
Congrats, you’ve just deployed an EC2 Instance in your AWS account using Terraform! To verify this, head over to the EC2 console,
7. Destroy the instance
To avoid extra AWS charges, destroy the infrastructure when you’re done:
terraform destroy
Conclusion
What started as a configuration file is now a live server running in the cloud. No console clicking, no guesswork, no waiting on someone else to provision an environment for you. Just code, a couple of commands, and a result you can see, version, and reproduce.
The coffee's probably still warm. Not bad for an afternoon's work.
Top comments (0)