Terraform-循环 [英] Terraform - loops

查看:94
本文介绍了Terraform-循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建一个创建此资源的循环?大量重复使用相同的资源.我尝试使用地图创建循环,但地图不接受其他任何默认块.

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.

或者手动创建全部4种资源是否正常?仅仅给出一些建议作为答案就足够了,我正在尝试自己学习.

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.

我们也不能在变量内使用 aws_vpc.vpc-test-02.id ,因为它是另一个tf的一部分.

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

推荐答案

此搜索计数和terraform文档中的for_each.这是一个示例,如何用一个公共测试替换public-test-a和public-test-b:

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

对于private_test也可以这样做.

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
}

然后访问值,如

local.x

这篇关于Terraform-循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆