microsoft / azure-pipelines-terraform

Azure Pipelines tasks for installing Terraform and running Terraform commands in a build or release pipeline.
MIT License
106 stars 66 forks source link

TerraformTaskV1@0 init without backendServiceArm using -backend=false #30

Closed chris-b-chalmers closed 1 month ago

chris-b-chalmers commented 4 years ago

I am looking to have a pipeline which only runs a Terraform install, Terraform init then Terraform validate. This works normally when I specify my backendServiceArm, backendAzureRmResourceGroupName, backendAzureRmStorageAccountName, backendAzureRmContainerName and backendAzureRmKey inputs.

- task: TerraformTaskV1@0
  displayName: 'Terraform init'
  inputs:
    provider: 'azurerm'
    command: 'init'
    backendServiceArm: '$(backendServiceArm)'
    backendAzureRmResourceGroupName: '$(backendAzureRmResourceGroupName)'
    backendAzureRmStorageAccountName: '$(backendAzureRmStorageAccountName)'
    backendAzureRmContainerName: '$(backendAzureRmContainerName)'
    backendAzureRmKey: '$(backendAzureRmKey)'

I'm aiming to remove the backend configuration so that I dont need to grant this pipeline access into Azure, and any validation can be done only within Azure DevOps.

I have tried combinations of the following but it always errors with "##[error]Error: Input required: backendServiceArm"

- task: TerraformTaskV1@0
  displayName: 'Terraform init'
  inputs:
    provider: 'azurerm'
    command: 'init'
    commandOptions: -backend=false

Is there a way to accomplish this using the extension?

AmrutaKawade commented 4 years ago

Currently its not possible. we will evaluate this suggestion.

merckm commented 3 years ago

Why is this still not possible. It dopes not make any sense. You configure the backend in the provider section of your terraform script.

titolopolous commented 3 years ago

I find this issue to be ridiculous as well. I did manage to get it to working using a PowerShell task that called terraform init -backend=false directly, but it would be much nicer to be able to do so using the proper extension.

PwshPally commented 3 years ago

This is an issue in TerraformTaskV2@2 as well. Should that be a separate issue?

i-am-shodan commented 2 years ago

+1 for this

p0onage commented 2 years ago

+1 for this, I have my remote states in different storage accounts and I'd like to init without the need to target any environment for the purpose of just wanting to run validate afterwards. I'll have to do it in powershell for now but this would be a nice addition just having a flag to say if -backend=false

p0onage commented 2 years ago

I found a better work around for me. I populated the required fields with N/A and put -backend=false as a command option. This allows me to init locally for the build step then I can init remote for my specific environment branches.

- task: TerraformTaskV3@3
  displayName: 'Terraform init test'
  inputs:
    provider: 'azurerm'
    command: 'init'
    commandOptions: '-input=false -backend=false'
    workingDirectory: '${{parameters.tf_path}}'
    backendServiceArm:  '${{parameters.tf_azurerm}}'
    backendAzureRmResourceGroupName: 'N/A'
    backendAzureRmStorageAccountName: 'N/A'
    backendAzureRmContainerName: 'N/A'
    backendAzureRmKey: 'N/A'

In my dev, qa and production stages I just need to remove the .terraform folder to make sure I don't get the error "migrate state needed"

rm -rf directoryname $(Pipeline.Workspace)/terraform/.terraform

victorfrye commented 11 months ago

+1 for this. Believe it's an issue as of v4 still.

jaredfholgate commented 11 months ago

I'm not sure of the exact use case here? The purpose of the Task is to simplify using a Service Connection with Terraform. It is designed for running Terraform with connections to Cloud providers.

If you are just running a local fmt / validate you don't need the task or a Service Connection. It actually makes it more complicated than it needs to be. I appreciate the Task could be updated to handle this edge case more gracefully, but in the meantime you can just use the standard CLI tooling.

E.g.

jobs:
  - job: validate
    displayName: Validate Terraform
    pool:
      vmImage: ubuntu-latest
    steps:
      - task: TerraformInstaller@0
        displayName: Install Terraform
        inputs:
          terraformVersion: 'latest'
      - pwsh: terraform fmt -check
        displayName: Terraform Format Check
      - pwsh: terraform init -backend=false
        displayName: Terraform Init
      - pwsh: terraform validate
        displayName: Terraform Validate

Example taken from here.

mericstam commented 1 month ago

closing this; alternative way provided