DEV Community

David Nanjila
David Nanjila

Posted on

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

AWS Console showing my provisioned vpc## Introduction

Today marks Day 3 of my 30-Day Terraform Challenge, and what an exciting milestone it's been! I successfully deployed my first web server using Infrastructure as Code (IaC) with Terraform. If you're new to Terraform or cloud infrastructure, this post will walk you through the journey and key learnings from building basic AWS infrastructure from scratch.

What I Accomplished Today

1. Understanding Terraform Fundamentals

  • Provider Blocks: Learned how to configure AWS as my cloud provider
  • Resource Blocks: Mastered creating and managing AWS resources
  • Data Sources: Used data sources to fetch dynamic information like availability zones

2. Built Complete Infrastructure

I created a fully functional web server infrastructure including:

  • VPC (Virtual Private Cloud): My own private network in AWS
  • Public Subnet: Where my server lives with internet access
  • Internet Gateway: Enables internet connectivity
  • Security Group: Acts as a firewall for my server
  • EC2 Instance: The actual web server running Apache

The Architecture

Here's what I built:

Infrusttructure diagram showing what I built

Key Code Highlights

Setting Up the Provider

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

Creating the VPC

resource "aws_vpc" "vpc" {
  cidr_block           = var.vpc_cidr
  enable_dns_hostnames = true
  enable_dns_support   = true

  tags = {
    Name        = var.vpc_name
    Environment = "demo_environment"
    Terraform   = "true"
  }
}
Enter fullscreen mode Exit fullscreen mode

Deploying the Web Server

resource "aws_instance" "web_server" {
  ami                         = data.aws_ami.amazon_linux.id
  instance_type               = var.instance_type
  subnet_id                   = aws_subnet.public_subnet.id
  vpc_security_group_ids      = [aws_security_group.web_server_sg.id]
  associate_public_ip_address = true

  user_data = <<-EOF
    #!/bin/bash
    yum update -y
    yum install -y httpd
    systemctl start httpd
    systemctl enable httpd
    echo "<h1>Hello from your first Terraform server!</h1>" > /var/www/html/index.html
  EOF

  tags = {
    Name        = var.instance_name
    Environment = "demo_environment"
    Terraform   = "true"
  }
}
Enter fullscreen mode Exit fullscreen mode

Challenges I Overcame

1. Invalid Index Error

Got confused with resource naming when referencing subnets:

Error: Invalid index
The given key does not identify an element in this collection value.
Enter fullscreen mode Exit fullscreen mode

Solution: Ensured the key names matched exactly between variable definitions and resource references.

What I Learned

1. Infrastructure as Code Benefits

  • Reproducibility: Can recreate the same infrastructure anywhere
  • Version Control: Infrastructure changes are tracked like code
  • Automation: No more manual clicking in AWS console

2. Terraform Best Practices

  • Use variables for flexibility
  • Tag all resources for better organization
  • Use data sources for dynamic values
  • Keep security groups restrictive but functional

3. AWS Networking Basics

  • VPCs provide isolated network environments
  • Subnets segment your network
  • Internet Gateways enable internet access
  • Security Groups act as virtual firewalls at instance level

The Result

After running terraform apply, I had:

  • ✅ A fully functional web server
  • ✅ Accessible via public IP
  • ✅ Serving a custom HTML page
  • ✅ All infrastructure defined as code

Vs code on the left the server display on the right

The server displays: "Hello from your first Terraform server!" along with its IP and instance ID.

Key Takeaways

  1. Start Simple: Begin with basic infrastructure before adding complexity
  2. Read Error Messages: Terraform provides clear error messages - read them carefully
  3. Use Variables: Makes your code reusable and flexible
  4. Test Incrementally: Apply changes step by step to catch issues early
  5. Document Everything: Good naming and tags make infrastructure manageable

What's Next?

Tomorrow I'll be exploring more advanced Terraform features and expanding this infrastructure. The foundation is solid, and I'm excited to build upon it!

Resources Used

Final Thoughts

Deploying your first server with Terraform feels like magic! What used to require multiple manual steps in the AWS console now happens with a single terraform apply command. The learning curve is worth it - Infrastructure as Code is definitely the way forward.

If you're starting your Terraform journey, don't be intimidated by the errors. They're learning opportunities that make you understand the underlying concepts better.


This post is part of my 30-Day Terraform Challenge. Follow along for more Infrastructure as Code adventures!

Top comments (0)