【问题标题】:Import existing Azure resource groups to terraform state将现有 Azure 资源组导入 terraform 状态
【发布时间】:2021-05-25 01:05:21
【问题描述】:

我们正在从通过 ARM 进行 Azure 部署迁移到跨开发团队使用 Terraform。我们正在寻找将当前部署迁移到 TF 状态的方法,因为在许多订阅中存在大量资源组,每个订阅都有许多资源。大多数现有部署使用具有大量依赖项的单片 ARM 模板(在某些情况下为数千行),因此对于某些团队而言,目前还不能选择从头开始部署。

尝试使用 tf import 1-by-1 是有问题的,因为这些资源组中的每一个都有多个环境,因此要移动的东西数量很多,这将是一项艰巨的任务。查找每个 Azure 资源 ID 也很困难。

要以更直接和类似批处理的方式进行导入,我们查看了terraformer,但它也有一些缺点。并非所有 Azure 资源都受支持,虽然我们可以贡献代码以添加它们,但似乎存在更大的问题。 Terraformer 想要查询订阅的所有已部署资源,然后按照您要求它执行的操作进行过滤。部署的资源数量似乎使程序不堪重负,并开始出现错误。我们也看到了Az2TF,但这停止了使用 azurerm 提供程序 1.44 的开发。

有没有人进行过这样的大型 tf 迁移,可以为我们指明另一个方向?

【问题讨论】:

    标签: azure terraform azure-resource-manager


    【解决方案1】:

    您可以存储到多个 tfstates
    一种环境,一种状态

    ### Import - ENV1 Prod
    terraform import -var-file="environment/prod/env_terraform.tfvars" -state=tfstates/prod/terraform.tfstate azurerm_monitor_autoscale_setting.VMSS_autoscale /subscriptions/XXXX/resourceGroups/my_resourceGroupsPROD/providers/Microsoft.insights/autoscalesettings/my_autoscale
    ### Import - ENV2 No Prod
    terraform import -var-file="environment/nopro/env_terraform.tfvars" -state=tfstates/noprod/terraform.tfstate azurerm_monitor_autoscale_setting.VMSS_autoscale /subscriptions/XXXX/resourceGroups/my_resourceGroupsNOPROD/providers/Microsoft.insights/autoscalesettings/my_autoscale
    
    

    那么,你申请的时候一定要指定文件

    ### Apply - ENV2 No Prod
    terraform apply -var-file="environment/noprod/env_terraform.tfvars" -var="action=charge" -state=tfstates/noprod/terraform.tfstate
    

    【讨论】:

    • 感谢这个答案。我在“”中设置状态选项后它起作用了:terraform import -var-file="environment/prod/env_terraform.tfvars" -state="tfstates/prod/terraform.tfstate" azurerm_monitor_autoscale_setting.VMSS_autoscale /subscriptions/XXXX/resourceGroups/my_resourceGroupsPROD/providers/Microsoft.insights/autoscalesettings/my_autoscale
    猜你喜欢
    • 2021-10-08
    • 2021-01-04
    • 2023-03-18
    • 2020-04-30
    • 2020-04-29
    • 2021-09-25
    • 2018-05-06
    • 2020-01-23
    • 2020-08-27
    相关资源
    最近更新 更多