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

ALLAN CHERUIYOTALLAN CHERUIYOT
2 min read

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

Terraform is a popular Infrastructure as Code (IaC) tool that helps you provision and manage cloud resources safely and efficiently. In this guide, you’ll learn how to deploy your very first server using Terraform.


What You’ll Need

  • A cloud provider account (e.g., AWS, Azure, Google Cloud) ,for my case i use AWS.

  • Terraform installed on your computer.

  • Basic understanding of command line and cloud concepts.


Step 1: Set Up Your Terraform Project

  1. Create a new directory for your Terraform project:

     mkdir terraform
     cd terraform
    
  2. Create a file named main.tf. This will contain the configuration for your server.


Step 2: Write Your Terraform Configuration

Here’s an example to create a simple EC2 instance on AWS:

# Specify the provider and region
provider "aws" {
  region = "us-east-1"
}

# Create a new EC2 instance
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"  # Amazon Linux 2 AMI (update with latest AMI ID)
  instance_type = "t2.micro"

  tags = {
    Name = "MyFirstServer"
  }
}

Note: The AMI ID should be updated based on your region and OS preference. You can find the latest AMI IDs on AWS documentation or console.


Step 3: Initialize Terraform

In your terminal, run:

terraform init

This downloads the necessary provider plugins (e.g., AWS) for your project.


Step 4: Preview What Terraform Will Do

Run:

terraform plan

Terraform shows you the actions it will take to create your resources. Review and confirm they match your expectations.


Step 5: Deploy Your Server

Run:

terraform apply

Terraform will ask for confirmation. Type yes to proceed.

Terraform will then provision your server on the cloud.


Step 6: Verify Your Server

After deployment, Terraform outputs the server details. You can also check your cloud provider’s console to see the new instance running.


Step 7: Clean Up (Optional)

To avoid charges, destroy the server when you’re done:

terraform destroy

Confirm with yes and Terraform will remove the server.

0
Subscribe to my newsletter

Read articles from ALLAN CHERUIYOT directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

ALLAN CHERUIYOT
ALLAN CHERUIYOT