Reputation: 101
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!
Upvotes: 2
Views: 1956
Reputation: 101
Thanks everyone for your help. Inspired by your suggestions about using import command, I ended up writing batch file for the Terraform directory where I import each resource one by one.
Script is as follows:
cmd /k "terraform import aws_glue_catalog_database.testdb <AWS-ACCOUNT-ID>:testdb & terraform import aws_glue_catalog_table.testtable <AWS-ACCOUNT-ID>:testdb:testtable"
It's working for this use case fine.
Upvotes: 1
Reputation: 1578
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/
Upvotes: 1
Reputation: 771
I found this link useful in your case.
https://stackoverflow.com/questions/54122890/terraform-fails-because-tfstate-s3-backend-is-lost
I think you can start importing the resources which will create the state file again.
One of the way I have not tried it yet, you can give it a try.
The best practise would be to use the S3 (with versioning) backend along with DynamoDB for the safety net of the tf state files.
Upvotes: 2