Is there a better solution to import aws resources to terraform?

452 views
Skip to first unread message

changd...@gmail.com

unread,
Feb 28, 2019, 1:11:55 AM2/28/19
to Terraform
We have several k8s stacks which created by kubespray use AWS, now we want to import them to terraform. I tried both official `terraform import` and `terraforming`, seems they both have issues. For `terraform import`, it not easy to import so many resources, it only can use to import resource one by one, and before import we must to create an empty tf file(it's really not good to use). For `terraforming`, it is easier to use than `terraform import`, but still have issues,. Firstly,  `terraforming` export the aws resources will use the tag as the name of resource, but sometimes we may use same tag for several ec2 instances as they have the same purpose. This will be lead to 'resource repeated multiple times' error. Secondly, looks like some of the format of `terraforming` exported can not be parsed by `terraform` (the following image).

So I want to know if there is a better solution to import aws resources to terraform? Or, if there is another official recommended third-part tools to perform import? 
By use the official `terraform import`, it only can generate a tfstate file, it looks like no difference with create a new resource by terraform directly. Which is the official recommend between import exists resources and create them directly?

lots_parse_errors.jpg




Tim Visher

unread,
Feb 28, 2019, 8:44:30 AM2/28/19
to terrafo...@googlegroups.com
On Thu, Feb 28, 2019 at 1:11 AM <changd...@gmail.com> wrote:
We have several k8s stacks which created by kubespray use AWS, now we want to import them to terraform. I tried both official `terraform import` and `terraforming`, seems they both have issues. For `terraform import`, it not easy to import so many resources, it only can use to import resource one by one, and before import we must to create an empty tf file(it's really not good to use).

I wonder if you could be more specific. I've had a lot of success using basic shell scripting techniques with the aws cli and jq, for instance. I've never had to create an empty tf file before to do it (kind of the point of `terraform import` is that it doesn't try to touch your tf files at all) but maybe I'm missing something about your use case?

changd...@gmail.com

unread,
Mar 4, 2019, 9:27:36 PM3/4/19
to Terraform
Thank you for your reply, I means when I use `terraform import`, I must create the import tf file first. When I use `terraforming <resource>`, it was very easy to export aws resource to tf files and tfstate files, but these tf files and tfstate files seems can not be used by `terrform` directly, it has some parse issues(the attachment screen shot).

Tim Visher

unread,
Mar 5, 2019, 9:25:12 AM3/5/19
to terrafo...@googlegroups.com
On Mon, Mar 4, 2019 at 9:27 PM <changd...@gmail.com> wrote:
Thank you for your reply, I means when I use `terraform import`, I must create the import tf file first. When I use `terraforming <resource>`, it was very easy to export aws resource to tf files and tfstate files, but these tf files and tfstate files seems can not be used by `terrform` directly, it has some parse issues(the attachment screen shot).

I think you forgot the screenshot.

I'm still not sure I'm understanding you. At the risk of repeating the docs, `terraform import <resource_address> <resource_id>` should import the resource `<resource_id>` into the state file at `<resource_address>` but not create or require any tf file representation. If you then run a plan afterwards terraform will helpfully tell you that it's going to delete the resource you just imported because there's no configuration for it.

_Then_ you need to create the configuration by hand. I generally do this until the resource stops claiming to be modified, iterating on it a few properties at a time.

Modules make this slightly more complicated as you either need to move the resource from the root into the module like `terraform state mv <resource_address> module.<module_name>.<resource_address>` or maybe `terraform import` has gotten smart enough that it can import directly into modules these days.

changd...@gmail.com

unread,
Mar 6, 2019, 1:28:02 AM3/6/19
to Terraform

Snipaste_2019-03-06_13-49-56.png


It may be that my bad English expression makes me unable to clearly describe my problem. I think I really know how to use `terraform import` to import the resources from aws, but just like the following screenshot from the docs, I really don't think this is a good way to import resources, it is not easy and it is not powerful. I want to know if there is a easier way to import resources without any manual work until we want modify it -- iterating on the configuration is painful. If we must do this to  import the resources, seems we create the resources by terraform directly is better?

⁞ Fernando Miguel

unread,
Mar 6, 2019, 1:49:30 AM3/6/19
to terrafo...@googlegroups.com
Have you looked at terraforming project? 

--
Fernando Miguel

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/2786052f-1bef-42ae-8cdc-b738fa37088e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

changd...@gmail.com

unread,
Mar 6, 2019, 8:27:02 PM3/6/19
to Terraform

lots_parse_errors (2).png

Yes! I tried it, but seems sometimes the tf files and tfstate files exported by 'terraforming' can not be correct parsed by 'terrform'.

⁞ Fernando Miguel

unread,
Mar 7, 2019, 1:59:13 AM3/7/19
to terrafo...@googlegroups.com
Then you should probably file bugs for those cases. 

I personally never had that happen to me. 

--
Fernando Miguel

Tim Visher

unread,
Mar 11, 2019, 10:02:47 AM3/11/19
to terrafo...@googlegroups.com
On Wed, Mar 6, 2019 at 1:28 AM <changd...@gmail.com> wrote:

Snipaste_2019-03-06_13-49-56.png

Ah I hadn't seen that in the docs before. It's actually not necessary AFAICT but obviously needs to be done eventually. 

It may be that my bad English expression makes me unable to clearly describe my problem. I think I really know how to use `terraform import` to import the resources from aws, but just like the following screenshot from the docs, I really don't think this is a good way to import resources, it is not easy and it is not powerful. I want to know if there is a easier way to import resources without any manual work until we want modify it -- iterating on the configuration is painful. If we must do this to  import the resources, seems we create the resources by terraform directly is better?

I think I follow now. You're looking for a way to have all current state automatically persisted into a tf file during the import. I've never needed to do that personally so I guess I'll bow out now. :)

Jd Daniel

unread,
Mar 11, 2019, 11:57:31 AM3/11/19
to terrafo...@googlegroups.com
There's also Terraforming, https://github.com/dtan4/terraforming

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages