Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally allow for extra arguments to format() #28558

Closed
Nuru opened this issue Apr 30, 2021 · 2 comments
Closed

Optionally allow for extra arguments to format() #28558

Nuru opened this issue Apr 30, 2021 · 2 comments

Comments

@Nuru
Copy link

Nuru commented Apr 30, 2021

Current Terraform Version

Terraform v0.15.1
on darwin_amd64

Use-cases

In many situation, I like to be able to accept a format string as input to allow for customization of things like AWS IAM role names or AWS Parameter Store keys. Because the format string allows indexed addressing of variables, this is a very flexible solution, because I can provide a static list of possible inputs and people can rearrange them as they like and omit variables they don't want.

  # Construct the SSM parameter store key name for where to store the database user's password
  ssm_key_name = format(var.format_string, var.region, var.stage, var.service, var.db_name, var.db_username)

So you can set

  var.format_string = "/%[5]v"

and ssm_key_name will be set to "/${var.db_username}"

Attempted Solutions

This works if, and only if, the format string references the last variable in the list. Otherwise, it fails with an error too many arguments.

Proposal

Have some way to disable the check for too many arguments. Could be a separate function format_nocheck, could be a special flag in the format string, could be something else, but should not be a command line option. It should be something completely contained in the code.

References

@Nuru Nuru added enhancement new new issue not yet triaged labels Apr 30, 2021
@jbardin jbardin added config and removed new new issue not yet triaged labels Apr 30, 2021
@apparentlymart
Copy link
Contributor

Thanks for sharing this use-case, @Nuru.

Although the specific proposal is different, I think the underlying use-case you've shared here is the same as #26838, and so I'm going to close this one just to consolidate the design discussion over there. I expect we'd prefer to meet this use case using Terraform's own template language rather than the format minilanguage, because it's a superset of the format language (you can call format inside it) and it typically results in a template that is easier to understand without referring to the call in order to see which values are assigned into which positional arguments.

With that said, we'd ideally like to find a way to do it that doesn't lead to the need for writing escaped templates inside templates, because "$${foo}" instead of "${foo}" is pretty hard to write and maintain correctly. The desire for a more integrated solution is what has caused us to hold on solving this right now, until we have some time to do some more thorough design work.

@github-actions
Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants