Multiple Terrara Accessibility Areas on AWS

The VPC I'm working on has 3 logical levels: Web, App, and DB. For each level, there is one subnet in each availability zone. Only 6 subnets in the region that I use.

I am trying to create EC2 instances using a module and parameter count, but I do not know how to say terraform in order to use two App level subnets. An additional limitation that I have is to use static IP addresses (or a way to have a deterministic private name).

I play with a resource

resource "aws_instance" "app_server" {
  ...
  count = "${var.app_servers_count}"

  # Not all at the same time, though!
  availability_zone = ...
  subnet_id = ...
  private_ip = ...
}

Things I've tried / thought so far:

  • Use data "aws_subnet" "all_app_subnets" {...}, filter by name, get all subnets that match and use them as a list. But aws_subnetcannot return the list;
  • data "aws_availability_zones" {...}, . ;
  • data "aws_subnet_ids" {...}, . , -, namel
  • . , ;
  • data "aws_subnet" "app_subnet_1" {...}, data "aws_subnet" "app_subnet_2" {...}, , ;
  • , , map . ;
  • . ... ?

. , . , . - ?

.

+10
4

, , data "aws_subnet_ids" {...} , , , terraform count:

variable "target_vpc" {}
variable "app_server_count" {}
variable "app_server_ip_start" {}

# Discover VPC
data "aws_vpc" "target_vpc" {
  filter = {
    name = "tag:Name"
    values = ["${var.target_vpc}"]
  }
}

# Discover subnet IDs. This requires the subnetworks to be tagged with Tier = "AppTier"
data "aws_subnet_ids" "app_tier_ids" {
  vpc_id = "${data.aws_vpc.target_vpc.id}"
  tags {
    Tier = "AppTier"
  }
}

# Discover subnets and create a list, one for each found ID
data "aws_subnet" "app_tier" {
  count = "${length(data.aws_subnet_ids.app_tier_ids.ids)}"
  id = "${data.aws_subnet_ids.app_tier_ids.ids[count.index]}"
}

resource "aws_instance" "app_server" {
  ...

  # Create N instances
  count = "${var.app_server_count}"

  # Use the "count.index" subnet
  subnet_id = "${data.aws_subnet_ids.app_tier_ids.ids[count.index]}"

  # Create an IP address using the CIDR of the subnet
  private_ip = "${cidrhost(element(data.aws_subnet.app_tier.*.cidr_block, count.index), var.app_server_ip_start + count.index)}"

  ...
}
+5

Terraform aws_subnet_ids , ( /).

:

variable "vpc" {}
variable "ami" {}
variable "subnet_tier" {}
variable "instance_count" {}

data "aws_vpc" "selected" {
  tags {
    Name = "${var.vpc}"
  }
}

data "aws_subnet_ids" "selected" {
  vpc_id = "${data.aws_vpc.selected.id}"

  tags {
    Tier = "${var.subnet_tier}"
  }
}

resource "aws_instance" "instance" {
  count         = "${var.instance_count}"
  ami           = "${var.ami}"
  subnet_id     = "${data.aws_subnet_ids.selected.ids[count.index]}"
  instance_type = "${var.instance_type}"
}

, AZ A . , AWS API AZ, , AZ (, AZ A, , , , ).

- , - , AZ A, , , , AZ, , Terraform, .

+3

, , . Terraform

(, ) - . , , . .

subnet_id = "${element(data.aws_subnet_ids.app_tier_ids.ids, count.index)}"
+1

You can evenly distribute instances across several zones modulo.

variable "zone" {
  description = "for single zone deployment"
  default = "europe-west4-b"
}

variable "zones" {
  description = "for multi zone deployment"
  default = ["europe-west4-b", "europe-west4-c"]
}

resource "google_compute_instance" "default" {
  count = "${var.role.count}"
  ...
  zone = "${var.zone != "" ? var.zone: var.zones[ count.index % length(var.zones) ]}"
  ...
}

This distribution mechanism allows you to evenly distribute nodes into zones.
For instance. zones = [A, B] - instance-1 will be in A, instance-2 will be in B, instance-3 will be again in A.
Adding zone C to the zones will move instance 3 to C.

+1
source

Source: https://habr.com/ru/post/1685033/


All Articles