简体   繁体   中英

How can I sync Terraform state with existing resources?

I have a Terraform script that sets up 30 resources in all, including roles, policies and ECS infrastructure.

The state file was destroyed when switching git branches - I should've backed it up, but now I do not have a state file referencing the already existing resources.

I can't use terraform apply because the names already exist, and I can't clear the existing resources with terraform destroy because the state file now thinks the resources don't exist.

Is there a way I could sync with existing resources? I am aware of terraform import but to my knowledge that only works with one resource at a time.

One of the functions of Terraform "state" is to allow Terraform to recognize which subset of objects in your account it's responsible for managing, and so unfortunately there is no built-in mechanism in Terraform to automatically recover that information: terraform import is the way to explicitly tell Terraform that it should begin managing an existing object.

However, there are some external tools that make use of additional knowledge about particular cloud platforms in order to provide a higher-level interface to importing. One example is terraformer , which allows you to import a set of objects matching a query, as long as the objects you want to import are in the subset of types it supports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM