Terraform with existing production resources
Asked Answered
R

3

8

I’m new to Terraform, I’d like to eventually have a Terraform template of my production resources so I can easily deploy to multi-regions and for disaster recovery. How can I do this if my resources on AWS (EC2, S3,VPC, etc) are already deployed?

Receptive answered 27/12, 2017 at 18:27 Comment(0)
T
3

You can use terraform import feature for this. Here is a link to the documentation : https://www.terraform.io/docs/import/index.html

However it's not capable of generating configurations for you. You have to build up the configurations and import the state of resources.

The current implementation of Terraform import can only import resources into the state. It does not generate configuration. A future version of Terraform will also generate configuration.

You can do it incrementally, start writing configurations for resources one by one.

Ex:

  1. Write configurations for VPC.
  2. Import VPC state
  3. Write configs for EC2 instance.
  4. Import EC2 state

After successfully importing a resource/s. You can use terraform plan command to ensure the state is correctly matching.

Remember to make use of the terraform modules (https://www.terraform.io/docs/modules/create.html) to better structure the code so that you could reuse them with other regions and environments.

Here's an article which describes a way to use terraform with multi-region deployments. https://medium.com/@ctindel/multi-region-aws-app-deployments-with-terraform-modules-859c9ecb64f4

Trieste answered 28/12, 2017 at 2:54 Comment(0)
A
3

Google has an opensource tool called terraformer for reverse Terraforming an existing AWS stack into tf and tfstate files.

They also support GCP (obviously), K8s, Openstack and other platforms.

AWS example usage:

terraformer import aws --resources=vpc,subnet --connect=true --regions=eu-west-1 --profile=prod

terraformer import aws --resources=vpc,subnet --filter=aws_vpc=vpc_id1:vpc_id2:vpc_id3 --regions=eu-west-1
Aurar answered 9/7, 2019 at 2:27 Comment(3)
Does this still work? Is this still a vetted tool?Kosciusko
Is this tool still good to use??Kosciusko
@小红书 it's not an official google product as stated in the project readme, however it has had some recent commits. Might be best to check out the issues to see how stable it is: github.com/GoogleCloudPlatform/terraformer/issuesAurar
L
1

I think you should try considering "Terraforming" in this case. It will export your current aws infrastructure in terraform style.

Ref: http://terraforming.dtan4.net/

Github: https://github.com/dtan4/terraforming

You may also consider using segment stack of production-ready modules of terraform, which you can use as the way you want to.

https://github.com/segmentio/stack

Laywoman answered 28/12, 2017 at 16:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.