Recap: What We Did in blog 1 (Variable Validation)
Here is if someone missed:
https://dev.to/rajpreet_gill_6272051bd31/linux-terraform-building-safe-infrastructure-with-variable-validation-d6b
In our first lab, we learned how to enforce strict input rules in Terraform with CLI - just like we validate configurations in Linux systems. Here's what we accomplished:
1. Basic Variable Declaration with syntax variable
"instance_count" {} # Accepts any input (risky!)
2. Added Type Safety with
variable "instance_count" {
type = number # Only numbers allowed
}
Lab 2: Deploying Multiple Instances with Confidence
Now in this blog, that we've mastered input validation, let's use those validated variables to deploy multiple EC2 instances safely.
Step 1: Bring Forward Our Validated Variables with Variables.tf
The variables.tf file is used to declare input variables for your Terraform configuration. These are values you can customize without hardcoding them in your main.tf.
It's all about making your code reusable, flexible, and clean.
Step 2: Deploy Using count (The First Approach) with main.tf
The main.tf file is typically the primary configuration file in a Terraform project. While Terraform doesn’t require files to be named main.tf, it’s a convention that helps keep things organized.
What Happens:
- Creates instances in order: alpha (index 0), beta (index 1), gamma (index 2)
- Each gets proper naming via our validated list
Step 3: creating file to define output values with output.tf
In Terraform, an output.tf file is used to define output values that Terraform displays after you run terraform apply. These are often values you want to see at the end of provisioning—like the public IP of an EC2 instance, a DNS name of a load balancer, or an ARN of a resource.
After Using terraform init, terraform plan -out tfplan, terraform apply tfplan the output shown below:
Hence, we could see in AWS Console, With name "alpha"
and "gamma" Instances are running.
You should see:
✅ alpha (existing instance preserved)
✅ gamma (existing instance preserved)
✅ beta (existing instance preserved)
After Edit your variables file (terraform.tfvars or CLI):
using this command ->
The beta Instance will destroy as Shown below...
After applying, terraform apply command beta ll gone forever
Example Workflow Comparison:
Final Thought:
"Managing cloud resources with Terraform is just like administering Linux servers – validate inputs, modify configurations deliberately, and always verify changes with the equivalent of ps or ls."
This version:
✅ Uses consistent Linux command comparisons
✅ Provides actionable verification steps
✅ Includes shareable snippet for social media
✅ Ends with engagement hook
#30DaysLinuxChallenge #CloudWhisler
DevOps #Linux #RHCSA #Opensource #AWS #CloudComputing
Catch out by My LinkedIn profile
https://www.linkedin.com/in/rajpreet-gill-4569b4161/