【问题标题】:unable to execute terraform apply in azure devops无法在 azure devops 中执行 terraform apply
【发布时间】:2021-09-02 12:42:04
【问题描述】:

我正在尝试通过 azure devops 执行 terraform 脚本。我无法通过不同的任务申请和验证,尽管 terraform 计划成功 terraform apply 失败了

##[error]TypeError: 无法读取属性 'includes' of null

这是我正在使用的 terraform 任务。我尝试了两个不同的任务

1.

- task: ms-devlabs.custom-terraform-tasks.custom-terraform-release-task.TerraformTaskV2@2
  displayName: 'Terraform : apply -auto-approve'
  inputs:
    command: apply
    workingDirectory: '$(System.DefaultWorkingDirectory)/Terraform'
    commandOptions: '-auto-approve'
    environmentServiceNameAzureRM: 'ps-vs-sc'
    backendAzureRmResourceGroupName: '$(rgname)'
    backendAzureRmStorageAccountName: $(strname)
    backendAzureRmContainerName: $(tfContainer)
    backendAzureRmKey: '$(storagekey)'
- task: TerraformTaskV2@2
  inputs:
    provider: 'azurerm'
    command: 'apply'
    workingDirectory: '$(System.DefaultWorkingDirectory)/Terraform'
    commandOptions: '--auto-approve'
    environmentServiceNameAzureRM: 'ps-vs1-sc'

这是我的地形文件

provider "azurerm" {
features {}
}

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "2.74.0"
    }
  }
}

data "azurerm_api_management" "example" {
  name                = var.apimName
  resource_group_name = var.rgName
}

resource "azurerm_api_management_api" "example" {
  name                = var.apName
  resource_group_name = var.rgName
  api_management_name = var.apimname
  revision            = "1"
  display_name        = "Example API1"
  path                = "example1"
  protocols           = ["https"]
  service_url         = "http://123.0.0.0:8000"
  subscription_required  = true

  import {
    content_format = "openapi+json"
    content_value  = #{storageaccountlink}#
     
}

【问题讨论】:

  • 本地适合你吗?
  • 是的,我可以在 vscode 终端中运行
  • 你在哪里设置var.apimNamevar.rgName
  • 基本上,我已经在管道中硬编码了我的参数,为了这个问题,我已经把它变成了 var。
  • 您好@threeleggedrabbit,请问您是否推荐过此thomasthornton.cloud/2020/07/08/…?你在 azure devops 中使用 SPN 方法吗?

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


【解决方案1】:

terraform 文件看起来不错,没有问题。你能检查一下你是否使用了 Azure 服务主体方法。

服务主体被认为是 CI/CD 管道中 DevOps 的良好实践。它用作在 Azure 订阅中对您进行身份验证的身份,以允许您部署相关的 Terraform 代码。 Deploying Terraform using Azure DevOps的详细demo你可以关注这个文档。

如果您已经按照上述步骤操作,但仍然面临同样的问题,那么问题出在 Terraform 'Validate and Apply 中的 'Configuration directory' 设置' 释放管道步骤。将其更改为包含构建工件的路径将解决此问题。还要检查您提供的 workingDirectory 是否正确。

here也提出了类似但不一样的问题,检查是否解决了问题。

【讨论】:

  • 我没有使用 serviceprincipal 方法,我使用服务连接来获取在 terraform 中创建的东西。问题是存储键的值是空的,因为在变量组中它以不同的名称被提及。其余所有设置都很好。谢谢
【解决方案2】:

我在此处添加了整个管道详细信息,仅供参考 - https://gist.github.com/PrakashRajanSakthivel/e6d8e03044a51d74803499aca75a258c

【讨论】:

    猜你喜欢
    • 2019-03-07
    • 2022-12-08
    • 2022-01-03
    • 2017-12-29
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多