Skip to content

Instantly share code, notes, and snippets.

View geekzter's full-sized avatar

Eric van Wijk geekzter

View GitHub Profile
@geekzter
geekzter / terraform-variable-case-azure-pipeline.ps1
Last active April 6, 2021 18:13
Fix the case of TF_VAR environment variables in Azure Pipeline
# Convert uppercased Terraform environment variables to their original casing expected by Terraform
foreach ($tfvar in $(Get-ChildItem -Path Env: -Recurse -Include TF_VAR_*)) {
$upperCaseName = $tfvar.Name + "_UC"
$properCaseName = $tfvar.Name.Substring(0,7) + $tfvar.Name.Substring(7).ToLowerInvariant()
$null = New-Item -Path env:$upperCaseName -Value $tfVar.Value
Remove-Item -Path env:$($tfvar.Name)
$null = New-Item -Path env:$properCaseName -Value $tfVar.Value
Set-Item -Path env:$upperCaseName -Value $null
}
# List environment variables
@geekzter
geekzter / terraform-variable-foo.tf
Created March 22, 2021 12:59
Terraform variable 'foo'
variable foo {
type = string
default = "notbar"
}
@geekzter
geekzter / terraform-version-github-actions.yml
Last active March 22, 2021 12:47
Use preferred Terraform version in GitHub Actions
- name: Detect desired Terraform version
id: terraform-version-check
run: |
$terraformVersion = (Get-Content .terraform-version)
Write-Output "::set-output name=TERRAFORM_VERSION::${terraformVersion}"
shell: pwsh
- name: Get Terraform
uses: hashicorp/setup-terraform@v1
with:
@geekzter
geekzter / terraform-version-azure-pipeline.yml
Last active April 6, 2021 18:13
Use preferred Terraform version in Azure Pipeline
- pwsh: |
$terraformVersion = (Get-Content .terraform-version)
Write-Host "##vso[task.setvariable variable=version;isOutput=true]${terraformVersion}"
displayName: 'Get preferred Terraform version'
- task: TerraformInstaller@0
displayName: 'Use preferred Terraform version'
inputs:
terraformVersion: '$(terraformVersion)'
@geekzter
geekzter / terraform-backend-azure-pipeline.yml
Last active April 6, 2021 18:13
Terraform backend setup in Azure Pipeline
- task: AzureCLI@2
displayName: 'Terraform init'
enabled: ${{ not(parameters.testMode) }}
inputs:
azureSubscription: '$(subscriptionConnection)'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
Copy-Item backend.tf.example backend.tf
# Run Terraform
@geekzter
geekzter / terraform-authentication-github-actions.yml
Last active May 13, 2024 19:49
Terraform authentication re-uses GitHub Azure Action credentials
- name: Get Azure CLI
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Get Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 'latest'
terraform_wrapper: false
@geekzter
geekzter / github-action-azure-secret.json
Created March 22, 2021 10:20
Azure Service Principal credentials for GitHub Azure login action
{
"clientId": "00000000-0000-0000-0000-000000000000",
"clientSecret": "00000000-0000-0000-0000-000000000000",
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"tenantId": "00000000-0000-0000-0000-000000000000",
"objectId": "00000000-0000-0000-0000-000000000000",
(...)
}
@geekzter
geekzter / terraform-authentication-powershell2.ps1
Last active April 6, 2021 18:11
Use Azure CLI to authenticate to Terraform interactively, Azure CLI subscription takes precedence
az login
az account set --subscription 00000000-0000-0000-0000-000000000000
$env:ARM_SUBSCRIPTION_ID=$(az account show --query "id" -o tsv)
$env:ARM_TENANT_ID=$(az account show --query "tenantId" -o tsv)
@geekzter
geekzter / terraform-authentication-powershell1.ps1
Last active April 6, 2021 18:11
Use Azure CLI to authenticate to Terraform interactively, ARM_SUBSCRIPTION_ID takes precedence
$env:ARM_SUBSCRIPTION_ID=00000000-0000-0000-0000-000000000000
$env:ARM_TENANT_ID=00000000-0000-0000-0000-000000000000
az login -t $env:ARM_TENANT_ID
az account set --subscription $env:ARM_SUBSCRIPTION_ID
@geekzter
geekzter / terraform-authentication-azure-pipeline.yml
Last active April 6, 2021 18:12
Terraform authentication inherits Azure Pipeline Service Connection credentials
- task: AzureCLI@2
displayName: 'Terraform init'
inputs:
azureSubscription: '$(subscriptionConnection)'
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
# Propagate pipeline Service Principal as Terraform variables
$env:ARM_CLIENT_ID ??= $env:servicePrincipalId
$env:ARM_CLIENT_SECRET ??= $env:servicePrincipalKey