Reputation: 879
I'm following a terraform tutorial on using variables in AWS.
It defines AMI variables like:
variable "amis" {
type = "map"
default = {
"us-east-1" = "ami-b374d5a5"
"us-west-2" = "ami-fc0b939c"
}
}
And then assigns the AMI variable like this:
resource "aws_instance" "example" {
ami = var.amis[var.region]
instance_type = "t2.micro"
}
If I try the example that way I get an error:
Error: Invalid index
on main.tf line 17, in resource "aws_instance" "example":
17: ami = var.amis[var.region]
The given key does not identify an element in this collection value: string
required.
However I can set the variable by hard coding it:
var.amis["us-west-2"] # <-- this works
What can I do to set the variable correctly with ami = var.amis[var.region]
?
Upvotes: 0
Views: 5388
Reputation: 731
you need to define variable var.region
or you can pass the value to variable during plan / apply as terraform plan -var 'region=us-west-2'
and terraform apply -var 'region=us-west-2'
Upvotes: 2