[英]Terraform: How to check if s3 access point exists before creating it?
I have a resource that creates multiple s3 access points depending on the input provided.我有一个资源可以根据提供的输入创建多个 s3 接入点。 The input is a map with s3 uri as the key and parsed bucket name as the value.输入是一个 map,其中 s3 uri 作为键,解析后的存储桶名称作为值。
Example:例子:
{
"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"
}
I then use for_each
to iterate through each element in the map to create s3 access points.然后我使用for_each
遍历 map 中的每个元素以创建 s3 接入点。 Unfortunately, there are 2 "my_bucket" values in the map, which means it will attempt to create s3 access points for that designated bucket twice, and thus will error out with message:不幸的是,map 中有 2 个“my_bucket”值,这意味着它将尝试为该指定存储桶创建 s3 访问点两次,因此会出错并显示以下消息:
AccessPointAlreadyOwnedByYou: Your previous request to create the named accesspoint succeeded and you already own it.
How can I check that the access point exists first before creating the resource?在创建资源之前如何检查访问点是否存在?
Code Example:代码示例:
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 }
}
Desired Output:所需的 Output:
{
"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>
}
I would invert your uri_bucket_map
:我会反转你的uri_bucket_map
:
locals {
uri_bucket_map_inverse = {
for k,v in local.uri_bucket_map: v => k...
}
}
giving:给予:
{
"my_bucket" = [
"s3://my_bucket/model1.tar.gz",
"s3://my_bucket/model3.tar.gz",
]
"my_bucket_2" = [
"s3://my_bucket_2/model2.tar.gz",
]
}
then just create access points as:然后只需将访问点创建为:
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")
}
and the output would use both the APs and the inverted list map: output 将同时使用 AP 和倒排列表 map:
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
}
]...)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.