Giter Site home page Giter Site logo

Comments (7)

osterman avatar osterman commented on May 19, 2024

@schollii you're right, this should be documented somewhere. Right now it's not.

Please have a look here for some of the scripts we've used in the past: https://github.com/cloudposse/terraform-root-modules/tree/master/aws/tfstate-backend/scripts

from terraform-aws-tfstate-backend.

schollii avatar schollii commented on May 19, 2024

Thanks that's useful to know. It could be useful to mention the scripts in the docs, but also the procedure I mentioned, in case the scripts are not adequate for whatever reason.

from terraform-aws-tfstate-backend.

jmcgeheeiv avatar jmcgeheeiv commented on May 19, 2024

@schollii @osterman.
I found and documented a smooth way to apply and destroy using this module. See PR #46.

from terraform-aws-tfstate-backend.

schollii avatar schollii commented on May 19, 2024

So the trick of using the module's support for automatically generating a backend.tf file works really nicely and simplifies the destruction. Here is what I use.

Creation

# main.tf
module "terraform_state_backend" {
  source                             = "git::https://github.com/cloudposse/terraform-aws-tfstate-backend.git?ref=..."
  ...
  terraform_backend_config_file_name = "backend.tf"
  terraform_backend_config_file_path = "."
  force_destroy                      = false
}

Then this is needed only once:

  1. terraform init: downloads modules and providers
  2. terraform apply -auto-approve: creates the backend.tf and the s3 bucket etc but state is stored locally
  3. terraform init -force-copy: moves the state to s3 bucket create

Since this is only once I put all these in a script init.sh that gets run once and that's it. It seems idempotent but I have not pushed testing of that very much.

Once the above is done you can define more resources and terraform apply as needed.

Deletion

  1. in main.tf edit the module.terraform_state_backend to have force_destroy true and terraform_backend_config_file_path to ""
  2. terraform apply -target module.terraform_state_backend -auto-approve: activate the above mods (s3 becomes destroyable, remove the backend.tf file)
  3. terraform init -force-copy: move the state to local host

I put all the above in a destroy.sh script that also check before step 3 that step 2 has been done (actually I could surely have the scrip do step 2 automatically but this way it is a more "conscious" decision by the user to destroy - running it by mistake will fail). Additionally the script does terraform init before step 2 because there are some failure conditions that need it.

from terraform-aws-tfstate-backend.

jmcgeheeiv avatar jmcgeheeiv commented on May 19, 2024

Excellent. Thank you. Could you put your scripts in a comment for reference? Then I'll rewrite my doc.

from terraform-aws-tfstate-backend.

jmcgeheeiv avatar jmcgeheeiv commented on May 19, 2024

I think this cam be closed. Nice working with you, @schollii.

from terraform-aws-tfstate-backend.

schollii avatar schollii commented on May 19, 2024

likewise @jmcgeheeiv thanks for contributing the solution in #46

from terraform-aws-tfstate-backend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.