Deploying Your First Web App Server with Terraform: A Beginner's Guide
Deploying basic infrastructure using Terraform

I'm Vijay, a seasoned professional with over 13 years of expertise. Currently, I work as a Quality Automation Specialist at NatWest Group. In addition to my employment, I am an "AWS Community Builder" in the Serverless Category and have served as a volunteer in AWS UG NCR Delhi and AWS UG MDU, a Pynt Ambassador (Pynt is an API Security Testing tool), and a Browserstack Champion. Actively share my knowledge and thoughts on a variety of topics, including AWS, DevOps, and testing, via blog posts on platforms such as dev.to and Medium. I always like participating in intriguing discussions and actively contributing to the community as a speaker at various events. This amazing experience provides me joy and fulfillment! 🙂
🕊️ In this article, will explain in detail on provider block, resource block, and deployment of web servers using Terraform 🕊️
🎯 Synopsis:
⭐Understand what a provider block and a resource block are, and learn how to design and deploy a web server on AWS
🎯 Flow Diagram:

🎯Provider block:
⭐Terraform code is written in the HashiCorp Configuration Language (HCL) in files with the extension (.tf). It is a declarative language, so your goal is to describe the infrastructure you want, and Terraform will figure out how to create it. Terraform can create infrastructure across a wide variety of platforms, or what it calls providers, including AWS and many others.
⭐The first step to using Terraform is typically to configure the provider(s) you want to use. Create an empty folder and put a file in it called main. tf that contains the following contents:
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.

🎯Resource block:
⭐For each type of provider, there are many different kinds of resources that you can create, such as servers, databases, and load balancers.
resource "<PROVIDER>_<TYPE>" "<NAME>" {
[CONFIG ...]
}
⭐where PROVIDER is the name of a provider (e.g., aws), TYPE is the type of resource to create in that provider (e.g., instance), NAME is an identifier you can use throughout the Terraform code to refer to this resource (e.g., my_instance), and CONFIG consists of one or more arguments that are specific to that resource.
resource "aws_instance" "example" {
ami = "ami-0fb653ca2d3203ac1"
instance_type = "t2.micro"
}
🎯Deploying a Single Web Server:
⭐The goal is to deploy the simplest web architecture possible: a single web server that can respond to HTTP requests
resource "aws_instance" "example" {
ami = "ami-0fb653ca2d3203ac1"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.instance.id]
user_data = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF
user_data_replace_on_change = true
tags = {
Name = "terraform-example"
}
}
🎯 Deployment of Web-Server using Terraform:
⭐ Creation of terraform. tf with hashicorp/aws version
terraform {
required_version = ">= 1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.0"
}
random = {
source = "hashicorp/random"
version = "3.1.0"
}
}
}
⭐ Creation of EC2 instance along with security group including AWS providers (main.tf)
# Configure the AWS Provider
provider "aws" {
region = "us-east-1"
}
# Security Group
resource "aws_security_group" "web-server" {
vpc_id = aws_vpc.vpc.id
name = "web-server"
description = "Allow incoming HTTP Connections"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Creation of EC2 Instance
resource "aws_instance" "web-server" {
ami = "ami-02e136e904f3da870"
instance_type = "t2.micro"
key_name = "terraform-challenge"
security_groups = ["${aws_security_group.web-server.name}"]
user_data = <<-EOF
#!/bin/bash
sudo su
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd
echo "<html><h1> Welcome to #30DaysTerraformChallenge. Happy Learning... </h1></html>" >> /var/www/html/index.html
EOF
tags = {
Name = "web_instance"
}
}
⭐ Creation of output. tf
output "web_instance_ip" {
value = aws_instance.web-server.public_ip
}
⭐ Once the code has been added make sure the configuration and format are valid by the below commands
⭐ Initialize the terraform using below command

⭐ Resource actions are indicated using the below command

⭐ To deploy the resources use the below command

⭐ You can log into the console and verify the EC2 instances and Security Group have been created


🤩 Here goes an output

⭐ You can destroy the resource created using the following command
🕵🏻I also want to express that your feedback is always welcome. As I strive to provide accurate information and insights, I acknowledge that there’s always room for improvement. If you notice any mistakes or have suggestions for enhancement, I sincerely invite you to share them with me.
🤩 Thanks for being patient and following me. Keep supporting 🙏
Clap👏 if you liked the blog.
For more exercises — please follow me below ✅!
https://vjraghavanv.hashnode.dev/
#aws #terraform #cloudcomputing #IaC #DevOps #tools #operations #developers #awsugmdu #awsugncr #automatewithraghavan




