News for hackers. Stuff that matters.
Re:Terraform should have remained stateless by janosdebugs on Saturday May 28, 2022 @12:26AM (#31537599)
I respectfully disagree. If TF was stateless, you'd have to manage situations by hand that involve changing the idempotency key, such as the name of a VM. You'd also have to manage situations by hand where a resource is removed from the config.

The whole point of TF is that it has state and doesn't require workarounds for these scenarios. Yes, you have to maintain state, but the state problems usually come from buggy providers, not Terraform itself. For example, try and use the GitHub provider to create a repo with GitHub Pages enabled from the gh-pages branch. It won't work because the authors didn't respect a fundamental rule of writing a provider: one state-changing API call = one resource. If you don't respect that, you have to do state handling yourself and you're almost certain to have state-related bugs.

Re:Terraform should have remained stateless by raffraffraff on Saturday May 28, 2022 @03:37AM (#31538370)
I'll play devil's advocate (because I understand the need for a state):

What if terraform was able to perform a complete audit of your environment for each provider and say "this is what you've got, mate", then give you options for each resource:

1. Accept (turns it into terraform code)

2. Reject (removes the resource)

Taken further, this idea effectively means that manually creating something like EKS could be a way to auto-compose Terraform code and store it in git.

Pros:

* You have the options of writing code, using the console or using a script or binary to perform a complex setup

* You have 100% code coverage once Terraform compared code with reality and forced you to choose

Cons:

* Auto-generated code is shitty.

* Slooooooow without caching (which is only one of the functions of state)

To make these concepts work without a remote state you pretty much have to run your infrastructure tool as a service, and implement things like:

1. Polling of providers on an interval

2. Alerting when something changes

3. Eventually consistent cache that can be forcibly updated in the UI or during sensitive operations

4. "Infrastructure Composer" UI, like a vastly simplified IaaS console that only shows deployed infrastructure and lets you group infrastructure by service, and let you define code-level modules

5. Show discovered dependencies and ordering, and allow you to explicitly set them (these should be stored as tags on the actual infrastructure components where possible)

6. Use some hot AI to group code-level resources, compare their configurations and generate config data to apply to resources/modules using for_each loops, while respecting service boundaries and offering advice on when to refactor.

Re:Terraform should have remained stateless by jillesvangurp on Saturday May 28, 2022 @05:13AM (#31538838)
Sounds like a plan for a next generation cloud provider. They all have internal state about your account and what's running there; they just don't really expose it in a standardized way. Nor do they provide standardized ways to update that (other than a lot of cli tools and REST APIs).

We keep coming up with these layers of abstractions around stuff that at the bottom is essentially already stateful but just not in a useful way. Doing it right from the ground up might help. Of course, left to the usual suspects, this would just turn into another Frankenstein blackhole of devops time. The usual suspects are billion dollar corporations that thrive on layers of complexity.

Re:Terraform should have remained stateless by InvaderFizz on Saturday May 28, 2022 @10:09AM (#31540457)
Oracle Cloud does this. You can export Terraform for a Compartment[0] (think LDAP OU).

This is how we do our initial terraform for new projects. We build up what is missing by hand, then export the terraform to save time and effort in building our own internal templates. We throw away most of it, but it is very useful for not having to look up how every aspect is named and what options are needed.

0: https://docs.public.oneportal.content.oci.oraclecloud.com/en...

Re:Terraform should have remained stateless by maciekmm on Saturday May 28, 2022 @04:07AM (#31538510)
That approach wouldn't work if your infrastructure is spread between multiple Terraform workspaces. (as is often done).
Re:Terraform should have remained stateless by raffraffraff on Saturday May 28, 2022 @04:46AM (#31538712)
I think that my imagining of the tool is sufficiently different to think that it isn't the same tool at all, and different workspaces wouldn't even be a thing. The composer UI in this case would provide a way to logically separate infra into different code bases but it would still have to know about all of them.

Edit: and actually, without a state there is no concept of a remote state. All codebases talk to the same "service" so circling a bunch of infrastructure and saving it to a specific git project would be easier than connecting to a bunch of remote states and reading specific resource days from them. Even if you're code repo doesn't declare a resource, the omniscient IAC service "knows all"

Re:Terraform should have remained stateless by mountainriver on Saturday May 28, 2022 @01:41PM (#31542204)
TF workspaces are one of the most complained about features at my company, hopefully a new tool would do something better
Re:Terraform should have remained stateless by janosdebugs on Sunday May 29, 2022 @03:09PM (#31551901)
This could partially work, but not for everything. For example, think of the userdata script that provisions the inside of a VM. Often, that needs to be generated based on something, for example certificates that are dynamically created.

I believe, Terraform needs a mode that removes unmanaged resources, but that's quite the big ask and would result in yet another API / SDK change that providers would need to implement.

Re:Terraform should have remained stateless by roguas on Monday May 30, 2022 @02:42PM (#31562183)
Yes - was thinking about exact same thing some time ago.
Re:Terraform should have remained stateless by sockpuppet_12 on Saturday May 28, 2022 @04:56PM (#31543772)
Azure terrafy
Re:Terraform should have remained stateless by Jasp3r on Saturday May 28, 2022 @06:31AM (#31539160)
Why would renaming be impossible to solve by a stateless terraform? In a cloud setup with 3 VMs, and you rename 1 VM from A to B:

Measured cloud setup would have VMs A + X + Y

New cloud setup would specify VMs B + X + Y

You can easily identity X and Y as their names go unchanged, A and B would have similar config/metadata, instead of assuming A would be added and B would be removed you can ask the user if a rename happened.

This is not a new problem to solve by the way, this is how database migration tools handle column renames.

Re:Terraform should have remained stateless by cassianoleal on Saturday May 28, 2022 @07:22AM (#31539435)
In this scenario, you'll have to manage your entire infrastructure from a single Terraform run.

In most places I've been, Terraform is scattered, each run managing its own corner of the infrastructure. In this case, each Terraform run would delete everybody else's infrastructure.

Also, how do you know when to re-create things like random strings or numbers? Or null resources? Presently it's a mix of keepers (requires state) and taint (also requires state).

Re:Terraform should have remained stateless by ImPostingOnHN on Saturday May 28, 2022 @12:21PM (#31541536)
as a user, I don't want to be asked to interactively guide what is supposed to be automated as its entire raison d'etre
Re:Terraform should have remained stateless by GauntletWizard on Saturday May 28, 2022 @12:35AM (#31537639)
Terraform already has to manage those, and yet worse - Changes to the name of objects within the state were entirely manual until 1.0. Changes to objects underneath terraform often break the connection. Terraform doesn't have good tooling built in to find orphan resources, which it would if it were working with stateless objects.

Terraform would be much better without state. Not 10x better, but 2x.

Re:Terraform should have remained stateless by jen20 on Saturday May 28, 2022 @09:55AM (#31540352)
This is absolutely untrue: changes to the logical name were manual until 0.6, CLI driven until recently, and encoded in config as of more recently. Changes to physical resource names is a per-provider concern and is supported wherever the upstream allows it.

Orphan resources only exist as a concept if you manage entire estates in a single configurations, which is simplistic to the extreme.

If I use local_file to emit a certificate authority for an EKS cluster, does that mean every other file on my file system is suddenly orphaned? What if the networking team responsible for VPCs, routing and transit search for orphaned resources in my AWS account - should that include all my instances?

If you think Terraform would be better without state, I’d encourage you to put your money where your mouth is and build it.

Re:Terraform should have remained stateless by rlabrecque on Saturday May 28, 2022 @12:59AM (#31537735)
I suspect there's probably a decent compromise to be made, like some kind of "reconcile" command, that explicitly tries to find orphan resources that reference resources it knows about, and tries to rebuild state.
Re:Terraform should have remained stateless by janosdebugs on Saturday May 28, 2022 @03:13AM (#31538275)
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.

Re:Terraform should have remained stateless by cheradenine_uk on Saturday May 28, 2022 @03:37AM (#31538367)
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 bit. I want to like it, but I do spend inordinate amounts of time faffing around with state files to make them match reality, and I'm not even doing particularly complex stuff. I'm told other tooling (eg: Bicep for Azure) dispenses with state entirely.

Re:Terraform should have remained stateless by giaour on Saturday May 28, 2022 @10:22AM (#31540547)
Bicep and Azure Resource Manager (ARM) are able to dispense with state only because every Azure resource is required to be individually addressable and to report its state in a consistent way, and each resource is expected to support fully idempotent creation operations. The state information is still there, but it's stored on the resource itself.

Bicep/ARM still has issues with resource deletion, though. The default deployment behavior is to ignore resources that aren't described in a deployment template, so if you remove a VM from your template and redeploy, the VM will keep running until you manually delete it. There are a couple ways around this issue, but they all rely on having state external to the resource itself.

Disclaimer: I work on Bicep/ARM and think it's pretty great, but it's not perfect.

Re:Terraform should have remained stateless by janosdebugs on Saturday May 28, 2022 @03:48AM (#31538421)
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 by rbongers on Saturday May 28, 2022 @03:39AM (#31538374)
I don't quite follow - why would it be able to find orphaned resources if it were working with stateless objects?
Re:Terraform should have remained stateless by sly010 on Saturday May 28, 2022 @02:07PM (#31542401)
Presumably because it would already have all the necessary code to find/enumerate existing resources.