Conditional Provisioning with Ternary Operators
Dynamically evaluate infrastructure configurations using Terraform conditional expressions (condition ? true_val : false_val). Automatically toggle EC2 instance sizing based on deployment environment variables like prod vs dev.
Core Implementation Highlights
The Complete Terraform Code
Save the code below into main.tf. It declares the environment input variable, registers an SSH Key Pair, sets up a Security Group firewall, provisions an EC2 instance with conditional instance sizing, and exports the public IP address.
# Define environment selection variable (Defaults to "prod")
variable "selectenvironment" {
type = string
default = "prod"
}
# Register SSH Key Pair using local public key
resource "aws_key_pair" "shankey" {
key_name = "serverkey"
public_key = file("/home/ak/.ssh/id_rsa.pub")
}
# Security Group Firewall allowing SSH (Port 22) Ingress
resource "aws_security_group" "webserver_ig" {
name = "webserver_ig"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Provision EC2 Instance with Conditional Instance Sizing
resource "aws_instance" "create_instance" {
ami = "ami-0272eca93537d5c9d"
instance_type = var.selectenvironment == "prod" ? "t2.micro" : "t4g.small"
associate_public_ip_address = true
vpc_security_group_ids = [aws_security_group.webserver_ig.id]
key_name = aws_key_pair.shankey.key_name
}
# Output the Public IP address of the created instance
output "aws_ec2_name" {
value = aws_instance.create_instance.public_ip
}Resource-by-Resource Explanation
variable "selectenvironment"
Defines a configurable input variable that controls the target environment. Setting a default value of "prod" allows the configuration to execute safely without requiring CLI input, but can be overridden at runtime using -var="selectenvironment=dev".
variable "selectenvironment" {
type = string
default = "prod"
}Ternary Operator (condition ? true : false)
The core of conditional logic in HCL uses standard ternary syntax: CONDITION ? TRUE_VAL : FALSE_VAL.
- When
var.selectenvironment == "prod": Evaluates totrue, assigning"t2.micro". - When
var.selectenvironment != "prod": Evaluates tofalse, assigning"t4g.small".
resource "aws_instance" "create_instance" {
ami = "ami-0272eca93537d5c9d"
instance_type = var.selectenvironment == "prod" ? "t2.micro" : "t4g.small"
key_name = aws_key_pair.shankey.key_name
vpc_security_group_ids = [aws_security_group.webserver_ig.id]
}aws_security_group & aws_key_pair
Binds custom network firewall rules and SSH authentication to the server:
aws_key_pair.shankey: Reads local public key file viafile("/home/ak/.ssh/id_rsa.pub").aws_security_group.webserver_ig: Opens port 22 for inbound SSH connections and enables unrestricted egress.
resource "aws_key_pair" "shankey" {
key_name = "serverkey"
public_key = file("/home/ak/.ssh/id_rsa.pub")
}
output "aws_ec2_name" {
value = aws_instance.create_instance.public_ip
}Deployment & Execution Commands
Test both production and non-production conditional branches using Terraform variable flags:
# 1. Initialize AWS Provider
$ terraform init
# 2. Plan for Production (Defaults selectenvironment to "prod" -> t2.micro)
$ terraform plan
# 3. Plan for Staging/Dev (Overrides variable -> t4g.small or t2.small)
$ terraform plan -var="selectenvironment=dev"
# 4. Apply Production Configuration
$ terraform apply -var="selectenvironment=prod" -auto-approve
# 5. Clean up infrastructure when done
$ terraform destroy -auto-approve