【问题标题】:How to use Terraform output variables stored in an storage account in Azure DevOps release pipelines?如何在 Azure DevOps 发布管道中使用存储在存储帐户中的 Terraform 输出变量?
【发布时间】:2021-05-27 18:08:58
【问题描述】:

在 Stackoverflow 上有与此类似的问题,但没有一个解决我在使用 Azure 存储帐户保留输出时使用 Terraform 的问题。

这是我的场景,听起来可能很熟悉:

我的 terraform 脚本使用功能键预配 Azure HTTP 触发的功能。此外,此 terraform 脚本提供了一个调用上述 HTTP 触发函数的 Web 应用程序。 HTTP 触发函数的密钥存储在 Web 应用的 appsettings.json 文件中,以将其包含在 HTTP 标头对 HTTP 触发函数的调用中。

以下代码 sn-p 说明了如何在 terraform 中配置 HTTP 触发的函数:

resource "azurerm_function_app" "myhttpfunc" {
  name                      = var.funcname
  location                  = "${azurerm_resource_group.rc.location}"
  resource_group_name       = "${azurerm_resource_group.rc.name}"
  app_service_plan_id       = "${azurerm_app_service_plan.funcsserviceplan.id}"
  storage_account_name      = "${azurerm_storage_account.funcstorage.name}"
  storage_account_access_key = "${azurerm_storage_account.funcstorage.primary_access_key}"
}

访问函数键的输出变量如下:

output "funchostkeys" {
  value = data.azurerm_function_app_host_keys.myhttpfunc
  sensitive = true
}

我假设此输出和其他输出将在未来某个时候出现在托管在专用 Azure 存储帐户上的 terraform.tfstate 中。

我的问题是,如何通过将配置条目替换为 terraform 在输出变量中产生的内容来在 Azure 发布管道中获取特定输出以操纵 appsettings.json 文件?

例如,this post 建议根据以下行将 terraform 的输出生成到文件中,但在我的情况下这似乎不是一个选项,因为我的 terraform 脚本利用 Azure 存储来保留输出。

terraform output -json > outputs.json

【问题讨论】:

    标签: azure-devops terraform terraform-provider-azure


    【解决方案1】:

    根据您的要求,您可以尝试使用以下命令输出变量:

    terraform output -raw funchostkeys
    

    然后您可以使用logging command 将值设置为管道变量。

    例如:Powershell 脚本

    - powershell: |
       echo ##vso[task.setvariable variable=varname]$(terraform output -raw funchostkeys)
       
      displayName: 'PowerShell Script'
    

    然后您可以使用Replace token task 来使用管道变量替换appsettings.json 中的值。

    您可以在 appsettings.json 中定义 #{variablename}#。运行管道时,输出值将设置在目标位置。

    【讨论】:

    • 我无法使用替换令牌任务,因为这显然适用于构建管道。在我的场景中,terraform 在发布管道中运行,其下一步是部署 Azure 函数。因此,变量是在 appsettings.json 文件已部署到 azure 函数应用的发布步骤中确定的。无法访问函数的 appsettings.json,因为它位于 zip 文件中。
    猜你喜欢
    • 2021-12-11
    • 2020-06-14
    • 2020-06-11
    • 2020-09-15
    • 1970-01-01
    • 2022-10-14
    • 2019-07-28
    • 2021-10-29
    • 2020-05-19
    相关资源
    最近更新 更多