0

I need to deploy 3 EC2 instances within 3 availability zones inside the same region using Terraform. I was trying to use the following structure:

variable "AVZ" {
  type = "map"
  default = {
  eu-central-1 = ["eu-central-1a","eu-central-1b","eu-central-1c"]
  eu-west-1    = ["eu-west-1a","eu-west-1b","eu-west-1c"]
 }
}

and

resource "aws_instance" "web-server0" {
   count = "${var.AWS_COUNT_WEB}"
   availability_zone = "${element(split(",", var.AVZ), count.index)}"
   ami = "${lookup(var.AWS_AMI,var.AWS_REGION)}"
   key_name = "${var.AWS_KEY_NAME}"
   instance_type = "${var.AWS_INSTANCE_TYPE}"
   associate_public_ip_address = "true"
   vpc_security_group_ids = [
     "${aws_security_group.allow_ssh.id}",
     "${aws_security_group.allow_web.id}"
   ]
 tags {
   Name = "web-server-b${count.index}"
   Tier = "blue"
   Role = "Web_Server"
      }

However it did not help much. It simply does not work I'm getting the following error:

At column 11, line 1: split: argument 2 should be type string, got type 
map in:

Does anybody knows how I can use variable type map, instead of string so I can map AZ to the Regions and lookup available AZs depending on Region defined and deploy EC2 instances depending on that results?

Thank you, Alex

1

You define AVZ as a map, but you do not use a key to access it. A split(",", ...) is normally used in old code with string values instead of lists.

So try this interpolation:

availability_zone = "${element(var.AVZ[var.AWS_REGION], count.index)}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.