...

/

Storing the State in a Remote Backend

Storing the State in a Remote Backend

Learn how to create a storage bucket to hold and save the state of our cluster.

Creating a Google storage bucket

Terraform maintains its internal information about the current state. That allows it to deduce what needs to be done and to converge the actual into the desired state defined in *.tf files. Currently, that state is stored locally in the terraform.tfstate file. For now, there shouldn’t be anything exciting in it. We can see this file in the code playground below by running the cat terraform.tfstate. But for reference, here are the contents of terraform.tfstate.

Press + to interact
{
"version": 4,
"terraform_version": "0.12.12",
"serial": 1,
"lineage": "c818a17f-...",
"outputs": {},
"resources": []
}

The field that really matters is resources. It’s empty because we didn’t define any. We’ll do that soon, but we’re not going to create anything related to our GKE cluster. At least not right away. What we need right now is a storage bucket.

Keeping Terraform’s state local is a bad idea. If it’s on a laptop, we can’t allow others to modify the state of our resources. We’d need to send them the terraform.tfstate file through email, keep it on a network drive, or implement some other similar solution. That is impractical.

We might be tempted to store it in Git, but that wouldn’t be secure. Instead, we’ll tell Terraform to keep the state in a Google bucket. Since we’re trying to define infrastructure as code, we won’t do that by executing a shell command, nor will we go to the GCP console. We’ll tell Terraform to create the bucket. It will be the first resource Terraform manages.

But, before we proceed, we need to confirm that billing for storage is enabled. Otherwise, Google won’t allow us to create one.

Note: Follow the instructions in this lesson to fulfill the billing requirement.

Viewing storage.tf

...