Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to import in bulk #22219

Closed
tmccombs opened this issue Jul 26, 2019 · 12 comments
Closed

Ability to import in bulk #22219

tmccombs opened this issue Jul 26, 2019 · 12 comments
Labels
cli enhancement import Importing resources providers/protocol Potentially affecting the Providers Protocol and SDKs

Comments

@tmccombs
Copy link
Contributor

Current Terraform Version

$ terraform -v   
Terraform v0.12.6-dev

Use-cases

While starting to manage infrastructure with terraform, I have hundreds of resources that I need to import. I want to be able to import all resources from a file that I have generated (in my case using the AWS API).

Attempted Solutions

I've gotten something that works by generating a script that has hundreds of calls to the terraform import command. However, it is very slow to run, because every single call has to acquire the lock, fetch the state (from S3), perform the import, store the state, and release the lock. It would be much more efficient if acquiring the lock, fetching the state, storing the state, and releasing the lock was done just once for all resources.

Proposal

Add on option to the terraform import command which accepts a file. The file should have a resource address and resource id seperated by a space on each line, and it should then acquire the lock and fetch the state, then import all resources in that file, then store the state and release the lock.

tmccombs added a commit to tmccombs/terraform that referenced this issue Jul 28, 2019
tmccombs added a commit to tmccombs/terraform that referenced this issue Sep 24, 2019
@rupa
Copy link

rupa commented Oct 1, 2019

Want to chime in that we (at NS1) are quite interested in the outcome of this issue/PR. We have some bulk import use cases. It can take quite some time to get 50k+ resources imported and seems like this could help a lot!

@ashishbista
Copy link

I'd like to import all resources for certain providers. For example, I'm using mysql provider to create users and grants. I am creating hundreds of mysql resources. Most of those resources are already created using some other process. I'd be nice to have an ability to import all existing mysql users and grants to a terraform state file. Without this, when I apply terraform, it complains about the existing resources. And, there is no way to import all existing mysql resources in one shot.

@yehudacohen
Copy link

Also have a use case that this would help with tremendously. When trying to help customers manage sets of existing infrastructure using terraform, the ability to only import one resource at a time feels crippling.

jarondl added a commit to jarondl/terraform that referenced this issue Jan 22, 2020
Allow to import many different terraform resources in one command, by
supplying a list of addr,id pairs.

Basically, importing hundreds of resources takes a few hours before this
cl, and a few mintues after it.

To make implementation somewhat easier I have taken out a chunk of the
mapping from args to targets into a smaller function, but the rest stays
almost the same. Some error messages change a bit.

Fixes hashicorp#22219
@yosefvb
Copy link

yosefvb commented Jun 3, 2020

Is there anything I can do to help merge? This solution would be very helpful! @pkolyvas @teamterraform

vito added a commit to concourse/governance that referenced this issue Mar 10, 2021
* does not store terraform state anywhere; just imports it on every run
  * would be nice if there was a bulk import feature! the internal API
    seems to support it. hashicorp/terraform#22219
* doesn't run apply yet - pushing this for a dry run first.

part of #4

Signed-off-by: Alex Suraci <suraci.alex@gmail.com>
@cmaster11
Copy link

cmaster11 commented Jun 2, 2021

This would indeed be a very useful feature :)

For now, e.g. for Route53 entries, https://mrkaran.dev/posts/terraform-route53-import/

@varmax2511
Copy link

varmax2511 commented Jun 18, 2021

Importing multiple resources is a highly sought out feature. The implemenation should support parallelism to improve efficiency of import with multiple resources

@zeako
Copy link

zeako commented Jul 31, 2021

for anyone still interested, Google recently open sourced this https://github.com/GoogleCloudPlatform/terraformer

tmccombs added a commit to tmccombs/terraform that referenced this issue Oct 28, 2021
@bflad bflad added import Importing resources providers/protocol Potentially affecting the Providers Protocol and SDKs labels Dec 1, 2021
tmccombs added a commit to tmccombs/terraform that referenced this issue Mar 13, 2022
@tmccombs
Copy link
Contributor Author

Another possible solution for this could be to allow defining imports in the terraform code itself, similar to the new moved block. This could either be a new import block, or a meta attribute (such as lifecycle.import) where you can specify the import id of a resource. And of course if the resource already exists, then the import directive is ignored.

@paulca99
Copy link

paulca99 commented Feb 21, 2023

I've read a few of these bulk-import tickets now, I'm in a bit of a pickle defining our DR procedures. We've over 100 RDS tables , defined in terraform , full of data , backed up in AWS Backup Vault. If some maniac decides to delete all these tables we'd initiate the restore from AWS Backup, which would create 100 new tables, but then would we have to import each back into the tf state one by one ? The state is quite large and single import takes over a minute, So that'd be at least a couple of hours before we could actually terraform apply to create the rest of the missing infrastructure. I suppose we could manually hack the state file to switch these tables state to point to the new ones, but that smells bad to me.

@kmoe
Copy link
Member

kmoe commented Jun 27, 2023

Bulk import is possible in Terraform v1.5+ with the use of import blocks: https://developer.hashicorp.com/terraform/language/import.

@kmoe kmoe closed this as completed Jun 27, 2023
@acdha
Copy link

acdha commented Jun 27, 2023

@kmoe I gave that a try last week and it worked really well. Thanks for all of the work getting that over the line.

Copy link
Contributor

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cli enhancement import Importing resources providers/protocol Potentially affecting the Providers Protocol and SDKs
Projects
None yet