prakashrajansakthivel
prakashrajansakthivel

Reputation: 2042

unable to execute terraform apply in azure devops

I am trying to execute terraform scripts through azure devops. I am not able to apply and validate through different tasks, though terraform plan is successful terraform apply is failing with

##[error]TypeError: Cannot read property 'includes' of null

Here is the terraform tasks which I am using. I have tried with two different tasks

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'

Here is my terraform file

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}#
     
}

Upvotes: 0

Views: 2623

Answers (2)

prakashrajansakthivel
prakashrajansakthivel

Reputation: 2042

I have added the entire pipeline details here just for the reference - https://gist.github.com/PrakashRajanSakthivel/e6d8e03044a51d74803499aca75a258c

Upvotes: 0

SauravDas-MT
SauravDas-MT

Reputation: 1444

The terraform file looks fine, there is no issue with it. Can you check if you are using the Azure Service Principal method.

A Service Principal is considered a good practice for DevOps within your CI/CD pipeline. It is used as an identity to authenticate you within your Azure Subscription to allow you to deploy the relevant Terraform code. You can follow this document for the detailed demo for Deploying Terraform using Azure DevOps.

If you have already followed the above steps and still facing the same issue then the problem is with the the 'Configuration directory' setting in the Terraform 'Validate and Apply' Release Pipeline step. Changing it to the path containing the build artifacts will fix the issue. Also check if the workingDirectory which you have provided is correct.

A similar but not same problem was also raised here, check if it solves the problem.

Upvotes: 1

Related Questions