Skip to content

Latest commit

 

History

History
131 lines (87 loc) · 3.96 KB

File metadata and controls

131 lines (87 loc) · 3.96 KB

Lab: Remote State

Help for the VSCode editor.

  1. Navigate to the indicated directory in the Explorer pane.

  2. 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 ${..}.
    1. In the indicated directory add main.tf

    2. Create the resource as directed

      Reveal
      resource "local_file" "state" {
          filename = "/root/${var.local-state}"
          content  = "This configuration uses ${var.local-state} state"
      }
      
    3. Deploy

      cd /root/terraform-projects/RemoteState
      terraform init
      terraform plan
      terraform apply
  3. Has a state file been created after you run terraform apply?

    YES

  4. What is the name of the state file created for this configuration?

    terraform.tfstate

  5. Information only. Explore the minio UI.

  6. 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

  7. 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.
    1. In main.tf replace all occurrences of "local-state" with "remote-state".
      You can use CTRL+H to bring up find/replace.
  8. 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
    1. Add new file terraform.tf

    2. 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.

  9. Information only. Examine the changes we have made to terraform.tf

  10. 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.

  11. 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.