【问题标题】:Update existing app service with Terraform使用 Terraform 更新现有应用服务
【发布时间】:2020-01-22 03:51:38
【问题描述】:

在我的 Azure 帐户中有一些资源。资源组、应用服务、存储帐户... 我使用 Azure 门户或 Powershell 创建了这些资源。 然后我编写了一个 terraform 脚本来添加 other 资源和 更新 一些现有资源。特别是我对更新应用服务感兴趣。我想向它添加一些设置和托管标识。 发生的事情是 terraform 说:“看,已经有一个应用程序服务具有您指定的名称”。 我尝试使用“terraform import”将现有的应用服务绑定到我的 terrafom 状态文件,但这样做我会丢失我在 terraform 文件中放入的设置。

我该如何解决这个问题?谢谢。

【问题讨论】:

    标签: azure terraform


    【解决方案1】:

    terraform import 是要走的路。如果您的文件中有任何现有设置:只需将其删除,直到您完全导入应用服务。

    完整教程 - 使用资源组而不是应用服务,但原理相同: https://azurecitadel.com/automation/terraform/lab6/#lab-importing-resources

    • 创建资源组:
    Grab the ID for the azure resource: id=$(az group show --name deleteme --query id --output tsv)
    

    在新的 import.tf 文件中为资源创建一个空节

    resource "azurerm_resource_group" "deleteme" {}
    
    • 运行导入命令:
    terraform import azurerm_resource_group.deleteme $id
    terraform-labs$ terraform import azurerm_resource_group.deleteme $id
    Acquiring state lock. This may take a few moments...
    azurerm_resource_group.deleteme: Importing from ID "/subscriptions/2d31be49-d999-4415-bb65-8aec2c90ba62/resourceGroups/deleteme"...
    azurerm_resource_group.deleteme: Import complete!
      Imported azurerm_resource_group (ID: /subscriptions/2d31be49-d999-4415-bb65-8aec2c90ba62/resourceGroups/deleteme)
    azurerm_resource_group.deleteme: Refreshing state... (ID: /subscriptions/2d31be49-d999-4415-bb65-8aec2c90ba62/resourceGroups/deleteme)
    
    Import successful!
    
    The resources that were imported are shown above. These resources are now in
    your Terraform state and will henceforth be managed by Terraform.
    
    • 运行terraform plan,您应该会看到一些错误,因为我们的块没有被填充
    • 运行terraform state show azurerm_resource_group.deleteme
    id       = /subscriptions/2d31be49-d999-4415-bb65-8aec2c90ba62/resourceGroups/deleteme
    location = westeurope
    name     = deleteme
    tags.%   = 0
    
    • 使用 loc 变量添加名称参数和位置
    • 重新运行 terraform 计划,它应该没有错误,也没有计划的更改
    • 资源现在已完全导入并在 Terraform 的控制下安全。

    【讨论】:

    • 好的,谢谢。我只是觉得这很乏味。如果我在 Azure 中已经有很多资源怎么办?我必须对每个现有资源应用这种方法...
    • 我同意... terraform 本身是这样说的:“虽然这可能看起来很乏味,但它仍然为 Terraform 用户提供了导入现有资源的途径。未来版本的 Terraform 将完全生成配置,大大简化了这个过程。”
    • @AlexAIT azure开发管线中如何添加导入步骤?
    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 2021-11-27
    • 1970-01-01
    • 2018-07-28
    • 2021-06-08
    • 2023-03-16
    • 1970-01-01
    • 2021-12-08
    相关资源
    最近更新 更多