Use existing IDs in other

Hi all!

In an OpenStack environment I have some VMs and security groups created by a file. In a second step a want to run another which adds some rules to the SGs. The rules are based on IP addresses that are only available after running the first But to create the security rules I have to refer to the already existing security group, which is not defined in my second

I managed to fetch the IDs from the terraform.tfstate file using jq, but I fail to import them. Command is:

terraform import id-from-tfstate

But when I run this I get “Before importing this resource, please create its configuration in the root module”.

When I add the mentioned resource to the configuration it says “Terraform is already managing a remote object for To import to this address you must first remove the existing object from the state”.

How can I use existing IDs in other main.tfs?

Thanks a lot,

It sounds like you are wanting to use data sources rather than resources in the second Terraform stack. Or you could use the remote state data source with outputs from the first stack.

1 Like

That sounds like a solution to the problem, but I´m not too sure about how to use that in my case.

Let´s say I have two folders “Env-A” and “Env-B” - in each folder is a and Env-A is containing the declaration of a resource “Res-A” which I want to refer to in “Env-B”. How would that look like?

Edit: Guess I found it. Will test that out and post here if it works. Thanks for givingme that hint, stuart-c. :slight_smile: