Live data from Hacker News

Terraform should have remained stateless

bejarano.io

121–130 of 329 posts

Re: Terraform should have remained stateless

#121

Is "remained stateless" accurate? The document only indicates that they used to try storing state in tags, not that it was stateless. "Stateless", to me, would mean that it tries to compare current resources with your current configuration without any metadata. If you mean that they should have tried to keep using tags, not every resource and cloud provider supporting tags pretty much ends the possibility of that. If…

It's not a drift detection tool for resources that are manually created.

Re: Terraform should have remained stateless

#122

The article misses a key bit of information TF needs when making a plan: 4. The previous Terraform configuration This is effectively stored by state. We need this because if a resource is removed from the new config then Terraform needs to be able to delete the existing resource from the world. If we don’t have the state then Terraform must either: 1. Not delete it from the world 2. Or risk deleting something not man…

TF noob here: is it not possible to store a "managed by terraform" annotation on the resources ? Or are not all resources able to store labels (extreme example: terraform-spotify plugin)

Re: Terraform should have remained stateless

#123

Earlier quoted context omitted.

I think there is a misunderstanding: when I'm talking about "name" I mean the "name" stored by the cloud provider, which is typically (arbitrarily) chosen as the idempotency key by stateless implementations. When you change the name of the resource that will, obviously, break the connection, that's a big no-no when writing Terraform code.

Sure, but isn't the artcle suggesting tags rather than the name / ID ? One problem, as mentioned in a sibling comment, is if you don't have state then how do you know a resource has been deleted? One other option might be a system that compares the "desired state" from the previous version of the config. That might be an interesting approach - keeping a history of config. But that is, of course, state :) I use TF a b…

Not every provider supports tags, or they don't support it for all resources. Sure, if you only use AWS this may be a solution, but not for everyone.

Regarding the state, it took me a very long time to come up with good organization for my code, but it works once you've gotten used to it. I really only need to mess with state files when there is a bug in the provider like the aforementioned GitHub issue.

Re: Terraform should have remained stateless

#124
post #122

The article misses a key bit of information TF needs when making a plan: 4. The previous Terraform configuration This is effectively stored by state. We need this because if a resource is removed from the new config then Terraform needs to be able to delete the existing resource from the world. If we don’t have the state then Terraform must either: 1. Not delete it from the world 2. Or risk deleting something not man…

TF noob here: is it not possible to store a "managed by terraform" annotation on the resources ? Or are not all resources able to store labels (extreme example: terraform-spotify plugin)

It is possible for resources that support tags, but not everything supports tags.

Re: Terraform should have remained stateless

#125

Earlier quoted context omitted.

Most (all?) cloud providers support some form of tagging. Have like a `managed-by=terraform` tag, and assume everything with that tag is Terraform managed.

There are a lot of resources in AWS that don't support tags.

Do you have a few examples?

Re: Terraform should have remained stateless

#126

Earlier quoted context omitted.

Last I checked Ansible can cache facts to a persistent store like a file.

Puppet also has PuppetDB, which is functionally similar.

I might be wrong here but I think Puppet DB is more like an eventually consistent database that stores the last known state of potentially thousands of machines that may not be reachable or even turned on. The use case is totally different. Puppet runs on each machine "in isolation", and before Puppet DB, the agent had no way to know about anything external to the local machine. It can be used while assembling the manifest that gets passed to a machine, but when the puppet agent actually runs on that machine it doesn't ask Puppet DB what state the local machine is in, it just looks directly at the resources because they're local - there's no performance penalty.

Re: Terraform should have remained stateless

#127

Is "remained stateless" accurate? The document only indicates that they used to try storing state in tags, not that it was stateless. "Stateless", to me, would mean that it tries to compare current resources with your current configuration without any metadata. If you mean that they should have tried to keep using tags, not every resource and cloud provider supporting tags pretty much ends the possibility of that. If…

It's not a drift detection tool for resources that are manually created.

Are you saying it should?

I think whether that's desirable or not is a matter of preference. The nice thing about Terraform is that you don't have to go all in. I don't really want to have to tell it to ignore every resource that I want to manually manage, and I don't want it wasting the extra API calls on trying to find every resource that's not in my configuration every time I want to deploy.

Now, if they provided a separate tool for detecting manually created resources, that would be awesome, but it wouldn't fit into the typical flow of showing drift regularly just before deploying in my opinion. That's more about detecting whether you're about to overwrite something that was manually done, or if you're about to make changes assuming your infrastructure is as you last left it. I don't need to take inventory of every single resource I have every time before deploying.

Re: Terraform should have remained stateless

#128
Terraform has to have state, for the reasons outlined by other commenters.

But, it is undoubtedly a pain in the arse in practice - invariably someone else is doing a change on a branch but has already applied it to the development infra using the shared state bucket, which then pollutes it for everyone else as you don't have those changes so your applys will now want to undo them.

As a first timer with this stuff, I think a key lesson learned is to balkanise your terraform quite heavily - we have a tfstate per environment but that is nowhere near granular enough, slicing it into smaller pieces would obviate many of those 'pollution' problems.

Re: Terraform should have remained stateless

#129
post #75

Earlier quoted context omitted.

Most (all?) cloud providers support some form of tagging. Have like a `managed-by=terraform` tag, and assume everything with that tag is Terraform managed.

Im gonna ignore the fact that moving state to other place completwly misses article point, but: Two Users create exactly the same resource with the same tags. Which one should be removed by Terraform? Now either way lets ignore that. You want to refresh infrastructure to know what to do. Without the state you have to go through EVERY API CALL on every service even those you did not create to be able to determine the…

I don't believe it misses the article's point - I think he's asking the valid question "if the target system has the ability to to store all the required state in order to understand mappings between what I want and what I have, why do we need additional state files which always seem to be wrong"

And a good answer might be "all providers don't have that capability" and/or "providers can't efficiently answer questions about that, such as 'find me all things with this configuration tag'.

In your example, those two users wouldn't have the same tags, because you'd arrange it so that they didn't - either by user or a resource grouping based on the configuration itself. This is the choice made by some other tooling, for better or worse.

Re: Terraform should have remained stateless

#130
Given that there are well over 100 comments in this thread it’s possible that someone already mentioned this, but for people who are short-staffed but highly adventurous NixOS has an interesting take on all this. There’s even a Terraform binding called Terranix that isn’t too bad.

It’s a real journey and not for people under short-term time pressure, but of all the pain-in-the-ass things I’ve learned in computing over the years it’s paid me back well-above average.

Post reply on HN