0
votes

I want to create 3 ec2 instances in 3 different regions. I am using iteration and count index feature of terraform. But I am not able to apply the same feature for region parameter in aws provider.

provider "aws" {
  region = "${element(var.region, count.index)}"
}

Where I am passing the values like below.

region = [ "us-east-1" , "eu-central-1" ]
instance_type = [ "t2.small" , "t2.micro" ]

I am getting following error after terraform init.

count variables are only valid within resources

1
As the error message says, you can't do that. You need to specify separate aliases for each anyway. - ydaetskcoR
forgot to mention, but I would like to have a generic solution. Adding aliases means hardcoding fix regions. - Sandip Divekar
Yep, and unfortunately that's a limitation of Terraform. It might be better if you could expand on what your requirements are and maybe there's a better solution. - ydaetskcoR
Hmm. As I have multi-region deployment so now generating separate terraform.tfvars file for each region. Thanks buddy @ydaetskcoR - Sandip Divekar

1 Answers

0
votes

The region to be entered in the provider block should be your default region. Since, there is only one default region per aws user, it can't accept a list of regions.

Try giving region as:

region = "${var.region[0]}"

It takes the first element of list of regions that you have specified as variables. Accordingly, you can change the index to point to some other element of the list.