27

I have looked at several bits of documentation as well as a udemy course on terraform and I do not understand how to do the thing that I want to do. I want to create a for loop and in it I want to create an S3 event notification, create an Sns topic that listens to that notification, create an Sqs queue and then subscribe the queue to the sns topic. It seems like for loops in terraform are not advanced enough to do this. Am I wrong, is there any documentation or examples that explain how to use for loops for this use case?

Thanks in advance.

3
  • The iterators are within the scope of the resource, and not outside of it. Given that, it is completely possible to functionally do what you describe. More assistance can be provided with an MCVE of your attempt at this. Commented Apr 1, 2020 at 20:43
  • Yes, that's kind of what i was seeing from the docs. I'd like to be able to create resources dynamically in a loop. I saw on another SO post that people have been creating their own wrappers with python to basically generate tf files which is a way to achieve this. Commented Apr 1, 2020 at 20:50
  • terraform.io/docs/configuration/…. Each resource type will be created independently with respective for_each. NOT in a big for loop if it is what you are expecting in which you can create a S3 event, then give it to SNS next to be created in a loop. Commented Apr 2, 2020 at 2:12

2 Answers 2

16

The for_each example looks like the following:


variable names {
  type = list(string)
  description = "List of names"
}

resource "aws_example_resource" "bar" {
  for_each = toset(var.names)
  name = each.key
}

This will create N resources (length of names), using the each.key to specify the value to assign to name each time

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! in my case it only allowed me to use it like this by having set(string) instead of list(string).
I had a case where I needed to use module with for loop instead of resource shown above. I have multiple services that need ses, the only thing different is the service name and the ses name. To do this in loop, below code is what worked for me. variable "service_names" { type = list(string) description = "List of services" default = [ "abc", "xyz","sdf","klm"] } module "custom_ses" { for_each = toset(var.service_names) source = "./modules/ses" name = "${each.key}_ses" }
14

An example to create AWS VPC subnets then give them to AWS EC2 instances.

resource "aws_subnet" "public" {
  count = length(var.public_subnet_cidr_blocks)
  vpc_id     = var.vpc_id
  cidr_block = var.public_subnet_cidr_blocks[count.index]
}

resource "aws_instance" "public_ec2" {
  count = length(var.public_subnet_ids)
  subnet_id = var.public_subnet_ids[count.index]
  ami           = var.ami_id
  instance_type = "t2.micro"
  tags = {
    Name = "PublicEC2${count.index}}"
  }
  provisioner "local-exec" {
    command = <<EOF
echo "Public EC2 ${count.index} ID is ${self.id}"
EOF
  }
}

There is no syntax like below to create resources.

[ for name in var.names:
 aws_s3_bucket {...} 
 aws_sns_topic {...}
]

For expression is basically for values, not for creating resources.

  • for Expressions

    A for expression creates a complex type value by transforming another complex type value.

To create multiple resources, as below in the document use for_each or count.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.