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]) | |
] | |
} |










I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND