DEV Community

Teniola
Teniola

Posted on

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

My name is Tennie, and I began my DevOps journey a few months ago. I am passionate about continuous learning, and I recently joined the Terraform 30-Day Challenge to deepen my understanding and sharpen my skills in infrastructure automation. My goal for this challenge is to become proficient in Terraform, master its concepts and applications, and ultimately achieve certification. For the next 30 days, I will be updating my progress as I take on this exciting learning journey!

Day 3
In this article i will be explaining Deploying Your First Server with Terraform as a beginner

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and provision your infrastructure using declarative configuration files. This guide will walk you through the steps to deploy your first server using Terraform. By the end of this article, you’ll have a basic understanding of how Terraform works and a running server.

Prerequisites

Before you begin, ensure you have the following:

Terraform Installed: Download and install Terraform from Terraform’s official website.

Cloud Provider Account: Create an account with your preferred cloud provider (e.g., AWS

Access Credentials: Set up access credentials for your cloud provider.

Code Editor: Use a code editor like Visual Studio Code.

Step 1: Initialize Your Terraform Project

Create a Project Directory:

mkdir terraform-server-deploy && cd terraform-server-deploy

Write Your First Configuration File:

Create a file named main.tf and open it in your editor. Add the following:

provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"

instance_type = "t2.micro"

tags = {
Name = "TerraformExampleServer"
}
}

Replace the ami value with an appropriate AMI ID for your region.

Initialize the Directory:
Run the following command to initialize your project:

terraform init
Enter fullscreen mode Exit fullscreen mode

This downloads the necessary provider plugins.

Step 2: Preview and Apply the Configuration

Preview Changes:
Run terraform plan to see what Terraform will create:

terraform plan
Enter fullscreen mode Exit fullscreen mode

Apply the Configuration:
Use the following command to create the server:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Review the proposed changes and type yes to confirm.

Step 3: Verify the Server

Once the terraform apply command completes, you’ll see output with information about the resources created. Use your cloud provider’s console to confirm the instance is running.

Step 4: Clean Up Resources

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

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Type yes to confirm the destruction of resources.

Top comments (0)