diff --git a/04-Terraform-Variables-and-Datasources/terraform-manifests/c5-ec2instance.tf b/04-Terraform-Variables-and-Datasources/terraform-manifests/c5-ec2instance.tf index 8c74dfab..d815d98a 100644 --- a/04-Terraform-Variables-and-Datasources/terraform-manifests/c5-ec2instance.tf +++ b/04-Terraform-Variables-and-Datasources/terraform-manifests/c5-ec2instance.tf @@ -9,3 +9,6 @@ resource "aws_instance" "myec2vm" { "Name" = "EC2 Demo 2" } } + + + diff --git a/Vikram-Notes.md b/Vikram-Notes.md new file mode 100644 index 00000000..997c8eee --- /dev/null +++ b/Vikram-Notes.md @@ -0,0 +1,413 @@ +=========================SUPERNOTES============================================== +>understand block, argument and attribute + +>.\terraform destroy -target="aws_instance.my-ec2-instance" + +>for list variable use variable "aws_instance_type_list" and same for map with string + +>if used count the update output to list output for each instance like value = [for instance in aws_instance.my-ec2-instance : instance.public_ip] + +>understand difference between user_data (bootstrp) and datasource (data) +user_data = file("${path.module}/app1-install.sh") +# Create a datasource to fetch the Latest Linux AMI ID for the specified region and name +data "aws_ami" "latest_linux_ami" { # data.aws_ami.latest_linux_ami.id + +>`for_each` Meta-Argument # for_each only works with map or set +- `toset` function +- `tomap` function + +>var.[] +ar.instance_type_list[1] # => "t3.small" + +>var.instance_type_list[1] # => "t3.small" +var.[""] + +>output "" { + value = [for in : .] +output "for_output_list" { + value = [for instance in aws_instance.myec2vm : instance.public_dns] + +>output "" { + value = {for in : => } +output "for_output_map1" { + value = {for instance in aws_instance.myec2vm : instance.id => instance.public_dns} + +>resource "aws_instance" "myec2vm" { + ami = data.aws_ami.amzlinux2.id + instance_type = var.instance_type + key_name = var.instance_keypair + + for_each = toset(data.aws_availability_zones.available.names) + availability_zone = each.key + + tags = { + Name = "For-Each-Demo-${each.key}" + } +} + +>for_each only works with map or set + + + + + + +===================================================================================== + +count Meta-Argument β€” Easy Formula +βœ… Easy-to-Remember Formula: + +resource "" "" { + count = + # other resource config + tags = { + Name = "${count.index}" + } +} + +πŸ” Plug in: + + = aws_instance + + = myec2vm + + = 2 + + = "Count-Demo-" + +βœ… Final Code: + +resource "aws_instance" "myec2vm" { + count = 2 + instance_type = "t3.micro" + tags = { + Name = "Count-Demo-${count.index}" + } +} + +🧠 Remember: + + count = how many copies + + count.index = 0, 1, 2... use for naming + +πŸ“ƒ List & Map Variable Access β€” Easy Formula +βœ… List Access Formula: + +var.[] + +Example: + +var.instance_type_list[1] # => "t3.small" + +βœ… Map Access Formula: + +var.[""] + +Example: + +var.instance_type_map["prod"] # => "t3.large" + +🧠 Remember: + + Lists = ordered β†’ use numbers + + Maps = named β†’ use keys + +πŸ” For Loop with List β€” Easy Formula +βœ… Easy-to-Remember Formula: + +output "" { + value = [for in : .] +} + +πŸ” Plug in: + + = for_output_list + + = instance + + = aws_instance.myec2vm + + = public_dns + +βœ… Final Code: + +output "for_output_list" { + value = [for instance in aws_instance.myec2vm : instance.public_dns] +} + +πŸ” For Loop with Map β€” Easy Formula +βœ… Easy-to-Remember Formula: + +output "" { + value = {for in : => } +} + +πŸ” Plug in: + + = for_output_map1 + + = instance + + = aws_instance.myec2vm + + = instance.id + + = instance.public_dns + +βœ… Final Code: + +output "for_output_map1" { + value = {for instance in aws_instance.myec2vm : instance.id => instance.public_dns} +} + +πŸ” For Loop with Map – Advanced +βœ… Formula: + +output "" { + value = {for , in : => .} +} + +Plug in: + + = for_output_map2 + + = c + + = instance + + = public_dns + +βœ… Final Code: + +output "for_output_map2" { + value = {for c, instance in aws_instance.myec2vm : c => instance.public_dns} +} + +πŸ’₯ Splat Operator β€” Easy Formula +βœ… Legacy: + +.*. + +βœ… Modern (Preferred): + +[*]. + +Example: + +aws_instance.myec2vm[*].public_dns + +🧠 Remember: + + [*].attr = grab all values of attr from multiple resources + +🌐 For-Each EC2 per AZ with toset β€” Easy Formula +βœ… for_each Formula: + +resource "" "" { + for_each = toset() + availability_zone = each.key +} + +Plug in: + + = aws_instance + + = myec2vm + + = data.aws_availability_zones.my_azones.names + +βœ… Final Code: + +resource "aws_instance" "myec2vm" { + for_each = toset(data.aws_availability_zones.my_azones.names) + availability_zone = each.key +} + +πŸ“€ Output with toset() β€” Easy Formula +βœ… Formula: + +output "" { + value = toset([for in : .]) +} + +Plug in: + + = instance_publicip + + = myec2vm + + = aws_instance.myec2vm + + = public_ip + +βœ… Final Code: + +output "instance_publicip" { + value = toset([for myec2vm in aws_instance.myec2vm : myec2vm.public_ip]) +} + +πŸ—ΊοΈ Output with tomap() β€” Easy Formula +βœ… Formula: + +output "" { + value = tomap({ + for , in : => . + }) +} + +Plug in: + + = instance_publicdns2 + + = s + + = myec2vm + + = public_dns + +βœ… Final Code: + +output "instance_publicdns2" { + value = tomap({ + for s, myec2vm in aws_instance.myec2vm : s => myec2vm.public_dns + }) +} + +🧠 TL;DR Summary Table +Formula Type Template +count Resource count = + "${count.index}" for unique names +List Access var.[] +Map Access var.[""] +For Loop (List) [for in : .] +For Loop (Map) {for in : => } +Advanced Map Loop {for , in : => .} +Splat (Modern) [*]. +for_each with set for_each = toset() + each.key +Output with toset() toset([for in : .]) +Output with tomap() tomap({for , in : => .}) + +for_each in Terraform β€” Easy-to-Remember Formula πŸ’‘ +βœ… Basic for_each Formula (with set or map) + +resource "" "" { + for_each = # toset(...) or map + + = each.key + # OR + = each.value + + tags = { + Name = "-${each.key}" # great for naming + } +} + +βœ… Example 1: for_each with Set (using toset()) +πŸ“¦ Goal: Create one EC2 instance per Availability Zone +Step-by-Step Plug-In: + + = aws_instance + + = myec2vm + + = toset(data.aws_availability_zones.available.names) + + = availability_zone + + = For-Each-Demo + +βœ… Final Code: + +resource "aws_instance" "myec2vm" { + ami = data.aws_ami.amzlinux2.id + instance_type = var.instance_type + key_name = var.instance_keypair + + for_each = toset(data.aws_availability_zones.available.names) + availability_zone = each.key + + tags = { + Name = "For-Each-Demo-${each.key}" + } +} + +βœ… Example 2: for_each with Map +πŸ“¦ Goal: Launch different EC2 instances per environment + +variable "env_map" { + default = { + dev = "t3.micro" + prod = "t3.large" + } +} + +Step-by-Step Plug-In: + + = aws_instance + + = env_instance + + = var.env_map + + = instance_type = each.value + + = Env-VM + +βœ… Final Code: + +resource "aws_instance" "env_instance" { + ami = data.aws_ami.amzlinux2.id + instance_type = each.value + key_name = var.instance_keypair + + for_each = var.env_map + + tags = { + Name = "Env-VM-${each.key}" + } +} + +βœ… Output with for_each β€” Use in Combination +🧾 Formula: + +output "" { + value = toset([ + for in : . + ]) +} + +βœ… Map Output with tomap(): + +output "" { + value = tomap({ + for , in : => . + }) +} + +🧠 Quick Reference Table +Part Formula / Meaning +for_each = toset() Use when you have a list +for_each = Use when you want key-value pairing +each.key Current key (like AZ name or env name) +each.value Value from the map (like instance type) +Use in tags "Name" = "prefix-${each.key}" +toset([...]) Convert list of items to a set for output +tomap({...}) Convert looped values to a map for output +🎯 Mnemonic to Remember + + "For Each Thing, Do a Unique Thing!" + Use each.key for naming, each.value for config. + +βœ… TL;DR Template + +resource "" "" { + for_each = toset() # or just a map + + = each.key # or each.value + + tags = { + Name = "-${each.key}" + } +} +