对于已经创建好的AWS资源,如果它不是通过terraform创建的,例如在控制台或使用API创建,如果想将它引入到Terraform来管理,需要使用terraform import
命令,比较不方便
aws2tf
就是为了解决这个繁琐的过程,自动将所有aws资源导入成tf文件
aws cli版本要大于2.9
terraform版本要大于1.3.7
cd ~/environment
git clone https://github.com/aws-samples/aws2tf.git
cd ~/environment/aws2tf
./aws2tf.sh
aws2tf
会进行以下操作:
generated
目录terraform plan
来确认tf文件的正确性在运行过程中可能会看到以下错误:
Found Error: Error: Attribute name required exiting .... (pass for now)
这个错误可以忽略,最终执行结果:
------------------------------------------------------------------------
No changes. Infrastructure is up-to-date.
This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, no
actions need to be performed.
---------------------------------------------------------------------------
aws2tf output files are in generated/tf.903233334942
---------------------------------------------------------------------------
进入到这个generated
目录,所有的tf文件已经为你生成好了:
cd ~/environment/aws2tf/generated/tf.*
ls
执行terraform plan
来验证是否跟线上环境一致:
terraform plan
假如有两个帐号test
和prod
,通过这种方式,可以在terraform中切换帐号,将test
下所有AWS资源一键部署到prod
帐号上面,所有过程不用写任何代码!