π Terraform Series β Day 7

Search for a command to run...

No comments yet. Be the first to comment.
π Terraform Series β Automate Your Infrastructure Starting a complete **Terraform series** where Iβll cover everything from **basic to advanced level** with real-world practicals. In this series, you will learn: β’ What is Infrastructure as Code (IaC) & why it matters β’ Terraform fundamentals (providers, resources, state) β’ Writing and managing Terraform configurations β’ Variables, outputs & modules β’ Remote state & state management β’ Provisioning infrastructure on AWS β’ Automation & real-world use cases β’ Advanced concepts like workspaces, modules, and best practices π― Goal: Help you automate infrastructure and become job-ready in DevOps. Perfect for **beginners, students, and DevOps learners** who want hands-on experience. Stay tuned and letβs build infrastructure the smart way β‘π» #Terraform #DevOps #Cloud #AWS #InfrastructureAsCode #Automation
Automating AWS EC2 Setup with Terraform and user_data Welcome back to our Terraform journey. In infrastructure as code, setting up a server is just the beginning. After your EC2 instance is running, y
Master GitLab CI/CD Variables, Secrets Management, Artifacts & Best Practices π Introduction In the previous article, we learned how GitLab Runners execute CI/CD pipelines and how to configure Self-H

Learn GitLab Runners, Hosted vs Self-Hosted Runners, Runner Registration, Tags, Pipeline Editor & Parallel Jobs π Introduction In the previous article, we created our first GitLab CI/CD pipeline and

Learn Continuous Integration, Continuous Delivery, Pipelines, Stages, Jobs & .gitlab-ci.yml π Introduction In the previous articles, we explored GitLab fundamentals, repository management, and collab

Learn Docker Scout, Multi-Stage Builds, Docker Hardened Images (DHI), SBOM, Docker Model Runner, Ask Gordon AI, and production-ready container security through practical, real-world examples. Introduc

Import Repositories, Mirroring & Repository Management Best Practices π Introduction In the previous articles, we learned the fundamentals of GitLab, created projects, and configured secure authentic

Variables & Outputs (Make Your Code Smart π₯)
In real-world DevOps, writing flexible and reusable code is very important.
Today, we will learn how to use Variables and Outputs in Terraform to make our infrastructure clean, dynamic, and production-ready.
Till now, we were writing Terraform codeβ¦
but there was one problem π
π Everything was hardcoded
And in real DevOps, hardcoding = BIG mistake β
So today we fix that π‘
Imagine this:
instance_type = "t2.micro"
Looks simpleβ¦ but π
β Want to upgrade instance? Change everywhere
β Want reuse? Not possible
β Working in team? Becomes messy
π Basically: Not scalable
Instead of fixing values in code, we use variables
Think like this:
π βKeep values separate, keep code cleanβ
Change once β Apply everywhere
Clean & readable code
Reusable infrastructure
Industry-level practice
π In short:
Variables = Flexibility + Clean Code + DevOps Standard
variables.tfvariable "ec2_instance_type" {
default = "t2.micro"
type = string
}
variable "ec2_root_storage_size" {
default = 10
type = number
}
variable "ec2_ami_id" {
default = "ami-0cb91c7de36eed2cb"
type = string
}
π§ Simple Understanding:
variable β variable name
default β default value
type β data type
π Values are now separated from the main code β
ec2.tfNow the real magic π₯
# Create Key Pair
resource "aws_key_pair" "my_key" {
key_name = "terra-key-aws"
public_key = file("terra-key-aws.pub")
}
# Default VPC
resource "aws_default_vpc" "default" {}
# Security Group
resource "aws_security_group" "my_groups" {
name = "my-group"
description = "Security group for EC2"
vpc_id = aws_default_vpc.default.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow SSH"
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow HTTP"
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
description = "Allow all outbound"
}
tags = {
Name = "automate-sg"
}
}
# EC2 Instance
resource "aws_instance" "my_instance" {
ami = var.ec2_ami_id
instance_type = var.ec2_instance_type
key_name = aws_key_pair.my_key.key_name
vpc_security_group_ids = [aws_security_group.my_groups.id]
root_block_device {
volume_size = var.ec2_root_storage_size
volume_type = "gp3"
}
tags = {
Name = "terra-ec2"
}
}
β‘ Important Line:
var.<variable_name>
π Example:
var.ec2_instance_type
Before:
instance_type = "t2.micro"
After:
default = "t3.micro"
β Change in one place
β Applied everywhere automatically
π₯ Thatβs the power
Deployment is doneβ¦ but now π
π How do you get EC2 Public IP?
β You have to manually check AWS Console
Terraform will show it directly π₯
outputs.tfoutput "ec2_public_ip" {
value = aws_instance.my_instance.public_ip
}
output "ec2_public_dns" {
value = aws_instance.my_instance.public_dns
}
When you run:
terraform apply
π At the end, Terraform shows:
β Public IP
β Public DNS
Directly in terminal π―
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar