deploying to multiple availability zones at the same time

2,637 views
Skip to first unread message

Sean Farrow

unread,
Sep 7, 2016, 3:43:08 AM9/7/16
to terrafo...@googlegroups.com

Hi,

 

Is there a way with terraform to deploy an EC2 instance to more than one AZ in a region concurrently?

Any help/best practice appreciated.

Kind regards

Sean.

David Maze

unread,
Sep 7, 2016, 6:50:08 AM9/7/16
to Terraform
On Wednesday, September 7, 2016 at 3:43:08 AM UTC-4, Sean Farrow wrote:

Is there a way with terraform to deploy an EC2 instance to more than one AZ in a region concurrently?


I have a shared "instance" module that declares:

 resource "aws_instance" "instance" {

  count = "${var.count}"

  # NB: element() wraps, so this is (count.index mod len(AZs))

  availability_zone = "${element(split(",", var.availability_zones), count.index)}"

  subnet_id = "${element(split(",", var.subnet_ids), count.index)}"


So if count is 4, and availability_zones is "us-east-1a,us-east-1b", and subnet_ids matches availability_zones, it will deploy 4 instances, 2 in each AZ.  If count is 1 and the settings are otherwise the same, the single instance will go into us-east-1a.

(I feel like there's some significant best practices around regions and availability zones that I totally just don't know.)

David Adams

unread,
Sep 7, 2016, 8:34:47 AM9/7/16
to terrafo...@googlegroups.com
Region strategy is very different, but for availability zones, this example by David Maze is almost exactly how we handle it. The difference is that I don't think you need to specify "availability_zone" if you're using a VPC and subnets. Specifying the subnet implies the AZ (and the VPC), and is all the AWS API needs.

Worth noting that there's an aws_availability_zones data resource that you can use to avoid having to enumerate all the AZs in your region by hand. That said, if you don't want to use all AZs available to you, it's sort of pointless.

But typically I would recommend using three AZs--if available in the region--for any service you wish to remain completely resilient. For services like Consul or Etcd that do leader election, three zones would be required for its quorum to always survive an isolated outage of one AZ. AZ outages are relatively rare but do happen (there was one in the ap-southeast-2 region earlier this year), so if you want your service to remain available, spread it out.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/84c754dc-a2f3-4e0f-9329-3b487d4cd18a%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages