Learn how Terraform automates AWS infrastructure with a simple EC2 project, key concepts, and workflow explained by our AWS Support team.

How to Deploy AWS Infrastructure Using Terraform: A Beginner’s Guide

Terraform helps teams manage cloud infrastructure using configuration files instead of manual setup. This guide covers key Terraform concepts, benefits, and a simple AWS project that creates a security group and launches an EC2 instance, along with the basic command workflow and best practices.

Explore the article to learn more about Terraform and infrastructure automation.

What is Terraform

How to Deploy AWS Infrastructure Using Terraform: A Beginner's Guide

Terraform is an open-source tool created by HashiCorp that helps teams build and manage infrastructure using configuration files. Instead of creating servers, networks, or databases manually in cloud dashboards, you define the required resources in code.


When the configuration runs, Terraform automatically creates or updates the infrastructure. It works with major cloud platforms such as Amazon Web Services, Microsoft Azure, and Google Cloud Platform, which makes infrastructure management easier and more consistent.

Explore Terraform for AWS

Chat animation


Key Benefits of Terraform

  • Terraform helps teams deploy and manage infrastructure more efficiently.
  • Infrastructure can be defined in code and stored in version control
  • Resources can be deployed quickly without manual setup
  • Development, testing, and production environments stay consistent
  • Infrastructure can be managed across multiple cloud platforms
  • Automation reduces configuration mistakes and saves time

Key Terraform Concepts

When you start using Terraform, a few basic concepts explain how it connects to cloud platforms and manages infrastructure.

Provider

A provider tells Terraform which platform it should interact with. It connects Terraform to services such as Amazon Web Services, Microsoft Azure, or Google Cloud Platform.

Example configuration

provider "aws" {
region = "us-east-1"
}

This configuration tells Terraform to use the AWS provider in the specified region.

Resource

A resource represents the infrastructure component you want to create. This could be a virtual machine, storage bucket, or network.

Example resource

resource "aws_s3_bucket" "example" {
bucket = "my-terraform-demo-bucket"
}

This configuration creates an S3 storage bucket in AWS.

State

Terraform keeps track of infrastructure using a state file called terraform.tfstate. This file records what resources already exist and how they are configured.

Because Terraform stores this information, it can

  • Detect infrastructure changes
  • Update existing resources
  • Avoid creating duplicate resources
Execution Plan

Before making changes, Terraform shows what actions it will perform. This step helps teams review infrastructure updates before they happen.

Example command

terraform plan

After reviewing the plan, you can apply the changes using
terraform apply

This process helps teams manage infrastructure in a controlled and predictable way.

Simple Terraform Project on AWS

This small project shows how you can use Terraform to create and manage cloud infrastructure on Amazon Web Services. You will configure AWS, create a security group, launch an EC2 instance, view its public IP, and remove the resources when finished.

What You Will Build

In this example, you will

  • Connect Terraform with AWS
  • Create a security group for SSH and HTTP
  • Launch a small EC2 instance
  • Display the public IP address
  • Remove the infrastructure safely
Prerequisites

Before starting, make sure the following are ready.

AWS account

You need an active AWS account.

AWS CLI configured

Run
aws configure
Enter your access key, secret key, region, and output format. This allows Terraform to access your AWS account.

Terraform installed

Check the installation
terraform version

Step 1: Create a Project Folder

Create a directory and move into it.

mkdir terraform-aws-project
cd terraform-aws-project
Step 2: Create the Terraform File

Create a configuration file.

touch main.tf

Terraform reads infrastructure instructions from files with the tf extension.

Step 3: Configure the AWS Provider

Add the following code inside main.tf.

provider "aws" {
region = "us-east-1"
}

This tells Terraform to use AWS and deploy resources in the selected region.

Step 4: Create a Security Group

Add this configuration.

resource "aws_security_group" "web_sg" {
name = "web-security-group"
ingress {
from_port   = 22
to_port     = 22
protocol    = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port   = 80
to_port     = 80
protocol    = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

This rule allows

  • SSH access on port 22
    • web traffic on port 80

Step 5: Launch an EC2 Instance

Add the EC2 configuration below the security group.

resource "aws_instance" "web_server" {
ami           = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
security_groups = [
aws_security_group.web_sg.name
]
tags = {
Name = "terraform-demo-server"
}
}

This creates a small EC2 server and attaches the security group.

Step 6: Initialize Terraform

Run

terraform init

Terraform downloads the AWS provider and prepares the project.

Step 7: Review the Plan

Before creating resources, run

terraform plan

This command shows what Terraform will create.

Step 8: Create the Infrastructure

Deploy the resources.

terraform apply

Type yes when Terraform asks for confirmation. The security group and EC2 instance will be created.

Step 9: Show the Public IP

Create a file called outputs.tf

output "instance_ip" {
value = aws_instance.web_server.public_ip
}

Run
terraform apply

View the IP address
terraform output
Step 10: Remove the Infrastructure

When the project is no longer needed, delete the resources.

terraform destroy

Terraform will remove the EC2 instance and related resources.

Complete Terraform Command Flow

The typical workflow for Terraform projects follows these commands.

Command Purpose
terraform init Prepares the project directory
terraform validate Checks the configuration for errors
terraform plan Shows the infrastructure changes
terraform apply Creates or updates resources
terraform output Displays useful deployment details
terraform destroy Removes the infrastructure

Best Practices for Beginners

When working with Terraform, follow these simple practices.

  • Store Terraform files in Git for version control
  • Avoid uploading the state file to public repositories
  • Use variables instead of hardcoding values
  • Remove unused resources to avoid unnecessary cost
  • Review the plan before applying changes

[Need assistance with a different issue? Our team is available 24/7.]

Conclusion

Terraform allows teams to create and manage infrastructure using simple configuration files, making deployments faster and easier to control. It also helps maintain consistency across different environments.