Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Terraform Import blocks aren't working correctly between windows / linux #258

Open
Poltergeisen opened this issue Nov 11, 2024 · 5 comments

Comments

@Poltergeisen
Copy link

Poltergeisen commented Nov 11, 2024

Hi, I have some code like this:

locals {
  level_2 = zipmap(
    flatten([for key, value in var.management_groups : formatlist("${key}/%s", keys(value.children)) if value.children != null]),
    flatten([for value in var.management_groups : values(value.children) if value.children != null])
  )
}

import {
  for_each = local.level_2

  id = "/providers/Microsoft.Management/managementGroups/${basename(each.key)}"
  to = module.base.azurerm_management_group.level_2[each.key]
}

On windows, when I run this it does this:

Image

On my linux build agent, I get the following:

Image

It looks like it's adding some "\"'s in the import statements for some reason. I can't reproduce locally, or on WSL. Only on my build agent using this task...

This is my build script so far, as bare bones as I could make it:


trigger:
  branches:
    include:
      - main
  batch: true

pool: usw-landing-zone-prod-vmss
stages:
  - stage:
    jobs:
    - job: "INIT_PLAN"
      steps:
      - checkout: self
      - task: TerraformInstaller@1
        inputs:
          terraformVersion: '1.9.8'
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'init'
          workingDirectory: '$(System.DefaultWorkingDirectory)/environments/lhg'
          backendAzureRmUseEnvironmentVariablesForAuthentication: false
          backendServiceArm: 'usw2-shared-services'
          backendAzureRmResourceGroupName: 'rg'
          backendAzureRmStorageAccountName: 'acct'
          backendAzureRmContainerName: 'tstate'
          backendAzureRmKey: 'cf/coreresources/environments/lhg/main.tfstate'
      - task: TerraformTaskV4@4
        inputs:
          provider: 'azurerm'
          command: 'plan'
          workingDirectory: '$(System.DefaultWorkingDirectory)/environments/lhg'
          environmentServiceNameAzureRM: 'usw2-shared-services'
@Poltergeisen
Copy link
Author

When I log into the build agent directly and run the terraform plan command, I get the proper output.

There seems to be something missing in regards to the backend configuration maybe? Is there special setup I need to do for that to work correctly for plan steps? Shouldn't the init be enough?

@Poltergeisen
Copy link
Author

I think this may be due to permissions errors. Is there a better way to maybe surface those? When I enable trace logging I get this:

2024-11-11T08:49:37.301Z [DEBUG] provider.terraform-provider-azurerm_v4.7.0_x5: {"error":{"code":"AuthorizationFailed","message":"The client '' with object id '' does not have authorization to perform action 'Microsoft.Management/managementGroups/read' over scope '/providers/Microsoft.Management/managementGroups/lhg-sandboxes' or the scope is invalid. If access was recently granted, please refresh your credentials."}}

@Poltergeisen
Copy link
Author

I'd like to request that this error be surfaced better. If there is a permissions error I feel like that should be displayed instead of a "not found" generic message. The only way to know you're hitting permissions errors is to enable trace logging.

@mericstam
Copy link
Collaborator

Hi, sorry for late reply, I will see if we can surface permission errors better.

br
Manuel

@Poltergeisen
Copy link
Author

Poltergeisen commented Dec 15, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants