gpt4 book ai didi

Terraform 和 VPC 对等互连

转载 作者:行者123 更新时间:2023-12-02 03:23:08 25 4
gpt4 key购买 nike

因此,我们在我们的组织中非常广泛地使用 terraform,我对其他人如何进行 VPC 对等互连有一些疑问。连接的初始创建非常简单。我们从刚刚创建的 VPC 拉入并引用另一个 VPC,然后它填充路由表等。问题出在我们刚刚与之对等的 VPC 上。现在,我们必须手动转到其他网络堆栈并手动添加 CIDR/PCX id 作为变量。我编写了一个脚本,可以让我们更轻松地处理这个问题,但我想问是否有人针对任何现有 VPC 动态执行 AWS 查找,并自动将现有 PCX 添加到该 VPC 的路由表中。

OPS VPC 就是一个体现这一点值(value)的示例。我们有OPS,然后是dev、prod、qa、stg、uat、cte等。因此,当我们创建CTE vpc时,它会自动创建一个pcx并将其链接到ops并路由到ops。然而,ops 并不知道这个新的 pcx。所以我们必须手动添加。我希望运营商能够自行进行资源查找,并为其找到的任何新 VPC/PCX 提供自己的资源。

TLDR;一种使双向 VPC 对等互连更加动态的方法

最佳答案

假设您使用 remote state backend ,您可以将 OPS 网络堆栈拉入 remote state data source然后从您希望它能够路由到的任何临时堆栈更改其路由表。

将尝试做一个最小的示例(显然缺少大量样板):

# my_ops_stack.tf

provider "aws" {
region = "eu-west-1"
}

module "ops_stack" {
source = "/my/modules/ops_stack"
cidr = "10.1.0.0/16"
// other vars probably
}

// the outputs which will be accessible
// via the remote state data source:
output "routing_table_id" {
value = "${module.ops_stack.routing_table_id}"
}
output "vpc_id" {
value = "${module.ops_stack.vpc_id}"
}
output "vpc_cidr" {
value = "10.1.0.0/16"
}

我现在 configure使用 terraform cli (this will soon be possible in config) 的该堆栈的远程状态后端:

# Run in the same folder as my_ops_stack.tf
terraform remote config \
-backend=s3 \
-backend-config="bucket=my-state-bucket" \
-backend-config="key=ops-stack/terraform.tfstate" \
-backend-config="region=eu-west-1"

现在状态后端已配置,您应用于堆栈的任何更改都将同步到该后端:

terraform apply
# the usual stuff... but now synced with s3!

现在,在新的临时堆栈的模板中(dev、prod、qa、stg、uat、cte 等):

# my_dev_stack.tf

provider "aws" {
region = "eu-west-1"
}

// Pull in your ops stack from the remote backend:
data "terraform_remote_state" "ops_stack" {
backend = "s3"
config {
bucket = "my-state-bucket"
key = "ops-stack/terraform.tfstate"
region = "eu-west-1"
}
}

// Create your dev stack
module "dev_stack" {
source = "/my/modules/dev_stack"
cidr = "10.2.0.0/16"
// The ops_stack vpc id for creating the peering connection:
ops_vpc_id = "${data.terraform_remote_state.ops_stack.vpc_id}"
// Maybe some security group rules you wanna setup
allow_access_from = "${data.terraform_remote_state.ops_stack.vpc_cidr}"
// other vars probably
}

// And use its outputs to add a route to the
// ops vpc routing table from the dev stack!
resource "aws_route" "ops_to_dev" {
route_table_id = "${data.terraform_remote_state.ops_stack.routing_table_id}"
destination_cidr_block = "10.2.0.0/16" // dev_stack's cidr
vpc_peering_connection_id = "${module.dev_stack.vpcx_id}"
}

完成临时堆栈后,您可以安全地销毁它,它甚至会清理其在操作堆栈中的路由。

希望这就是您想要的!

关于Terraform 和 VPC 对等互连,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41818231/

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