Live data from Hacker News

Terraform should have remained stateless

bejarano.io

191–200 of 329 posts

Re: Terraform should have remained stateless

#191
post #172

Earlier quoted context omitted.

So the position is Terraform should have state, but rather than keep it in one place such as an S3 object, it should be spread across the metadata of many resources? Some issues with that: - Fetching the whole state would be hugely impractical due to the number of API calls - The risk of losing state information by a resource being deleted outside of Terraform is greater - Again, not all resources have metadata that…

>- The risk of losing state information by a resource being deleted outside of Terraform is greater This isn't losing state information though! That is the state. If state information were kept outside this it would now be wrong which means terraform *would do the wrong thing.

No because the metadata on the deleted resource is now lost.

With the information being stored outside the resource, we know that it was deleted and the metadata about it.

Re: Terraform should have remained stateless

#192

Basically agree with the article. I've used direct cloud formation, AWS SAM, Ansible, terraform, and AWS CDK to spin up infrastructure... My hard line opinion is that if something NEEDS state management to exist and update, it's a pet, treat it like a pet. Don't mix pets with the rest of your automated machinery except to the minimum extent required, when absolutely necessary. We had to rewrite an Ansible role becaus…

> I've almost never had this happen with any AWS tool... Sure, there's drift possibilities, but those are controllable by mainly not letting humans do things, and not having multiple cooks in the kitchen changing things in automation, which are good ideas for terraform and Ansible to...

This is also what you do with Terraform: that's what the Terraform Cloud product is about (or you can just build a CI pipeline in your tool of choice with a blog post's amount of work).

It also sounds like what you're saying is that you can just avoid all these problems by having everything automated from day one, but that's not the reality in any employer I've ever worked for. Unless you're starting a company today and happen to have an experienced infrastructure engineer on staff from day one you're not getting that world.

Terraform state also comes in handy as an audit trail. Through versions of your state file, you can see how your infrastructure changed in the past (You could use S3 versioning or you could have Terraform Cloud manage the state file automatically).

Terraform Cloud turns that concept into a relatively powerful feature. It becomes a compliance record: who changed what and how it changed, along with diff files.

As another commenter pointed out, it really seems like the alternative tool you're using (CloudFormation) has its own concept of state, but it's just hiding it from you. Pulumi also has a concept of state. I haven't really seen an infrastructure automation tool without that concept.

Ultimately, these tools need to have some way to track what resource the code is referencing. Call that metadata or state, you have to track something. And if you come up with something that somehow avoids it, I have to ask why? It seems like trying really hard to make a car without doors that also prevents rain from coming inside: why not just leave the doors on the car?

Re: Terraform should have remained stateless

#193

Earlier quoted context omitted.

No DNS provider I've ever used has a way to store metadata alongside a DNS record (like an individual A record)

That's what TXT records are for: https://en.m.wikipedia.org/wiki/TXT_record

Exposed to the world?

Re: Terraform should have remained stateless

#195

Earlier quoted context omitted.

No DNS provider I've ever used has a way to store metadata alongside a DNS record (like an individual A record)

That's what TXT records are for: https://en.m.wikipedia.org/wiki/TXT_record

Terraform can be used to manage TXT records too. Where does it store metadata for them? Or is it TXT all the way down?

Re: Terraform should have remained stateless

#197
post #176

Earlier quoted context omitted.

How would TF work without state and without tags? Let's say your TF config declares a database Foo. Your AWS account has databases Foo and Bar. How does TF know whether it's responsible for database Bar and whether to delete it?

Resource is tagged with “terraform stack x created this”?

The parent said he thinks tags aren't the right approach. I wanted to know what he proposes instead.

Re: Terraform should have remained stateless

#198
post #176

Earlier quoted context omitted.

Resource is tagged with “terraform stack x created this”?

Interesting. Disregarding for a moment that not all cloud resources supporting tags on all cloud providers (and let's not get started with all the non-cloud stuff Terraform is capable to manage), how does tf know that `stack x` is the thing that's currently running?

By the time the tag has enough information to be useful, you are storing a TF state file in a tag. Anyway, I don't think people complaining about a s3 bucket for tf state actually has experience creating tools like tf.

Re: Terraform should have remained stateless

#200

Assuming every resource has a name field, auto prefix it with "tf-" If the resource is prefixed with "tf-" but missing in the terraform config, delete it.

What happens when you have multiple terraform repositories interacting with the same account?
Post reply on HN