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

Feature request: Import resource into .tfstate AND create a HCL resource definition in .tf file #15608

Open
Nowaker opened this issue Jul 21, 2017 · 15 comments
Labels
enhancement import Importing resources

Comments

@Nowaker
Copy link

Nowaker commented Jul 21, 2017

terraform import is used for importing a resource into Terraform state, as per tf import --help:

In the current state of Terraform import, the resource is only imported into your state file. Once it is imported, you must manually write configuration for the new resource or Terraform will mark it for destruction. Future versions of Terraform will expand the functionality of Terraform import.

I'd like to request an extension of import functionality. Two things would happen:

  1. Update *.tfstate file. (Already implemented)
  2. Create a new *.tf file with a resource definition in HCL. (Feature request)

As far as I can see, (2) is trivial. All data that a HCL resource definition requires is already in the state - inside primary.attributes hash. Everything is super clear - 1 to 1. Example:

image

And no changes detected:

% tf plan -target=datadog_monitor.test
datadog_monitor.test: Refreshing state... (ID: 2448088)
No changes. Infrastructure is up-to-date.
@davidbegin
Copy link

This would be a very useful feature to be a part of terraform.

To do something like currently, I use import and the . https://github.com/dtan4/terraforming to kind of achieve something similar.

However, not all resources are supported as part of terraforming, and you have to sift through all the resources of a particular type and pull out the one you want/

@apparentlymart
Copy link
Contributor

Thanks for requesting this, @Nowaker.

As you can see in the docs, this is definitely something we intend to implement. You're right that the basic mechanism here is rather straightforward, but there are some details that need to be addressed such as representation of dependencies (so e.g. Terraform can be told that an imported AWS VPC subnet depends on its associated AWS VPC, which must currently be hand-written in config) and some other user experience improvements around the specification of resources that don't have a "natural" identifier to use, such as aws_route53_record.

We've been looking at all this under the heading "import improvements", and do intend to make these improvements but want to take the time to make sure it's designed and implemented robustly. We'll have more to share on this once we've completed some design work and prototyping.

@Nowaker
Copy link
Author

Nowaker commented Jul 21, 2017

Hey @apparentlymart - thanks for your reply. Absolutely - things get challenging when you need to represent the dependencies. I'll look into this as well. I guess for time being some Ruby scripting for mass imports and HCL generation would do wonders - I'll post my results here. Thanks!

@Nowaker
Copy link
Author

Nowaker commented Jul 22, 2017

Here's a naive script that imports a resource and generates an HCL file: https://gitlab.com/Nowaker/terraform-import-as-hcl. It's not perfect but still better than nothing.

@tompave
Copy link

tompave commented Jul 26, 2017

I would add a +1 to this, but there are already countless issues open on the same subject and apparently it's been in discussion since 2015.

At the moment I'm struggling with importing an AWS resource: the default route table for a VPC. That resource has an AWS id, and I can't find any documentation on how to write HCL for it. I can try to replicate its attributes, but then the original will be deleted and a new one will be created.

@gdxhsw
Copy link

gdxhsw commented Aug 7, 2017

+1 for this feature, would be very useful!

@lisfo4ka
Copy link

+1 to have this feature

@ravibhure
Copy link

+1 for this feature

2 similar comments
@martinbjorgan
Copy link

+1 for this feature

@digitalcrafted
Copy link

+1 for this feature

@mshakirov
Copy link

+1

1 similar comment
@premkumar-masilamani
Copy link

+1

@arunvnera
Copy link

+1

@nmaludy
Copy link

nmaludy commented Apr 12, 2018

+1 would also like to see this for other providers like vSphere

@apparentlymart
Copy link
Contributor

I'm going to lock this issue for now because it's attracting voting noise, which is disruptive for the people watching the issue. We'll unlock it again when we're ready to start working on this. Thanks for the interest, everyone!

@hashicorp hashicorp locked and limited conversation to collaborators Apr 16, 2018
@pkolyvas pkolyvas added import Importing resources and removed core labels Apr 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement import Importing resources
Projects
None yet
Development

No branches or pull requests