Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create multiple resources in a for loop with terraform?

Tags:

terraform

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.

like image 203
Dan Avatar asked Apr 01 '20 19:04

Dan


People also ask

How do you make a loop in Terraform?

Using the count meta-argument The count meta-argument is the simplest of the looping constructs within Terraform. By either directly assigning a whole number or using the length function on a list or map variable, Terraform creates this number of resources based on the resource block it is assigned to.

Can we use for loop in Terraform?

In a general-purpose programming language, you'd probably use a for-loop: # This is just pseudo code. It won't actually work in Terraform. One problem with this code is that all three IAM users would have the same name, which would cause an error, since usernames must be unique.

How for each loop works in Terraform?

The for_each argument will iterate over a data structure to configure resources or modules with each item in turn. It works best when the duplicate resources need to be configured differently but share the same lifecycle. Tip: Terraform 0.13+ supports the for_each argument on both resource and module blocks.

How do you use nested loops in Terraform?

Terraform does however support nested loops when creating local data structures, and it has a flatten function which can flatten the resulting list-of-lists. We can combine these two features to create a flat list of objects suitable for use with for_each .


Video Answer


1 Answers

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.

  • for_each: Multiple Resource Instances Defined By a Map, or Set of Strings

    By default, a resource block configures one real infrastructure object. However, sometimes you want to manage several similar objects, such as a fixed pool of compute instances. Terraform has two ways to do this: count and for_each.

like image 180
mon Avatar answered Oct 15 '22 17:10

mon