🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Terraform looping using lists-and-maps-with-for with example

terraform {
required_version = ">= 0.12.0"
}
# List of letters
variable "letters" {
description = "a list of letters"
default = ["a", "b", "c"]
}
# Convert letters to upper-case as list
output "upper-case-list" {
value = [for l in var.letters: upper(l)]
}
# Convert letters to upper-case as map
output "upper-case-map" {
value = {for l in var.letters: l => upper(l)}
}
variable "vm_name_pfx" {
description = "VM Names"
default = "test-vm-"
type = string
}
variable "vm_count" {
description = "Number of Virtual Machines"
default = 3
type = string
}
resource "azurerm_windows_virtual_machine" "vm" {
count = var.vm_count # Count Value read from variable
name = "${var.vm_name_pfx}-${count.index}" # Name constructed using count and pfx
resource_group_name = azurerm_resource_group.rg.name
location = var.location
size = "Standard_F2"
admin_username = var.vm_admin_login
admin_password = var.vm_admin_password
tags = var.tags
network_interface_ids = [
azurerm_network_interface.nic[count.index].id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "MicrosoftWindowsServer"
offer = "WindowsServer"
sku = "2016-Datacenter"
version = "latest"
}
}
terraform {
required_version = ">= 0.12.0"
}
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "172.16.0.0/16"
tags = {
Name = "tf-0.12-for-example"
}
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "172.16.10.0/24"
tags = {
Name = "tf-0.12-for-example"
}
}
data "aws_ami" "ubuntu_14_04" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm/ubuntu-trusty-14.04-amd64-server-*"]
}
owners = ["099720109477"]
}
resource "aws_instance" "ubuntu" {
count = 3
ami = data.aws_ami.ubuntu_14_04.image_id
instance_type = "t2.micro"
associate_public_ip_address = ( count.index == 1 ? true : false)
subnet_id = aws_subnet.my_subnet.id
tags = {
Name = format("terraform-0.12-for-demo-%d", count.index)
}
}
# This uses the old splat expression
output "private_addresses_old" {
value = aws_instance.ubuntu.*.private_dns
}
# This uses the new full splat operator (*)
output "private_addresses_full_splat" {
value = [ aws_instance.ubuntu[*].private_dns ]
}
# This uses the new for expression
output "private_addresses_new" {
value = [
for instance in aws_instance.ubuntu:
instance.private_dns
]
}
# This uses the new conditional expression
# that can work with lists
# This uses the list interpolation function
output "ips_with_list_interpolation" {
value = [
for instance in aws_instance.ubuntu:
(instance.public_ip != "" ? list(instance.private_ip, instance.public_ip) : list(instance.private_ip))
]
}
# It also works with lists in [x, y, z] form
output "ips_with_list_in_brackets" {
value = [
for instance in aws_instance.ubuntu:
(instance.public_ip != "" ? [instance.private_ip, instance.public_ip] : [instance.private_ip])
]
}

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.