1

I have a resource that creates multiple s3 access points depending on the input provided. The input is a map with s3 uri as the key and parsed bucket name as the value.

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. 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:

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:

{
"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>
}
Riley Hun
  • 2,541
  • 5
  • 31
  • 77

1 Answers1

2

I would invert your 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 "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
                          }  
                     ]...)
}
Marcin
  • 215,873
  • 14
  • 235
  • 294
  • Unfortunately, we need to output the original s3 uri inputs with the corresponding s3 access points in the output. Do you have any suggestions as to how to achieve this? – Riley Hun Aug 16 '22 at 01:37
  • 1
    You terraform skills are transcendant. Thanks so much! – Riley Hun Aug 16 '22 at 03:06