🔹Table of Contents :
Introduction
Why Use Terraform?
Getting Started with Terraform
- Installation Process
Setting up an EC2 Instance with Terraform
- Step-by-Step Guide for Ubuntu
Real-World Use Cases for Terraform
Today, I delved into Infrastructure as Code (IaC) using Terraform! Terraform is a tool that allows us to manage, automate, and provision infrastructure in a safe, efficient, and scalable way. With Terraform, repetitive infrastructure tasks are simplified, and deployments become faster and more reliable.
✅Why Terraform?
Consistency: With code, your infrastructure setup remains consistent across different environments.
Version Control: Infrastructure configurations can be versioned, making it easy to track changes.
Efficiency: It reduces the risk of manual errors and saves time.
✅Getting Started with Terraform: Installation
To begin, I set up Terraform and used it to configure an EC2 instance on Ubuntu. Here’s a breakdown of the installation and setup process:
Install Terraform:
Visit the official Terraform website and download the installer for your operating system (supports Windows, Mac, and Linux).
Steps For Linux:-
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \ gpg --dearmor | \ sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
gpg --no-default-keyring \ --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \ --fingerprint
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \ https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \ sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform
Follow OS-specific Installation:
For Windows: Download the
.exe
file from the website, move it to a directory, and add the path to your system's PATH environment variable.For Mac: Use a package manager like Homebrew for installation. Run the command:
brew tap hashicorp/tap brew install hashicorp/tap/terraform
Verify the Installation:
Check that Terraform is installed by running:
terraform --version
✅Setting up an EC2 Instance with Terraform
After installing Terraform, I worked through creating an EC2 instance using it. Here’s how to set it up on Ubuntu:
Step-by-Step Guide:
Create a Directory for the Project:
- Use a directory to store your configuration files, keeping your infrastructure organized.
Write a Terraform Configuration File:
Create a
.tf
file, which defines your desired infrastructure. This file should contain resource blocks specifying the EC2 instance type, region, and AMI (Amazon Machine Image) ID.Example structure:
hclCopy codeprovider "aws" { region = "us-west-2" } resource "aws_instance" "example" { ami = "ami-XXXXXXX" instance_type = "t2.micro" }
Initialize the Directory:
- Run the
terraform init
command in the directory to initialize the project. This installs the necessary provider plugins (like AWS).
- Run the
Plan and Apply Configuration:
Plan: Before creating resources, run
terraform plan
to review changes that will be made.Apply: Once reviewed, run
terraform apply
to execute the configuration and launch your EC2 instance.
Access and Manage the Instance:
Use SSH to access your EC2 instance:
kotlinCopy codessh -i "your-key.pem" ubuntu@<instance_public_ip>
✅Real-World Use Cases for Terraform
Provisioning Cloud Resources: Set up virtual machines, databases, networks, and more across providers like AWS, Azure, or GCP.
Multi-Environment Consistency: Deploy identical environments for development, testing, and production without discrepancies.
Automated Disaster Recovery: Maintain scripts to recreate infrastructure in the event of failure or rollback needs.
Stay tuned for more hands-on tasks and in-depth learning!
🚀Thanks for joining me on Day 60! Let’s keep learning and growing together!
Happy Learning! 😊
#90DaysOfDevOps