Demo repo using Terraform to deploy an Azure Application Gateway resource, with two Azure Storage Accounts acting as backends hosting static websites.
In HCP Terraform:
- New | Workspace
- Select project
- Click Create
- Select CLI-driven workflow
- Enter workspace name 'terraform-appgw'
https://www.hashicorp.com/en/blog/access-azure-from-hcp-terraform-with-oidc-federation
-
Create Azure resource group
az group create --name rg-terraform-appgw-australiaeast --location australiaeast
-
Create service principal and role assignments
az ad sp create-for-rbac --name sp-terraform-appgw-australiaeast --role Contributor --scopes /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-terraform-appgw-australiaeast
Make a note of the appID and tenant ID. Use the appId in the next command:
az role assignment create --assignee appId --role "Role Based Access Control Administrator" --scope /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-terraform-appgw-australiaeast
-
Create
credential.json
{ "name": "tfc-plan-credential", "issuer": "https://app.terraform.io", "subject": "organization:flcdrg:project:my-project-name:workspace:terraform-appgw:run_phase:plan", "description": "Terraform Plan", "audiences": [ "api://AzureADTokenExchange" ] }
-
And create federated credentials for your service principal. The
--id
parameter should be set to the appId that you noted previously.az ad app federated-credential create --id xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx --parameters credential.json
Update the
credential.json
file and replace 'plan' with 'apply' (3 places). Create a second federated credential by running the above command again. -
Repeat this process to enable authentication from GitHub Actions (for the deployment to the storage accounts). Replace
octo-org
with your username or organisation, andocto-repo
with the GitHub repository name.{ "name": "main", "issuer": "https://token.actions.githubusercontent.com", "subject": "repo:octo-org/octo-repo:environment:production", "description": "Production environment", "audiences": [ "api://AzureADTokenExchange" ] }
-
Get the Azure subscription ID:
az account subscription list
-
Back in HCP Terraform, set the following environment variables in your workspace
TFC_AZURE_PROVIDER_AUTH
= trueTFC_AZURE_RUN_CLIENT_ID
= <appId value>ARM_SUBSCRIPTION_ID
= Azure subscription idARM_TENANT_ID
= Azure tenant id
-
Click on your profile and select Account settings, then Tokens.
-
Click on Create an API token
-
In Description field enter
terraform-appgw-australiaeast
-
Review (and adjust if required) the expiration date
-
Click Create
-
Note the token value.
To allow the GitHub Action workflows to connect to HCP Terraform and to Azure, in the GitHub project
-
Go to Settings, Secrets and Variables
-
In Actions, click on New repository secret
-
In Name, enter
TF_API_TOKEN
-
In Secret, paste the HCP Terraform token, and click Add secret
-
Also add the same variable as a Dependabot secret (so that Dependabot pull requests can succeed)
-
Repeat this process for the following variables. They only need to be added as Repository Secrets:
AZURE_CLIENT_ID
the Application (client) IDAZURE_TENANT_ID
the Directory (tenant) IDAZURE_SUBSCRIPTION_ID
your subscription ID