0

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}#
     
}
5
  • locally is works for you? Commented Sep 2, 2021 at 13:03
  • yes, I am able to run in vscode terminal Commented Sep 2, 2021 at 13:06
  • Where do you set var.apimName and var.rgName? Commented Sep 2, 2021 at 17:51
  • basically, I have hardcoded my parameters in the pipeline, for the question sake, I have made it to var. Commented Sep 3, 2021 at 6:43
  • Hello @threeleggedrabbit, May I know if you have referred this thomasthornton.cloud/2020/07/08/…? are you using SPN method in azure devops ? Commented Sep 6, 2021 at 13:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I am not using the serviceprincipal method, I am using service connection to get things created in terraform. the problem is that the storage key, the value is empty because in the variable group it was mentioned in the different name. rest all the settings are fine. thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.