Advertisement
mariussm

Terraform init workaround

Sep 15th, 2020
2,050
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. Param
  3. (
  4.     [Parameter(Mandatory=$true)]
  5.     [String] $StateFileName,
  6.     [Parameter(Mandatory=$true)]
  7.     [String] $StorageAccountName,
  8.     [Parameter(Mandatory=$true)]
  9.     [String] $SubscriptionId,
  10.     [Parameter(Mandatory=$true)]
  11.     [String] $ResourceGroup,
  12.     [Parameter(Mandatory=$true)]
  13.     [String] $StorageContainerName
  14. )
  15.  
  16. # Change to script directory    
  17. $ScriptPath = $MyInvocation.MyCommand.Path
  18. $CurrentWorkingDirectory = Split-Path $ScriptPath
  19. Set-Location $CurrentWorkingDirectory
  20.  
  21. Write-Host "##[debug] :: az account show:"
  22. az account show | ForEach-Object {Write-Host "##[debug] ::    $($_)"}
  23.  
  24. Write-Host "##[debug] :: az group list:"
  25. az group list | ForEach-Object {Write-Host "##[debug] ::    $($_)"}
  26.  
  27. Write-Host "##[debug] :: az storage account list:"
  28. az storage account list | ForEach-Object {Write-Host "##[debug] ::    $($_)"}
  29.  
  30. Write-Host "##[debug] :: Getting keys for storage account $StorageAccountName"
  31.  
  32. # List keys for storage account
  33. $Keys = az storage account keys list --account-name $StorageAccountName | ConvertFrom-Json
  34.  
  35. # Create a temporary file for backend configuration
  36. $Content = 'terraform {
  37.  backend "azurerm" {
  38.    subscription_id      = "' + $SubscriptionId + '"
  39.    resource_group_name  = "' + $ResourceGroup + '"
  40.    storage_account_name = "' + $StorageAccountName + '"
  41.    container_name       = "' + $StorageContainerName + '"
  42.    key                  = "' + $StateFileName + '"
  43.    access_key           = "' + $Keys[0].value + '"
  44.  }
  45. }'
  46.  
  47. Write-Host "##[debug] :: Writing file 'backend.tf'"
  48. Set-Content -Value $Content -Path "backend.tf"
  49.  
  50. Write-Host "##[debug] :: Running 'terraform init'"
  51.  
  52. # Run terraform initialization with above backend configured
  53.  
  54. terraform init
  55.  
  56. Write-Host "##[debug] :: Completed 'terraform init'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement