DEV Community

saheed
saheed

Posted on

 Deploying Your First Server with Terraform: A Beginner's Guide

INTRODUCTION
Welcome to Day 3 of my 30-Day Terraform Challenge! In the "ancient" days of software, deploying a server was a manual, fragile process involving physical hardware and manual commands Today, we use Infrastructure as Code (IaC) to automate this entire process using a simple, declarative language if you are just starting out, this guide walks you through the exact steps I took to launch my first virtual server on AWS.

The Compute Resource: aws_instance

In the Terraform world, a virtual server is defined using the aws_instance resource. To get my server running, I had to provide two critical arguments:

AMI (Amazon Machine Image): This is the OS snapshot for the server. I used an Ubuntu 20.04 image

Instance Type: I chose t2.micro, which is perfect for learning because it falls under the AWS Free Tier

One important lesson I learned is that AMI IDs are region-specific
If I move my deployment from us-east-2 to us-west-2, I have to update that ID, or the deployment will fail

The first step in any Terraform project is to define your Provider, which tells Terraform which cloud platform you are using For this challenge, I am using AWS

provider 
"aws" {
  region = "us-east-2"
}
Enter fullscreen mode Exit fullscreen mode

Note: I chose us-east-2 (Ohio), but remember that resources like AMI IDs are region-specific

By default, AWS is a "walled garden" that denies all incoming traffic
To see our web server in action, we must create an aws_security_group to open specific ports

Port 80 (HTTP): To allow users to view our website

Port 22 (SSH): To allow us to securely log into the server for maintenance

The Terraform Lifecycle in Action

With the code ready, I followed the standard Terraform workflow to bring the server to life

terraform init: This prepares the directory and downloads the AWS provider plugin

terraform plan: A "sanity check" dry run. My plan showed 2 resources to add (the instance and the security group)

terraform apply: This executes the changes. After typing yes, Terraform provisioned the server in about 40 seconds

terraform destroy: Once I was done testing, I ran this to cleanly remove all resources and avoid unnecessary AWS costs

CONCLUSION
The most important lesson today was understanding Terraform's declarative nature I don't tell AWS how to build a server; I just describe what I want the server to look like Terraform builds a dependency graph and handles the heavy lifting of figuring out the most efficient way to make it happen I am thrilled to continue this journey alongside the AWS AI/ML UserGroup Kenya, Meru HashiCorp User Group, and EveOps communities.

Top comments (0)