Help for the VSCode editor.
-
Navigate to the indicated directory in the Explorer pane.
-
First, create a simple configuration to create a local_file resource within the directory called RemoteState. The resource block should be created inside the main.tf file.
Specification
- Resource Name:
state
- filename:
/root/<variable local-state>
- content: "
This configuration uses <variable local-state> state"
- Use the variable called local-state in the
variables.tf
file which is already created for you and make use of variable interpolation syntax${..}
.
-
In the indicated directory add
main.tf
-
Create the resource as directed
Reveal
resource "local_file" "state" { filename = "/root/${var.local-state}" content = "This configuration uses ${var.local-state} state" }
-
Deploy
cd /root/terraform-projects/RemoteState terraform init terraform plan terraform apply
- Resource Name:
-
Has a state file been created after you run terraform apply?
YES
-
What is the name of the state file created for this configuration?
terraform.tfstate
-
Information only. Explore the minio UI.
-
We have already created an s3 bucket that we will use to store the remote state. From the Minio Browser, identify the name of this bucket.
remote-state
-
Before we add the configuration for the s3 backend, let's first change the local file resource. Change the variable used to remote-state instead of local-state.
- In
main.tf
replace all occurrences of "local-state" with "remote-state".
You can useCTRL+H
to bring up find/replace.
- In
-
Great! Now, let us configure the remote backend with s3. Add a terraform block in a new file called terraform.tf with the following arguments:
Arguments:
- bucket:
remote-state
- key:
terraform.tfstate
- region:
us-east-1
-
Add new file
terraform.tf
-
Configure the backend as requested (documentation)
Reveal
terraform { backend "s3" { key = "terraform.tfstate" region = "us-east-1" bucket = "remote-state" } }
Do not run
terraform init
yet! Since we are making use of minio we also have to add a couple of additional arguments to get this to work!We will do that in the next step. When using the regular s3 service from AWS the above arguments should be sufficient to configure remote state.
- bucket:
-
Information only. Examine the changes we have made to
terraform.tf
-
Try running terraform apply, are you able to do it? If not why?
cd /root/terraform-projects/RemoteState terraform apply
You can see from the error that it requires to be initiailzed. When changing backend configuration this is always required.
-
Run terraform init in our configuration directory now.
terraform init
Answer
yes
to the prompt, to allow terraform to copy the state file to the new backend.The local copy of
terraform.tfstate
may now be deleted.In the minio UI, go to Object Browser and select the bucket. You will see
terraform.tfstate
stored there.