Multiple stacks with remote backend

Hi, I have a setup with multiple stacks using GCS as the state backend. It looks something like this:

class MyStack extends TerraformStack {
    constructor(scope: Construct, name: string) {
        super(scope, name);

        new GcsBackend(this, {
            bucket: "myBucket",
        });
    }
    // stack code ...
}

class MyOtherStack extends TerraformStack {
    constructor(scope: Construct, name: string) {
        super(scope, name);

        new GcsBackend(this, {
            bucket: "myBucket",
        });
    }
    // stack code ...
}

Now, this will not work because both state files are stored in “myBucket/default.tfstate” and they will overwrite each other. I can fix this by setting a prefix which works fine:

new GcsBackend(this, {
    bucket: "myBucket",
    prefix: `stacks/${name}`
});

But I was wondering, am I missing something here or is there really no way to change the name of “default.tfstate”? The documentation says it should be the name of the stack, but that only seems to work if you use the Terraform Cloud backend.

If this is intended, maybe it should be in the documentation? Since it’s not really obvious the first time. The state just gets overwritten without any warning, which causes quite a bit of chaos.

I haven’t used the GCS backend myself, but it sounds as though the state file is named after the Terraform workspace used. (There’s a brief mention here: Backend Type: gcs | Terraform by HashiCorp). It is possible to change the workspace used; however, setting the prefix is a simpler approach.

Each state backend is a little bit different and requires some knowledge about how they work. Feel free to open an issue/PR with any suggested documentation improvements.

Thanks for the clarification, I’ll stick with the prefix then!