47574

Terraform: How to append the server count and assign servers to multiple AZ's?

Question:

<strong>main.tf</strong>

resource "aws_instance" "service" { ami = "${lookup(var.aws_winamis, var.awsregion)}" count = "${var.count}" key_name = "${var.key_name}" instance_type = "t2.medium" subnet_id = "${aws_subnet.private.id}" # private_ip = "${lookup(var.server_instance_ips, count.index)}" vpc_security_group_ids = ["${aws_security_group.private-sg.id}"] associate_public_ip_address = false availability_zone = "${var.awsregion}a" tags { Name = "${format("server-%01d", count.index + 1)}" Environment = "${var.environment}" } }

<strong>Variable.tf</strong>

variable "awsregion" {default =""} variable "count" {default = ""} variable "server_instance_ips" {default = ""}

<strong>dev.tfvars</strong>

server_instance_ips = ["10.0.2.25", "10.0.2.26"] #doesn't work as a list but works with single IP address count = "4" awsregion = "us-east-1"

I want the servers to have tags - dla-server-1/2/3/4 after they're created but with my above code I can only do server-1/2/3/4 but not dla/sla/pla depending on the environments; IP addresses are randomly assigned to the servers as I'm not able to pass the list of IP's and lastly, how can I create servers in different AZ's i.e. 2 servers in AZ1a and 2 servers in AZ1b?

Answer1:

The first answer is easy: add the prefix in the tag name.

tags { Name = "${var.environment}-${format("server-%01d", count.index + 1)}" Environment = "${var.environment}" }

You need to specify the type of the variable to be a list:

variable "server_instance_ips" { type = "list" default = [] }

The last question can be solved in many ways, I would go for something similar

availability_zone = "${data.aws_availability_zones.available.names[count.index]}"

Recommend

  • Terraform fails to import key pair with Amazon EC2
  • New git repository error on first push
  • Making an Image Hyperlink in R Shiny header
  • Question mark icons showing up for quotation marks when there's a UTF-8 character encoding
  • Wordpress Category Link get_category_link(id)
  • ViewModel MVC 5 created using 2 classes, controller which using it
  • Link two D3 plots
  • PushPad: Subscribe is removed after site refresh
  • How do I get my links to glow on :hover with CSS3? [duplicate]
  • How to set base path property in swagger for .Net Core Web API
  • how to compile code from svn into jar file?
  • How do I narrow down scope when running an ansible playbook?
  • Apache doesn't execute script of index.php in root automatically
  • What products support 3-digit region subtags, e.g., es-419 for Latin-American Spanish?
  • RegEx replacing IMG src attributes
  • Vim ctags behaves strangely
  • Converting RVML to SVG using XSLT in C#
  • How to get the click event on sIFR embedded tag in IE?
  • How to read JSON-LD data from HTML in Objective-C?
  • Why am I getting an Array of an Array with Doctrine 2 & Symfony 2 Data Transformer?
  • How to fallback to entirely different index page if user has javascript disable?
  • Always require certain dependencies in RequireJS
  • MySQL multiple IN conditions to subquery with same table
  • Is it possible to get the word under the mouse cursor in a ``?
  • BeautifulSoup difference between findAll and findChildren
  • NHibernate manually control fetching
  • Email format validation in mvc3 view
  • Cannot connect to cassandra from Spark
  • Fill an image in a square container while keeping aspect ratio
  • How to model a transition system with SPIN
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Cant find why the layout is getting smaller
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • Is it possible to post an object from jquery to bottle.py?
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Python/Django TangoWithDjango Models and Databases