← Back to list

Deploying AWS Infrastructure with Terraform: A Step-by-Step Guide

Terraform is a powerful infrastructure-as-code tool that allows you to automate the deployment and management of your cloud resources…

Muhammad Ibrahim · 2024-10-11 18:26 · 0 claps · 2.4 min read
#terrraform #cloud-computing #aws
Open on Medium ↗
Wiki topics: BIZ · Business Strategy ☁️ · DevOps & Cloud

Deploying AWS Infrastructure with Terraform: A Step-by-Step Guide

Terraform is a powerful infrastructure-as-code tool that allows you to automate the deployment and management of your cloud resources. Whether you’re provisioning EC2 instances, S3 buckets, or entire VPCs, Terraform makes cloud infrastructure easier to manage, version, and scale.

In this tutorial, I will guide you through deploying AWS infrastructure (an EC2 instance) using Terraform. By the end, you’ll know how to write Terraform configuration files, apply them to create resources in AWS, and manage your infrastructure with code.

Step 1: Install Terraform

Before you start, you need to have Terraform installed on your local machine. You can download the latest version from the official Terraform website.

For macOS (using Homebrew)

brew install terraform

For windows (using choclatey)

choco install terraform

Step 2: Configure AWS Credentials

Terraform requires AWS credentials to interact with your AWS account. You can configure credentials in several ways, but the simplest method is by using the AWS CLI.

  1. Install the AWS CLI (if not already installed):
sudo apt-get install awscli # for Linux
brew install awscli # for macOS
  1. Configure the AWS CLI:
aws configure

You’ll be prompted to enter your AWS Access Key ID, Secret Access Key, Region, and Output Format. These credentials will allow Terraform to authenticate with AWS.

Step 3: Write Your First Terraform Configuration

Let’s create a simple Terraform configuration to launch an EC2 instance in AWS.

Create a Directory for Your Terraform Files:

mkdir my-terraform-project
cd my-terraform-project

Create a main.tf File:

In the main.tf file, define the provider and the resource you want to create (in this case, an EC2 instance):

# main.tf

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

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

  tags = {
    Name = "TerraformExampleInstance"
  }
}

Code explanation:

  • The provider block specifies that we’re using AWS as our cloud provider.
  • The aws_instance block defines the resource (an EC2 instance) with attributes such as ami and instance_type. We also add a tag to name the instance.

Step 4: Initialize Terraform

Before applying any configuration, you need to initialize Terraform. This downloads the necessary provider plugins (in this case, AWS).

terraform init #this command will intialize the project directory

Step 5: Plan and Apply the Configuration

After initialization, you can review your configuration by running a plan. This command doesn’t create any resources but shows what Terraform will do.

terraform plan

if the plan is good then apply the configuration.

terraform apply

Terraform will ask for confirmation. Type yes, and Terraform will create the EC2 instance as defined in the main.tf file.

Step 6: Verify the Deployed Infrastructure

Once the Terraform apply is complete, you can log into your AWS Management Console and navigate to the EC2 Dashboard. You should see a running EC2 instance named TerraformExampleInstance.

You can also verify using the AWS CLI:

aws ec2 describe-instances --filters "Name=tag:Name,Values=TerraformExampleInstance"

This command will show details of your EC2 instance, including its public IP, instance ID, and status.

Step 7: Destroying the Infrastructure

Terraform not only helps create infrastructure but also makes it easy to tear it down when you’re done. Use the terraform destroy command to remove the resources you created:

terraform destroy

Terraform will prompt you to confirm the destruction of the resources. After typing yes, Terraform will terminate the EC2 instance and delete any associated resources.


메타데이터
post_id
d1a4d71bdec7
slug
deploying-aws-infrastructure-with-terraform-a-step-by-step-guide-d1a4d71bdec7
url
https://medium.com/@mibrahimzia48/deploying-aws-infrastructure-with-terraform-a-step-by-step-guide-d1a4d71bdec7
canonical_url
https://medium.com/@mibrahimzia48/deploying-aws-infrastructure-with-terraform-a-step-by-step-guide-d1a4d71bdec7
author_url
https://medium.com/@mibrahimzia48
status
ok
fetched_at
2026-06-23 03:48:11