-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Current Terraform Version
Terraform v0.12.2
Use-cases
My use case is being able to replace a suffix in an AWS ARN for secrets manager
The arn has the following structure
arn:aws:secretsmanager:us-east-1:123456789012:secret:path/to/secret-name-<random chars>
I want to replace the last 6 chars with a wildcard.
Having the ability to use negative indices I could split the ARN on - then slice -1 as the end index to remove the last element, then concatenate with my wildcard.
This would allow editing of lists in place instead of having to compute the length to pass
Attempted Solutions
This works but is quite verbose and requires intermediate variables
locals {
split_arn = split("-", aws_secretsmanager_secret.secret.id)
arn_without_suffix = slice(local.split_arn, 0, length(local.split_arn)-1)
wildcard_arn = join("-", concat(local.arn_without_suffix, ["??????"]))
}We could transform it into a 1 liner by splitting twice (once for the value and once for the length) but that becomes unreadable.
Proposal
Allow negative indices for the end of the slice in the slice function
- Allow negative indices in https://github.com/hashicorp/terraform/blob/master/lang/funcs/collection.go#L1141
- If the index is negative, substract it from the list length in https://github.com/hashicorp/terraform/blob/master/lang/funcs/collection.go#L1117
Then have a look for other similar functions that should support it and make it consistent, for example element().
With this change the example above can be simplified to
join("-", concat(slice(split("-", aws_secretsmanager_secret.secret.id), 0, -1), ["??????"]))