gpt4 book ai didi

Terraform:如何将多个子网关联到路由表?

转载 作者:行者123 更新时间:2023-12-03 16:45:45 26 4
gpt4 key购买 nike

我正在使用 terraform v.0.11.7。

我想创建 4 个子网(2 个公共(public)子网,2 个私有(private)子网)

这是 vars.tf 的内容

variable "region" {
default = "ap-south-1"
}

variable "ami_id" {
type = "map"
default = "ami-d783a9b8"
}

variable "credentials" {
default = "/root/.aws/credentials"
}

variable "vpc_cidr" {
default = "10.0.0.0/16"
}

variable "pub_subnet_aza_cidr" {
default = "10.0.10.0/24"
}

variable "pub_subnet_azc_cidr" {
default = "10.0.20.0/24"
}

variable "pri_subnet_aza_cidr" {
default = "10.0.30.0/24"
}

variable "pri_subnet_azc_cidr" {
default = "10.0.40.0/24"
}

现在在 main.tf 中,我想将前 2 个公共(public)子网关联到公共(public)路由表,该怎么做?
resource "aws_subnet" "pub_subnet_aza" {
vpc_cidr = "{aws_vpc.vpc.id}"
cidr_block = "${var.pub_subnet_aza_cidr}"
tags {
Name = "Pub-Sunet-A"
}
availability_zone = "${data.aws_availability_zone.available.name[0]}"
}

resource "aws_subnet" "pub_subnet_azc" {
vpc_cidr = "{aws_vpc.vpc.id}"
cidr_block = "${var.pub_subnet_azc_cidr}"
tags {
Name = "Pub-Subnet-C"
}
availability_zone = "${data.aws_availability_zone.available.name[2]}"
}

resource "aws_route_table_association" "public" {
subnet_id = "${aws_subnet.pub_subnet_aza.id}" # How to put pub_subnet_azc.id into here?
route_table_id = "${aws_route_table.public.id}"
}

最佳答案

更好地使用子网列表来减少变量的数量。然后你也可以使用count = length(var.subnets)获取路由表关联资源的 2 个实例并从子网列表中选择正确的一个。

variable "subnet_cidrs_public" {
description = "Subnet CIDRs for public subnets (length must match configured availability_zones)"
# this could be further simplified / computed using cidrsubnet() etc.
# https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum-
default = ["10.0.10.0/24", "10.0.20.0/24"]
type = "list"
}

resource "aws_subnet" "public" {
count = "${length(var.subnet_cidrs_public)}"

vpc_id = "${aws_vpc.main.id}"
cidr_block = "${var.subnet_cidrs_public[count.index]}"
availability_zone = "${var.availability_zones[count.index]}"
}

resource "aws_route_table_association" "public" {
count = "${length(var.subnet_cidrs_public)}"

subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
}

我看到您一直在通过 data 阅读可用区,这很好,你仍然可以这样做。您只需要以某种方式设置子网和 AZ 之间的关联。我把它留给你。

当然更优雅的是在该区域的每个 AZ 中配置一个子网。一旦我们使用 cidrsubnet() 要计算子网的地址空间,我们可以使用 length(data.availability_zones)作为所有其余部分的驱动程序。不应该太复杂。

这是完整的代码:
provider "aws" {
region = "eu-west-1"
}

variable "availability_zones" {
description = "AZs in this region to use"
default = ["eu-west-1a", "eu-west-1c"]
type = "list"
}

variable "vpc_cidr" {
default = "10.0.0.0/16"
}

variable "subnet_cidrs_public" {
description = "Subnet CIDRs for public subnets (length must match configured availability_zones)"
# this could be further simplified / computed using cidrsubnet() etc.
# https://www.terraform.io/docs/configuration/interpolation.html#cidrsubnet-iprange-newbits-netnum-
default = ["10.0.10.0/24", "10.0.20.0/24"]
type = "list"
}

resource "aws_vpc" "main" {
cidr_block = "${var.vpc_cidr}"
tags {
Name = "stackoverflow-51739482"
}
}

resource "aws_subnet" "public" {
count = "${length(var.subnet_cidrs_public)}"

vpc_id = "${aws_vpc.main.id}"
cidr_block = "${var.subnet_cidrs_public[count.index]}"
availability_zone = "${var.availability_zones[count.index]}"
}

resource "aws_route_table" "public" {
vpc_id = "${aws_vpc.main.id}"

tags {
Name = "public"
}
}

resource "aws_route_table_association" "public" {
count = "${length(var.subnet_cidrs_public)}"

subnet_id = "${element(aws_subnet.public.*.id, count.index)}"
route_table_id = "${aws_route_table.public.id}"
}

关于Terraform:如何将多个子网关联到路由表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51739482/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com