markpjohnson
9/14/2017 - 9:53 PM

terraform.ps1

Instructions

  1. Ensure this folder exists: $env:USERPROFILE\azure
  2. Create a *.env file with your subscription ID, client ID, client secret, tenant ID, and storage account access key (for remote storage).
  3. Save terraform.ps1 and put it in your system PATH.
  4. Ensure Docker is installed and running.

Examples

terraform.ps1 myenv plan -out=plan.tfplan
terraform.ps1 myenv apply plan.tfplan

# change credentials and deployment targets by changing profiles
terraform.ps1 myotherenv plan -out=plan.tfplan
terraform.ps1 myotherenv apply plan.tfplan
ARM_SUBSCRIPTION_ID=xxxx-sub-id
ARM_CLIENT_ID=xxx-client-id
ARM_CLIENT_SECRET=xxx-client-secret
ARM_TENANT_ID=xxxx-tenant-id
ARM_ACCESS_KEY=xxxx-storage-access-key
$drv = (Get-Location).Drive.Root.Replace('\', '/')
$loc = (Get-Location).Drive.CurrentLocation.Replace("\", "/")

$azure_env = $args[0]
$destArgs = @()
if ($args.Length -gt 1) {
    $destArgs = $args[1..$args.Length]
}

$path = Get-Location
$parent = (Get-Item $path.Path).Parent
$varsFiles = Get-ChildItem -Path $parent.FullName -Filter "*.tfvars"

$varsFileArg = ''
$varsFileParameter = '-var-file='
if ($varsFiles -is [array]) {
    $varsPaths = $varsFiles | ForEach-Object { $_ | Resolve-Path -Relative }
    $varsPaths = $varsPaths -join " $varsFileParameter"
    $varsFileArg = $varsFileParameter + $varsPaths
} elseif ($varsFiles) {
    $relPath = $varsFiles | Resolve-Path -Relative
    $varsFileArg = $varsFileParameter + $relPath
}

if ($varsFileArg) {
    $varsFileArg = $varsFileArg.Replace("\", "/")
    $destArgs += $varsFileArg
}

docker pull hashicorp/terraform:latest
docker run --rm -it --env-file "$env:USERPROFILE\azure\$azure_env.env" -v ${drv}:/data --workdir=/data/${loc} hashicorp/terraform:latest $destArgs