devopshobbies / terraform-tutorial

you can follow up this repo to learn terraform with practical examples. also we recommend to check our youtube channel for this topic.
GNU General Public License v3.0
64 stars 25 forks source link

[Question]: create multiple resource in Variables #25

Open Faarhad opened 1 year ago

Faarhad commented 1 year ago

Describe and write the question.

How to create multiple resources in scenario described in branch 007 (it was about creating resources using tfvars. I mean more than 1 insurance in tfvars Regards,

The logs if needed

No response

mohammadll commented 1 year ago

Hi , we can create multiple resources using Meta-arguments . I wrote two of them below

for example:

we want to create two instances with different machine_names using single "aws_instance" block

variables.tf:

variable "instance_names" { type = map(string) default = { "instance1" = "babak" "instance2" = "babak2" } }

main.tf:

resource "aws_instance" "aws_instances" { for_each = var.instance_names ami = var.ami_id instance_type = var.instance_type key_name = var.key_name

tags = { Name = each.value Created_By = "terraform" } }