From ClickOps to DevOps: My First Infrastructure as Code Project with Terraform
Source: Dev.to
Introduction
The Architecture
- VPC: A custom Virtual Private Cloud.
- Subnet: A public subnet for the instance.
- Internet Gateway (IGW): To allow internet access.
- Route Table: Configuring routes to the IGW.
- Security Group: Allowing SSH, HTTP, and HTTPS.
- EC2 Instance: The server itself.
Why Terraform?
The Code
The Network Setup
resource "aws_internet_gateway" "terra-igw" {
# configuration omitted for brevity
}
resource "aws_subnet" "terra-subnet1" {
# configuration omitted for brevity
}
Security Groups
- Allow SSH from anywhere.
- Allow all outbound traffic.
The Instance
resource "aws_instance" "my_server" {
# other configuration omitted
tags = {
# tag definitions
}
}
The Workflow: 4 Magic Commands
terraform init– Initializes the directory and downloads the necessary AWS providers.terraform validate– Checks your code for syntax errors before you run it.terraform plan– Shows a “dry run” of what will be created, changed, or destroyed, giving you confidence before applying changes.terraform apply– Executes the API calls to AWS to build the resources.
Conclusion
Have you worked with Terraform? What was the first resource you automated? Let me know in the comments! 👇