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

Support unique id generation with user-defined prefixes #2269

Closed
phinze opened this issue Jun 8, 2015 · 7 comments
Closed

Support unique id generation with user-defined prefixes #2269

phinze opened this issue Jun 8, 2015 · 7 comments

Comments

@phinze
Copy link
Contributor

phinze commented Jun 8, 2015

Right now we have resource.UniqueId() which is a feature that provider authors can use to generate guaranteed-unique values for attributes.

These values have a static prefix of terraform- and a blob of alphanumeric characters.

It'd be helpful if we could give users a bit more control over unique ID generation:

  • Use a unique value in any attribute
  • Define a custom prefix

We can implement this as an interpolation function uuid(), but we don't currently have any way of preventing the identifier from being regenerated on every plan and apply (thereby creating a perpetual diff). The ignore_changes meta-parameter proposed in #2018 should help with this, and is basically a prerequisite to any sane UX. (Not sure if I missed any other caveats of the implications of a constantly changing interpolation function.)

Comments on this proposed feature are welcome!

@jszwedko
Copy link
Contributor

👍 this would be very useful for us.

@ghost
Copy link

ghost commented Sep 30, 2015

++1

@ilijaljubicic
Copy link

have a specific situation right now,
need to create s3 bucket resource having random string append to a prefix as a name:
mybucket-4234234324234

once created it shouldn't change until destroyed.

not sure if there is better way to do this at moment,
but what I ended up is to create bucket name variable which
is created new only if terraform output doesn't have it yet
otherwise it gets if from output and put into the vars (so it comes same on the next update).

having uuid() as interpolation function would work great given that ignore_changes is there as well
in above case.

+1

@apparentlymart
Copy link
Contributor

I had previously left a comment over in #494 that seems to be more relevant over here.

To restate it in a manner that's more applicable to this issue, I'd like to propose/discuss the idea of "resource metadata" that can, unlike resource data, be interpolated into the resource itself.

Resource metadata is updated by Terraform as part of the planning process, and could include:

  • created_time - set to the current timestamp when a resource is first created, and then constant forever after unless the resource is deleted and recreated.
  • updated_time - this one is tricker since it has some chicken-and-egg issues to figure out with planning, but the idea is that it would update each time the resource is updated. I'd imagining that during plan the previous value would be used to prevent creating needless diffs, but then the new value would be inserted "just in time" for any resource that has an update diff. (This one is trickier than the others and it could be left out if it's too hard to model.)
  • unique_id - a guid or something guid-like that's generated when a resource is created, and just like created_time it never changes unless the resource is deleted and recreated.

The design I had in mind is that Terraform records all of the above values in a new section of the instance state so that they'll persist between runs, and then Terraform itself will manage them to ensure that they get set to the right values during plan/apply.

Unique Id generation can then look something like this:

resource "aws_security_group" "foo" {
    name = "example-${meta.self.unique_id}"
    // ...
}

But you could also do something like this, using metadata between resources (using the proposal from #2696 as an example, just because it's the simplest example of such a construction that sprang to my mind):

resource "something" "anything" {}

resource "something_else" "anything_else" {}

// Run a provisioner when either of the above resources is updated.
resource "stateful_provisioning" "foo" {
    state_key = "${meta.something.anything.updated_time} ${meta.something_else.anything_else.updated_time}"

    provisioner "whatever" {
        // ...
    }
}

I think this model makes a better building block within Terraform than relying on ignore_changes to include an otherwise-constantly-changing value, since it allows Terraform itself to manage when these values change on a per-resource basis, rather than simply forcing the idea that a value will be set and will never change again.

@ilijaljubicic
Copy link

yes, above suggestion looks very promising for enhancing terraform workflow

@phinze
Copy link
Contributor Author

phinze commented Mar 14, 2016

Landed a simple uuid() function in #5575. We can lean on the rest of our interpolation functionality for formatting tweaks: e.g. "myprefix-${uuid()}" "${replace(uuid(), "-", "")}", etc.

@ghost
Copy link

ghost commented Apr 27, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 27, 2020
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

4 participants