Terraform - Schleifen

Nov 13 2020

Ist es möglich, eine Schleife zu erstellen, die diese Ressourcen erstellt? Es gibt viele Wiederholungen derselben Ressourcen. Ich habe versucht, mit Maps eine Schleife zu erstellen, aber Map akzeptiert keinen anderen Standardblock.

Oder ist es normal, alle 4 Ressourcen manuell zu erstellen? Nur ein paar Vorschläge als Antwort reichen aus, ich versuche es selbst zu lernen.

resource "aws_subnet" "public-test-a" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.16/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[1]

  tags = {
    Name = var.subnets_names[index]
  }
}

resource "aws_subnet" "public-test-b" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.16/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[1]

  tags = {
    Name = "public-test-b"
  }
}

resource "aws_subnet" "private-test-a" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.32/28"
  availability_zone = var.AZ[0]

  tags = {
    Name = "private-test-a"
  }
}


resource "aws_subnet" "private-test-b" {
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.48/28"
  availability_zone = var.AZ[1]

  tags = {
    Name = "private-test-b"
  }
}

Ich habe so etwas ausprobiert, aber es scheint nicht gültig zu sein.

Wir können aws_vpc.vpc-test-02.iddie Variable auch nicht verwenden , da sie Teil eines anderen tf ist.

variable "subnets" {
  type = map

  default = {
    vpc_id = aws_vpc.vpc-test-02.id
  }

  public-test-a = {
    map_public_ip_on_launch = true
    availability_zone = var.AZ[0]
  }

  public-test-b = {
    map_public_ip_on_launch = true
    availability_zone = var.AZ[1]
  }

  private-test-a = {
    availability_zone = var.AZ[0]
  }

  private-test-b = {
    availability_zone = var.AZ[1]
  }
}

variable "AZ" {
  type = list
  default = ["ap-south-1a", "ap-south-1b", "ap-south-1c"]
}

variable "subnets_cird" {
  type = list
  default = ["10.0.0.0/28", "10.0.0.16/26", "10.0.0.32/28", "10.0.0.48/28"]
}


variable "subnets_names" {
  type = list
  default = ["public-test-a", "public-test-b", "private-test-a", "private-test-b"]
}

Antworten

2 FedorPetrov Nov 13 2020 at 11:14

Für diese Suche zählen und for_each in der Terraform-Dokumentation. Hier ist ein Beispiel, wie Sie public-test-a und public-test-b durch einen public-test ersetzen können:

variable "number_of_subnets" {
default = 2
}

resource "aws_subnet" "public-test" {
  count = var.number_of_subnets
  vpc_id = aws_vpc.vpc-test-02.id
  cidr_block = "10.0.0.16/28"
  map_public_ip_on_launch = true
  availability_zone = var.AZ[1]

  tags = {
    # here you pick the right name according to the subnet index, where e.g subnets_names = ["public-test-a","public-test-b"]
    Name = var.subnets_names[count.index]
  }
}

Das gleiche kann für private_test gemacht werden.

In Bezug auf die Dinge, die Sie versucht haben. Variablen können keine anderen Variablen zugewiesen werden. Um diese Funktionalität zu erreichen, verwenden Sie Einheimische:

locals {
   x = aws_vpc.vpc-test-02.id
}

und dann auf den Wert wie zugreifen

local.x