-
Notifications
You must be signed in to change notification settings - Fork 458
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
Support terraform import
via the cdk cli
#1134
Comments
This is what I'm doing in my own project for now: Created this script at
And then made some scripts in my This one is the base for the rest.
And here's some example shorthand utility scripts (useful if you have long commands that you run often:
Then you can run these like Extending this all to the I haven't had to import anything yet, but I think you could do: No warranty / guarantees, but hope this helps someone 🎉 |
That would be very useful to have a way to generate HCL configuration from existing configuration such as https://github.com/cycloidio/terracognita seems to provide for different vendors. |
With the |
Having recently come into a new (AWS) environment where I have to import 100s of resources, I decided to build a solution that works for me using the CDK for Terraform. This is pretty much inspired by how Pulumi allows for resource imports from code, so I'm following that model pretty closely. I have no idea if this approach will be amendable to being included into The idea I had is simple: for many of these resources, I know the "aws_security_group": {
"some_logical_id": {
"//": {
"metadata": {
"path": "some_path",
"uniqueId": "some_logical_id"
},
"importId": "sg-0123345123345"
},
"name": "my-sg"
}, Once #1543 is merged, it should enable native support for users to add this The code that inserts this can look something like this: self.my_sg = MySecurityGroup(
self, 'some_logical_id',
name="my-sg",
import_="sg-0123345123345",
) Once the
Once that script generates a After I ensure the state is in a valid state, I remove the Hopefully that workflow makes sense, if there are any questions, feel free to ask. Notes:
|
@rirze Thanks for sharing your workflow. I could see having some like Might be interesting to have some sort of |
@jsteinich Glad to see some feedback on this-- I think providing a basic level of "use this resource_id when using If you want to move forward with this idea, do you see a particular way of exposing this to the user? I've seen @ansgarm detail an approach using an |
I see that approach as more of just a friendlier way for a user to add additional functionality to a resource (in this case adding some import metadata). Building directly into the api we could add |
Yes, that was exactly I was after there. It was my proposal as an alternative to an "add any metadata" method. However, in case of the import use-case we're discussing here, I'd go with an API as proposed by @jsteinich. Does anyone of you know whether there is any documentation on the id that one has to supply to |
It's my understanding that this is left up to the provider, so this heavily depends (like you noted) on the provider's strategy and particular resource import implementation. Take a look at |
It's not something that's directly available, but the idea of looking at the docs during generation has been brought up before. Might also be feasible to get it included in the upstream Terraform schema. |
The following script was able to import the existing log group to the CDKTF state.
|
The AWS CDK has a surprisingly decent flow that could be emulated here: https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk#cdk-import TL;DR:
|
It's a very manual process imo. From what I remember, the CLI prompts for the ARN (AWS specific in this case) one by one. Might be a good initial step but I don't think this should be the long term solution. |
Finally, I found this manual workaround to import existing resources.
You should see |
We also recently published some docs around refactoring. If you move constructs between stacks you also need to import, maybe it is helpful to anyone reading this: https://developer.hashicorp.com/terraform/cdktf/examples-and-guides/refactoring |
Thanks for that message, works like a charm. Make sure you use the "DB instance ID" for on the DB you want to import, not the ARN. I guess it works the same for other types of ressources. Makes life easier and prevent the string too long errors |
terraform import azurerm_kubernetes_cluster_extension.flux /subscriptions/58b12744-8fce-4491-8a8a-bff0b85fa0e8/resourceGroups/juice-shop-workshop/providers/Microsoft.ContainerService/managedClusters/juice-shop-workshop/providers/Microsoft.KubernetesConfiguration/extensions/flux hashicorp/terraform-cdk#1134 (comment)
The workaround from @keidarcy works indeed nicely. We are using the CDK with C# and I wrote some code which automates this for us until there is an official feature.
Of course there can be errors in cases where you mix actually new items and items to import in one run but we can deal with that. The biggest challenge was building the import IDs which depend on the provider and resource you are importing and you need to align this with the docs. As we are doing a larger migration it was worth automating this for us. |
Hi everyone. I created a CLI for CDKTF import using the solution provided from this comment of @keidarcy and @Danielku15 . This is still on development but this is better than doing this manually. |
Hey @mackignacio, just FYI, we merged #2972 now that includes import functionality. We are soon releasing a new cdktf version, so we hope you'll find it useful (or that your import needs are already solved by your CLI :) ) |
Nice Thank you. For this moment I will just used mine until you release the new version. |
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've 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. |
Community Note
Description
Support importing existing resources into a stack's state file.
References
https://www.terraform.io/docs/cli/import/index.html
The text was updated successfully, but these errors were encountered: