Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- Param
- (
- [Parameter(Mandatory=$true)]
- [String] $StateFileName,
- [Parameter(Mandatory=$true)]
- [String] $StorageAccountName,
- [Parameter(Mandatory=$true)]
- [String] $SubscriptionId,
- [Parameter(Mandatory=$true)]
- [String] $ResourceGroup,
- [Parameter(Mandatory=$true)]
- [String] $StorageContainerName
- )
- # Change to script directory
- $ScriptPath = $MyInvocation.MyCommand.Path
- $CurrentWorkingDirectory = Split-Path $ScriptPath
- Set-Location $CurrentWorkingDirectory
- Write-Host "##[debug] :: az account show:"
- az account show | ForEach-Object {Write-Host "##[debug] :: $($_)"}
- Write-Host "##[debug] :: az group list:"
- az group list | ForEach-Object {Write-Host "##[debug] :: $($_)"}
- Write-Host "##[debug] :: az storage account list:"
- az storage account list | ForEach-Object {Write-Host "##[debug] :: $($_)"}
- Write-Host "##[debug] :: Getting keys for storage account $StorageAccountName"
- # List keys for storage account
- $Keys = az storage account keys list --account-name $StorageAccountName | ConvertFrom-Json
- # Create a temporary file for backend configuration
- $Content = 'terraform {
- backend "azurerm" {
- subscription_id = "' + $SubscriptionId + '"
- resource_group_name = "' + $ResourceGroup + '"
- storage_account_name = "' + $StorageAccountName + '"
- container_name = "' + $StorageContainerName + '"
- key = "' + $StateFileName + '"
- access_key = "' + $Keys[0].value + '"
- }
- }'
- Write-Host "##[debug] :: Writing file 'backend.tf'"
- Set-Content -Value $Content -Path "backend.tf"
- Write-Host "##[debug] :: Running 'terraform init'"
- # Run terraform initialization with above backend configured
- terraform init
- Write-Host "##[debug] :: Completed 'terraform init'"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement