Terraform - loops

大憨熊 提交于 2020-12-15 06:06:25

问题


Is it possible to create a loop that creates this resources? There is a lot of repetition of the same resources. I tried using maps to create a loop but map doesn't accept anything other default block.

Or is it normal to manually create all 4 resources? Just some suggestions as answer is enough, I'm trying to learn it myself.

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"
  }
}

I was trying out something like this but it doesn't seem valid.

Also we can't use aws_vpc.vpc-test-02.id inside the variable since its part of another tf.

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"]
}

回答1:


For this search count and for_each in terraform documentation. Here is an example, how you can replace public-test-a and public-test-b with one public-test:

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]
  }
}

The same can be done for private_test.

Concerning the things you've tried. Variables cannot be assigned another variables. To achieve this functionality use locals:

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

and then access the value like

local.x


来源:https://stackoverflow.com/questions/64819338/terraform-loops

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!