I am trying to 2 workspaces and in each workspace ec 2 instances with different ami but failing with below errror, Please suggest what could be the wrong
Here is the code:
Variable region
AWS provider
provider “aws” {
access_key = “XXX”
secret_key = “YYY”
region = “us-west-2”
}
variable instance_type {
default = “t2.xlarge”
}
variable ami {
type = map
default = {
“ProjectA” = “ami-005e54dee72cc1d00”
“ProjectB” = “ami-0c2f25c1f66a1ff4d”
}
}
resource “aws_instance” “projectA”{
instance_type = var.instance_type
ami = lookup (var.ami , terraform.workspace)
tags = {
name = terraform.workspace
}
}
resource “aws_instance” “projectB”{
instance_type = var.instance_type
ami = lookup (var.ami , terraform.workspace)
tags = {
name = terraform. Workspace
}
}
Error:
Hi @sk8228402,
Your workspace seems to be called “projectA” (lowercase P) but your map key is named “ProjectA” (uppercase P).
These two names must match for the lookup to succeed.
1 Like
Hi Mart,
Thank you it worked, one more question, every time I create instance with tag name it supposed to show name in the below red marked pointed area right? but why it is not showing? instead when I click on option Tag then it is showing as projectA in the bottom. why it is so, thank you.
I’m not sure about that but when I’ve worked with AWS in the past I’ve noted that it’s conventional to name tags so that their names start with an uppercase letter, which would mean naming that tag “Name” instead of “name”.
I don’t know if these names are case-sensitive in AWS but that is the first thing I would try. Otherwise I think that will be a question for AWS support instead, since it’s an AWS behavior rather than a Terraform behavior.