How to Use Terraform with AWS: A Complete Beginner’s Guide

Terraform is an Infrastructure as Code (IaC) tool that lets you define and provision AWS resources using declarative configuration files. Instead of clicking around the console, you write code in HCL (HashiCorp Configuration Language) to manage your cloud infrastructure reliably and repeatably.

Before you start, ensure you have an AWS account, Terraform installed on your machine, and the AWS CLI configured with an IAM user’s access keys. You’ll also need a basic code editor. Now let’s walk through the core steps.

Article illustration

Step 1: Configure the AWS Provider

Create a new directory for your project and add a file named main.tf. Inside it, define the AWS provider with your preferred region. For example:

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

This tells Terraform which cloud provider to use and where to deploy resources.

Step 2: Define Your First Resource

Now add a resource block to create, say, an EC2 instance. Use the aws_instance resource type and specify an AMI ID and instance type:

resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" }

You can also add tags, security groups, and other attributes as needed.

Step 3: Initialize and Apply the Configuration

Open your terminal in the project directory. Run terraform init to download the AWS provider plugin. Then run terraform plan to preview the changes. If everything looks good, execute terraform apply and confirm with “yes”. Terraform will create the EC2 instance in your AWS account.

Step 4: Manage State and Clean Up

Terraform automatically creates a terraform.tfstate file that tracks your deployed resources. Never edit this file manually. To destroy everything you created, run terraform destroy. This ensures you don��t leave unused resources running and incurring costs.

With these basics, you can expand your configuration to include VPCs, security groups, S3 buckets, and more. Terraform makes AWS infrastructure version-controlled, auditable, and reusable.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *