page_type | languages | name | description | products | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|---|
sample |
|
Using Azure DevOps Pipelines Workload identity federation (OIDC) or Managed Identity with Azure for Terraform Deployments |
A sample showing how to configure Azure DevOps Workload identity federation (OIDC) or Managed Identity connection to Azure with Terraform and then use that configuration to deploy resources with Terraform. |
|
azure-devops-terraform-oidc-ci-cd |
Using Azure DevOps Pipelines Workload identity federation (OIDC) or Managed Identity with Azure for Terraform Deployments
This is a two part sample. The first part demonstrates how to configure Azure and Azure DevOps for credential free deployment with Terraform. The second part demonstrates an end to end Continuous Delivery Pipeline for Terraform.
File/folder | Description |
---|---|
terraform-example-deploy |
Some Terraform with Azure Resources for the demo to deploy. |
terraform-oidc-config |
The Terraform to configure Azure and Azure DevOps ready for Workload identity federation (OIDC) or Managed Identity authenticaton. |
.gitignore |
Define what to ignore at commit time. |
CHANGELOG.md |
List of changes to the sample. |
CONTRIBUTING.md |
Guidelines for contributing to the sample. |
README.md |
This README file. |
LICENSE.md |
The license for the sample. |
This sample includes the following features:
- Option 1: Setup 3 Azure User Assigned Managed Identities with Federation ready for Azure DevOps Workload identity federation (OIDC).
- Option 2: Setup 3 Azure App Registrations (Service Principals) with Federation ready for Azure DevOps Workload identity federation (OIDC).
- Option 3: Setup 3 Azure User Assigned Managed Identities with Self-hosted Azure DevOps agents in Azure Container Instances.
- Setup an Azure Storage Account for State file management.
- Setup Azure DevOps repository and environments ready to deploy Terraform with Workload identity federation (OIDC).
- Run a Continuous Delivery pipeline for Terraform using Workload identity federation (OIDC) auth for state and deploying resources to Azure.
- Run a Pull Request workflow with some basic static analysis.
There are three approaches shown in the code for credential free deployment of Azure resources from Azure DevOps.
The preferred method is to use Workload identity federation (OIDC) with a User Assigned Managed Identity since this does not require elevated permissions in Azure Entra ID and has a longer token timeout than an App Registration (Service Principal). However the code also shows the App Registration (Service Principal) approach for those that prefer that method. If you choose the App Registration (Service Principal) approach then the account creating the infrastructure will need permission to create Applications in Azure Active Directory.
The default option is Option 1: oidc-with-user-assigned-managed-identity
.
This option will create managed identities configured for federation and service connections for them.
This option will create app registrations configured for federation and service connections for them.
This option will create self hosted agents and service connections for the managed identities associated with those agents.
- HashiCorp Terraform CLI: Download
- Azure CLI: Download
- An Azure Subscription: Free Account
- An Azure DevOps Organization and Project: Free Organization
- Clone the repository locally and then follow the Demo / Lab.
The instructions for this sample are in the form of a Lab. Follow along with them to get up and running.
- Navigate to dev.azure.com.
- Login and navigate to your organisation.
- Click on the marketplace icon (shopping bag) in the top right of the screen and select
Browse Marketplace
. - Type
terraform
into the search bar and hit enter. - Find the task published by
Microsoft DevLabs
and click on it. (Here is a shortcut to take you directly there) - Click
Get if free
. - Select your organisation from the drop down list and click
Install
. -
NOTE: If you are not an organisation administator, you will need to click
Request
instead and then prompt your admin person to approve it.
- Navigate to dev.azure.com.
- Login and select the
User Settings
icon in the top right and thenPersonal access tokens
. - Click
New token
. - Type
Demo_OIDC
into theName
field. - Click
Show all scopes
down at the bottom of the dialog. - Check these scopes:
Agent Pools
:Read & manage
Build
:Read & execute
Code
:Full
Environment
:Read & manage
Service Connections
:Read, query, & manage
Project and Team
:Read, write, & manage
Variable Groups
:Read, create, & manage
- Click
Create
-
IMPORTANT: Copy the token and save it somewhere.
- Clone this repository to your local machine.
- Open the repo in Visual Studio Code. (Hint: In a terminal you can open Visual Studio Code by navigating to the folder and running
code .
). - Navigate to the
terraform-oidc-config
folder and create a new file calledterraform.tfvars
. - In the
terraform.tfvars
file add the following:
prefix = "<your_initials>-<date_as_YYYYMMDD>"
azure_devops_organisation_target = "<your_azure_devops_organisation_name>"
azure_devops_project_target = "<your_azure_devops_project_name>"
e.g.
prefix = "JFH-20221208"
azure_devops_organisation_target = "my-organization"
azure_devops_project_target = "my-project"
NOTE if you wish to use Options 2 or 3, then also add this setting to
terraform.tfvars
:
security_option = "self-hosted-agents-with-managed-identity"
OR
security_option = "oidc-with-app-registration"
- Open the Visual Studio Code Terminal and navigate the
terraform-oidc-config
folder. - Run
az login
and follow the prompts to login to Azure with your Global Administrator account. - Run
az account show
. If you are not connected to you test subscription, change it by runningaz account set --subscription "<subscription-id>"
- Run
terraform init
. - Run
terraform apply
. - You'll be prompted for the variable
var.azure_devops_token
. Paste in the PAT you generated earlier and hit enter. - The plan will complete. Review the plan and see what is going to be created.
- Type
yes
and hit enter once you have reviewed the plan. - Wait for the apply to complete.
- You will see three outputs from this run. These are the Service Principal Ids that you will require in the next step. Save them somewhere.
NOTE: If you are a Microsoft employee you may get a 403 error here. If so, you need to grant your PAT SSO access to the Azure-Samples organisation. This does not affect non-Microsoft users.
When deploying the example you will have selected to use the default Managed Identity approach or the Service Principal approach choose the relevant option below.
- Login to the Azure Portal with your Global Administrator account.
- Navigate to your Subscription and select
Resource groups
. - Click the resource group post-fixed
identity
(e.g.JFH-20221208-identity
). - Look for a
Managed Identity
resource post-fixed withdev
and click it.
- Click on
Federated Credentials
. - There should only be one credential in the list, select that and take a look at the configuration.
- Examine the
Subject identifier
and ensure you understand how it is built up.
- Login to the Azure Portal with your Global Administrator account.
- Navigate to
Azure Active Directory
and selectApp registrations
. - Select
All applications
, then find the one you just created post-fixed withdev
(e.g.JFH-20221208-dev
). - Select
Certificate & secrets
, thenFederated credentials
. - There should only be one credential in the list, select that and take a look at the configuration.
- Examine the
Subject identifier
and ensure you understand how it is built up.
- Navigate to your Subscription and select
Resource groups
. - You should see four newly created resource groups.
- Click the resource group post-fixed
dev
(e.g.JFH-20221208-dev
). - Select
Access control (IAM)
and selectRole assignments
. - Under the
Contributor
role, you should see that yourdev
Service Principal or Managed Identity has been granted access directly to the resource group.
- Navigate to your Subscription and select
Resource groups
. - Click the resource group post-fixed
state
(e.g.JFH-20221208-state
). - You should see a single storage account in there, click on it.
- Select
Containers
. You should see adev
,test
andprod
container. - Select the
dev
container. - Click
Access Control (IAM)
and selectRole assignments
. - Scroll down to
Storage Blob Data Owner
. You should see yourdev
Service Principal or Managed Identity has been assigned that role.
- Open Azure DevOps in your browser (login if you need to).
- Navigate to your organisation and project.
- Click
Repos
, then select your new repo in the drop down at the top of the page (e.g.JFH-20221208-wild-dog
). Click on it. - You should see some files under source control.
- Hover over
Pipelines
, then selectEnvironments
. - You should see 3 environments called
dev
,test
andprod
. - Click on the
dev
environment and take a look at the settings.
- Hover over
Pipelines
, then selectLibrary
. - You should see 3 variable groups called
dev
,test
andprod
. - Click on the
dev
environment and take a look at the variables.
- Click
Project Settings
in the bottom left corner. - Click
Service connections
under thePipelines
section. - There should be 3 service connections configured for Managed Identity or Workload Identity Federation depending on the option you choose.
- Click on one of the service connections and click
Edit
to look at the settings.
- Click
Project Settings
in the bottom left corner. - Click
Agent pools
under thePipelines
section. - There should be 3 new agent pools configured.
- Click on one of them and navigate to the
Agents
tab, you should see 2 agents in the pool ready to accept runs.
- Click on
Pipelines
- You should see a pipeline in the list. Click on it.
- Click on
Edit
and examine the pipeline.
- Select
Pipelines
, then click on the pipeline you created. - Click the
Run pipeline
in the top right, then clickRun
in the dialog. - Wait for the run to appear or refresh the screen, then click on the run to see the details.
- You will see each environment being deployed one after the other. In a real world scenarios you may want to have a manual intervention on the environment for an approval to promote to the next stage.
- You will also note that the
Validation
step was skipped. - Drill into the log for one of the environments and look at the
Terraform Apply
step. You should see the output of the plan and apply. - Run the workflow again and take a look at the log to compare what happens on the Day 2 run.
- Clone your new repository and open it in Visual Studio Code.
- Create a new branch, call it whatever you want.
- Open the
terraform-example-deploy/virtual-machine.tf
file. - Rename the virtual machine to
example-machine-pr
. - Commit and push the change.
- Raise a pull request.
- You'll see the Azure DevOps Pipeline running in the pull request. This is because we created a branch policy to enforce this.
- The
Terraform Fmt
step will fail formain.tf
. Fix it, commit and push your change. - Wait for the Pipeline to run again.
- Merge the Pull Request.
- Navigate to
Pipelines
and watch the run.