Terraform
Terraform - Infrastructure as Code Build
Unleashing the Power of Terraform: A Simple Build That Saved Me Hours
Hello all! I want to share with you a powerful example of how Terraform, the Infrastructure as Code (IaC) tool, can save you an incredible amount of time and effort in managing your AWS resources. By writing a simple script, I was able to set up a VPC, a subnet, and an EC2 instance in AWS without manually navigating through the AWS Management Console.Step 1: Define Required Providers
First things first, I specified the required provider for my AWS resources using the following block:terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 2.0"
}
}
}
Step 2: Set Up the AWS Provider
Next, I configured the AWS provider, specifying the region where I wanted to build my resources:provider "aws" {
region = "us-east-1"
}
Step 3: Create a VPC
With the provider in place, I then defined a Virtual Private Cloud (VPC):resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
Step 4: Create a Subnet
Within the VPC, I set up a subnet:resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
}
Step 5: Deploy an EC2 Instance
Finally, I deployed an EC2 instance within the subnet:resource "aws_instance" "name" {
ami = "ami-0f34c5ae932e6f0e4"
instance_type = "t2.micro"
subnet_id = aws_subnet.my_subnet.id
tags = {
Name = "my_instance"
}
}Result: Time Saved and a Clean, Repeatable Process
By utilizing this simple Terraform script, I managed to save hours of manual configuration time. Here's how it benefitted me:- Speed: I had the entire infrastructure up and running in minutes, just by running
terraform apply
. - Repeatability: If I need to create a similar infrastructure elsewhere, I can reuse the code, ensuring consistency.
- Maintainability: Managing and modifying the infrastructure becomes a matter of changing a few lines of code rather than a long series of manual adjustments.