terraformaws-gluerecoverytfstate

Possibility to re-generate tfstate file


I just recently started learning Terraform and created a terraform file (main.tf) to generate AWS Glue catalog database and some Glue catalog tables added to it. I ran the needed commands (terraform init, terraform plan, terraform apply) and resources were generated to AWS Glue environment.

Now I'm aware that I should keep tfstate file saved to keep up with the changes but what if I deleted tfstate file? Is there a possibility to re-generate tfstate file according to what resources I already have in AWS Glue environment? Can terraform 'sniff' that information somehow?

I could always start over by removing those resources manually on AWS Glue environment and then re-run the commands but is there a recovery method? Now when I already have those resources and I run the file (without tfstate file), it plans to generate all those resources, and returns an error:

aws_glue_catalog_database.testdb: Creating...
Error: Error creating Catalog Database: AlreadyExistsException: Database already exists.

Any help would be appreciated!


Solution

  • You might be able to use "terraform import". If you have existing infrastructure terraform import can read the config from the resources back into the tfstate file, as long as there is a definition in the config. Here's what someone else did: https://tryingthings.wordpress.com/2021/03/31/lessons-learned-after-losing-the-terraform-state-file/