Terraform Dynamic Expressions¶
Step-01: Introduction¶
- Learn Dynamic Expressions in Terraform
- Conditional Expression: A conditional expression uses the value of a bool expression to select one of two values.
- Splat Expression: A
splat expressionprovides a more concise way to express a common operation that could otherwise be performed with aforexpression. - The special [*] symbol iterates over all of the elements of the list given to its left and accesses from each one the attribute name given on its right.
- A splat expression can also be used to access attributes and indexes from lists of complex types by extending the sequence of operations to the right of the symbol:
Step-02: Review Terraform Manifests¶
c1-versions.tf¶
- Added new random provider in
required_providersblock
c2-vairables.tf¶
- Added new variables
- availability_zones
- name
- team
- high_availability
c3-security-groups.tf¶
- Added common tags
c4-ec2-instance.tf¶
- Added Random ID resource block
- Added new locals block
- Important Note: Inside locals block we can add conditional expressions as below.
- Added Availability zone argument with count.index
- We will discuss about following conditional expressions here
c5-outputs.tf¶
- Added Splat expression [*] for all outputs
- Added Common Tags and ELB DNS Name as new outputs
c6-ami-datasource.tf¶
- No changes
c7-elb.tf¶
- Added this new resource
- We will be creating ELB only if "high_availability" variable value is true else it will not be created
Step-03: Execute Terraform Commands¶
# Terraform Initialize
terraform init
# Terraform Validate
terraform validate
# Terraform Plan: When Variable values, high_availability = false and name = "ec2-user"
terraform plan
Observation:
1) Plan will generate for only 1 EC2 instance and 2 Security Groups.
2) ELB Resource will not be created with these variable options
# Terraform Plan: When Variable values, high_availability = true and name = ""
terraform plan
1) Plan will generate for only 2 EC2 instance, 2 Security Groups and 1 ELB
2) ELB Resource will be created with these variable options
3) name value will be a random value known after terraform apply completed
# Terraform Apply
terraform apply -auto-approve
# Verify
1) Verify Outputs
2) Verify EC2 Instances & Security Groups & Common Tags
3) Verify ELB & Common Tags for ELB
4) Access App using ELB DNS Name
Step-04: Clean-Up¶
# Terraform Destroy
terraform destroy -auto-approve
# Clean-Up
rm -rf .terraform*
rm -rf terraform.tfstate*
# Uncomment and Comment right values in c2-variables.tf (Roll back to put ready for student demo)
high_availability = false
name = "ec2-user"
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
MARCH2026
Enroll Now on Udemy →
🎉 Offer