我有一个资源,它根据提供的输入创建多个s3访问点。输入是一个映射,其中s3uri作为键,解析的bucket名称作为值。
示例:
{
"s3://my_bucket/model1.tar.gz" -> "my_bucket",
"s3://my_bucket_2/model2.tar.gz" -> "my_bucket_2",
"s3://my_bucket/model3.tar.gz" -> "my_bucket"
}
然后,我使用for_each
对映射中的每个元素进行迭代,以创建s3访问点。不幸的是;my_bucket";值,这意味着它将尝试为指定的bucket创建两次s3访问点,因此将错误地显示消息:
AccessPointAlreadyOwnedByYou: Your previous request to create the named accesspoint succeeded and you already own it.
在创建资源之前,如何首先检查访问点是否存在?
代码示例:
resource "aws_s3_access_point" "s3_access_point" {
for_each = var.create ? local.uri_bucket_map : {}
bucket = each.value
name = format("%s-%s", each.value, "access-point")
}
output "s3_access_point_arn" {
description = "The arn of the access point"
value = { for uri, ap in aws_s3_access_point.s3_access_point : uri => ap.arn }
}
期望输出:
{
"s3://my_bucket/model1.tar.gz" -> <access point uri>,
"s3://my_bucket_2/model2.tar.gz" -> <access point uri>,
"s3://my_bucket/model3.tar.gz" -> <access point uri>
}
我会反转您的uri_bucket_map
:
locals {
uri_bucket_map_inverse = {
for k,v in local.uri_bucket_map: v => k...
}
}
给予:
{
"my_bucket" = [
"s3://my_bucket/model1.tar.gz",
"s3://my_bucket/model3.tar.gz",
]
"my_bucket_2" = [
"s3://my_bucket_2/model2.tar.gz",
]
}
然后只创建接入点为:
resource "aws_s3_access_point" "s3_access_point" {
for_each = var.create ? local.uri_bucket_map_inverse : {}
bucket = each.key
name = format("%s-%s", each.key, "access-point")
}
并且输出将使用AP和反向列表映射:
output "s3_access_point_arn" {
description = "The arn of the access point"
value = merge([for bucket_name, ap in aws_s3_access_point.s3_access_point:
{ for uri in local.uri_bucket_map_inverse[bucket_name]:
uri => ap.arn
}
]...)
}