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

Question: How to store an arbitrary value in state? #32270

Closed
gtmtech opened this issue Nov 22, 2022 · 7 comments
Closed

Question: How to store an arbitrary value in state? #32270

gtmtech opened this issue Nov 22, 2022 · 7 comments
Labels
enhancement new new issue not yet triaged

Comments

@gtmtech
Copy link

gtmtech commented Nov 22, 2022

Terraform Version

N/A

Use Cases

I want to store an arbitrary value in state. I will know the value (passed in by an environment variable), and I just want to store it in state. I imagine there is a provider for this somewhere but I couldn't see an easy one to use .

My actual usecase is that I want to know from which git commit a terraform infra was last deployed. I have all my terraform resources in a module (module.root), so i figure outside the top level module I'll have some arbirary resource like this:

resource "some_resource" "git_commit_hash" {
    value = var.git_commit_hash
    depends_on = [ 
        module.root
    ]
}

This will mean the some_resource only gets applied when everything in the module.root is successfully applied - great!

By looking at all my git_commit_hash resources across all my statefiles I will understand the last successful githash that was applied.

Attempted Solutions

I am looking for a provider or terraform resource that can just store a value, and I cant think of one, despite searching. I'm sure there is one

Proposal

No response

References

No response

@gtmtech gtmtech added enhancement new new issue not yet triaged labels Nov 22, 2022
@gtmtech
Copy link
Author

gtmtech commented Nov 22, 2022

Related somewhat to #2550 which @crw closed recently

@bflad
Copy link
Contributor

bflad commented Nov 22, 2022

Related: #31757

@jbardin
Copy link
Member

jbardin commented Nov 22, 2022

Hi @gtmtech,

The objects which are stored in state consist of managed resources, and root modules output. The canonical utility resource for storing arbitrary values is the null_resource, but as @bflad mentioned, there is a proposal for a more flexible builtin resources to take its place.

@gtmtech
Copy link
Author

gtmtech commented Dec 20, 2022

@jbardin I didn't think null_resource stored any values in state? - what am I missing?

@jonwtech
Copy link

jonwtech commented Dec 20, 2022

How about:

resource "local_file" "hash" {
  content  = var.git_commit_hash
  filename = "/dev/null"
  depends_on = [ 
    module.root
  ]
}

Not perfect as Terraform will always want to create the resource, but the value is stored in state.

@jbardin
Copy link
Member

jbardin commented Dec 20, 2022

@gtmtech, null_resource has a triggers attribute which is a map that can store any number of string values. The above PR for a terraform_data resource has been merged for v1.4, which has dynamically typed attributes that can store any type.

Closed via #31757

@jbardin jbardin closed this as completed Dec 20, 2022
@github-actions
Copy link

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 Jan 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants