简体   繁体   English

使用现有生产资源进行Terraform

[英]Terraform with existing production resources

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. 我是Terraform的新手,我想最终拥有一个Terraform模板来生成我的生产资源,这样我就可以轻松部署到多个区域并进行灾难恢复。 How can I do this if my resources on AWS (EC2, S3,VPC, etc) are already deployed? 如果已部署AWS(EC2,S3,VPC等)上的资源,我该怎么做?

You can use terraform import feature for this. 您可以使用terraform import功能。 Here is a link to the documentation : https://www.terraform.io/docs/import/index.html 以下是文档的链接: 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. Terraform导入的当前实现只能将资源导入该状态。 It does not generate configuration. 它不会生成配置。 A future version of Terraform will also generate configuration. Terraform的未来版本也将生成配置。

You can do it incrementally, start writing configurations for resources one by one. 您可以逐步执行此操作,逐个开始编写资源配置。

Ex: 例如:

  1. Write configurations for VPC. 写入VPC的配置。
  2. Import VPC state 导入VPC状态
  3. Write configs for EC2 instance. 为EC2实例编写配置。
  4. Import EC2 state 导入EC2状态

After successfully importing a resource/s. 成功导入资源后。 You can use terraform plan command to ensure the state is correctly matching. 您可以使用terraform plan命令确保状态正确匹配。

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. 请记住使用terraform模块( https://www.terraform.io/docs/modules/create.html )来更好地构建代码,以便您可以将它们重用于其他区域和环境。

Here's an article which describes a way to use terraform with multi-region deployments. 这篇文章描述了一种使用多区域部署的terraform的方法。 https://medium.com/@ctindel/multi-region-aws-app-deployments-with-terraform-modules-859c9ecb64f4 https://medium.com/@ctindel/multi-region-aws-app-deployments-with-terraform-modules-859c9ecb64f4

I think you should try considering "Terraforming" in this case. 我认为在这种情况下你应该尝试考虑“Terraforming”。 It will export your current aws infrastructure in terraform style. 它将以terraform风格导出您当前的aws基础设施。

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

Github: https://github.com/dtan4/terraforming 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. 您也可以考虑使用terraform生产就绪模块的分段堆栈,您可以按照自己的方式使用它。

https://github.com/segmentio/stack https://github.com/segmentio/stack

Google has an opensource tool called terraformer for reverse Terraforming an existing AWS stack into tf and tfstate files. Google有一个名为terraformer的开源工具,用于反向将现有AWS堆栈转换为tftfstate文件。

They also support GCP (obviously), K8s, Openstack and other platforms. 他们还支持GCP(显然),K8s,Openstack和其他平台。

AWS example usage: AWS示例用法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM