gpt4 book ai didi

amazon-web-services - Terraform 错误创建子网依赖

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

我正在尝试启动并运行一个 documentdb 集群,并让它在我创建的私有(private)子网中运行。

在没有 depends_on 的情况下运行下面的配置我收到以下错误消息,因为尚未创建子网:

Error: error creating DocDB cluster: DBSubnetGroupNotFoundFault: DB subnet group 'subnet-0b97a3f5bf6db758f' does not exist.
status code: 404, request id: 59b75d23-50a4-42f9-99a3-367af58e6e16

添加了依赖设置以等待子网创建但遇到问题。

  cluster_identifier      = "my-docdb-cluster"
engine = "docdb"
master_username = "myusername"
master_password = "mypassword"
backup_retention_period = 5
preferred_backup_window = "07:00-09:00"
skip_final_snapshot = true
apply_immediately = true
db_subnet_group_name = aws_subnet.eu-west-3a-private
depends_on = [aws_subnet.eu-west-3a-private]
}

在运行 terraform apply 时,我收到配置错误:

Error: error creating DocDB cluster: DBSubnetGroupNotFoundFault: DB subnet group 'subnet-0b97a3f5bf6db758f' does not exist.
status code: 404, request id: 8b992d86-eb7f-427e-8f69-d05cc13d5b2d

on main.tf line 230, in resource "aws_docdb_cluster" "docdb":
230: resource "aws_docdb_cluster" "docdb"

最佳答案

数据库子网组本身就是一种逻辑资源,它告诉 AWS 它可以在 VPC 中的什么地方安排数据库实例。它不是直接引用子网,而这正是您要在那里做的事情。

要创建数据库子网组,您应该使用 aws_db_subnet_group resource .然后在创建数据库实例或集群时直接按名称引用它。

一个基本的例子是这样的:

resource "aws_vpc" "example" {
cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "eu-west-3a" {
vpc_id = aws_vpc.example.id
availability_zone = "a"
cidr_block = "10.0.1.0/24"

tags = {
AZ = "a"
}
}

resource "aws_subnet" "eu-west-3b" {
vpc_id = aws_vpc.example.id
availability_zone = "b"
cidr_block = "10.0.2.0/24"

tags = {
AZ = "b"
}
}

resource "aws_db_subnet_group" "example" {
name = "main"

subnet_ids = [
aws_subnet.eu-west-3a.id,
aws_subnet.eu-west-3b.id
]

tags = {
Name = "My DB subnet group"
}
}

resource "aws_db_instance" "example" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "mydb"
username = "foo"
password = "foobarbaz"
parameter_group_name = "default.mysql5.7"
db_subnet_group_name = aws_db_subnet_group.example.name
}

同样的事情适用于 Elasticache subnet groups使用 aws_elasticache_subnet_group resource .


还值得注意的是,将 depends_on 添加到已经通过插值引用依赖资源的资源中不会执行任何操作。 depends_on 元参数适用于不公开仅直接提供此依赖信息的参数的资源。

关于amazon-web-services - Terraform 错误创建子网依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59239970/

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