Skip to content

This script is to practice how to use Terraform to define, provision, and manage AWS infrastructure using IaC.

Notifications You must be signed in to change notification settings

freda1874/Terraform-Script

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

Terraform with AWS Infrastructure Setup

This project demonstrates how to use Terraform to define, provision, and manage AWS infrastructure using Infrastructure as Code (IaC). It covers core Terraform functionalities such as initializing, applying, and managing state files, as well as deploying an Ubuntu EC2 instance with networking configurations.

Key Concepts Practiced

  • Terraform: Automates AWS infrastructure provisioning.
  • Infrastructure as Code (IaC): Manages cloud resources via code.
  • State Management: Tracks deployed resources.
  • EC2 Deployment: Launches an AWS instance with Terraform.
  • Networking: Configures VPC, subnets, route tables, and security groups.

image

⚙️ Setting Up Terraform

🔧 Prerequisites

  • Install Terraform: brew install terraform (Mac) or choco install terraform (Windows).
  • Ensure you have an AWS account and AWS CLI configured: aws configure
  • Set up an SSH key pair in AWS to access EC2 instances.

🚀 Essential Terraform Commands

Action Command Description
Initialize Terraform terraform init Initializes Terraform in the working directory.
Preview Changes terraform plan Shows the changes Terraform will apply before execution.
Apply Changes terraform apply Creates or updates AWS infrastructure as defined in the code.
Destroy Infrastructure terraform destroy Deletes all Terraform-managed resources.
List Tracked Resources terraform state list Displays all resources tracked in the Terraform state file.
Show Details of a Resource terraform state show aws_instance.web-server Shows the current state and attributes of a specific resource.
Rename/Move a Resource terraform state mv aws_instance.old-name aws_instance.new-name Renames or moves a resource within the Terraform state.
Remove a Resource Without Deleting It terraform state rm aws_s3_bucket.my_bucket Stops tracking a resource in Terraform without destroying it.

Terraform Output Variables

Retrieve EC2 Public IP

Add the following to main.tf to output the EC2 public IP:

output "instance_public_ip" {
  description = "Public IP of the EC2 instance"
  value       = aws_instance.web-server.public_ip
}

Run this command to display the value:

terraform output instance_public_ip

Deploying an AWS EC2 Instance

Terraform Configuration for an EC2 Instance

resource "aws_instance" "web-server-instance" {
  ami               = "ami-04b4f1a9cf54c11d0"
  instance_type     = "t2.micro"
  availability_zone = "us-east-1a"
  key_name          = "terraform-main-key"

network_interface { device_index = 0 network_interface_id = aws_network_interface.web-server-nic.id }

user_data = <<-EOF #!/bin/bash sudo apt update -y sudo apt install apache2 -y sudo systemctl start apache2 sudo bash -c 'echo "Your very first web server" > /var/www/html/index.html' EOF tags = { Name = "web-server" } }

Networking Setup: VPC, Subnets, Security Groups

image

VPC & Subnet Configuration

It is recommended to use the same Availability Zone (AZ) as the subnet.

  • VPC: Defines a private network in AWS.
  • Subnet: Divides a VPC into isolated networks.
  • Internet Gateway: Allows outbound internet traffic from the VPC.
  • Route Table: Controls traffic routing within the VPC.
  • Security Groups: Control inbound/outbound traffic to instances.

Accessing EC2 on Mac/Linux

ssh -i "your-key.pem" ubuntu@

Accessing EC2 on Windows (Using PuTTY)

  • 1️⃣ Download and install PuTTY.
  • 2️⃣ Open PuTTYgen → Load your .pem key → Save as .ppk.
  • 3️⃣ Open PuTTY → Enter Hostname: ubuntu@34.232.56.18.
  • 4️⃣ Under SSH → Auth → Select your .ppk key.
  • 5️⃣ Click **Open** to connect.

📜 Credit

Inspired by this tutorial: https://www.youtube.com/watch?v=SLB_c_ayRMo

About

This script is to practice how to use Terraform to define, provision, and manage AWS infrastructure using IaC.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages