How to create 3 different resources in one block?


I want to create multiple ec2 instances and i’m usually doing it by creating block for each instance. how can I make, for example 3 instances in one block.

resource "aws_instance" "INSTANCES" {
	ami                                    = var.ami
	instance_type                  = var.instance_type
	key_name                         = var.key_name
	subnet_id                          = var.subnet_id
	vpc_security_group_ids  = [var.vpc_security_group_ids]
	iam_instance_profile      = var.iam_instance_profile

	tags = {
		Name                         = "Instance_name"
	user_data = file(var.user_data)

I tried to use the count and for_each, but I only manage to control limited stuff.
I would want in one block to give every node:

  • different tag name
  • different user_data_file
  • different subnet_id

how can i do that?

Hi @HashiBeliver!

Could you please also share what you tried with count and/or for_each? The answer here is likely to include at least one of those but it’ll be easier to see what you’re trying to do if we can work from your previous attempts. If you saw an error when you tried those things it would also be helpful to share the full error messages.

I did recently manage to find a way with count and the element function, there were other problems I might share if they will impact a functionality I need
thanks for reaching out : )