<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justin Drögemeier on Medium]]></title>
        <description><![CDATA[Stories by Justin Drögemeier on Medium]]></description>
        <link>https://medium.com/@justindroegemeier?source=rss-2c459617bea6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CpgUSW-msCVi-6H9</url>
            <title>Stories by Justin Drögemeier on Medium</title>
            <link>https://medium.com/@justindroegemeier?source=rss-2c459617bea6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 12:06:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@justindroegemeier/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Build Secure Azure Connections in Azure DevOps with Terraform]]></title>
            <link>https://medium.com/@justindroegemeier/how-to-build-secure-azure-connections-in-azure-devops-with-terraform-2e1a7c0633a0?source=rss-2c459617bea6------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e1a7c0633a0</guid>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[service-connection]]></category>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[workload-identity]]></category>
            <category><![CDATA[azure-devops]]></category>
            <dc:creator><![CDATA[Justin Drögemeier]]></dc:creator>
            <pubDate>Sun, 09 Feb 2025 18:31:03 GMT</pubDate>
            <atom:updated>2025-02-09T19:02:29.948Z</atom:updated>
            <content:encoded><![CDATA[<h3>Secure and Simplify: Building a Secure Connection to Azure with Workload Identity Federation in Azure DevOps Using Terraform</h3><h3>Introduction</h3><p>In this article, I will show you how to set up a <strong>Managed Identity</strong> in Azure using Terraform and configure it as a service connection in Azure DevOps. Instead of using client secrets — as is common in many tutorials — we will leverage the more secure and streamlined method of <strong>Workload Identity Federation</strong>.</p><h3>Background: Service Connections in Azure DevOps</h3><p>Service connections in Azure DevOps enable your pipelines to authenticate against external platforms — such as cloud providers like Azure. They allow your pipelines to securely access and interact with resources on these platforms without exposing sensitive credentials.</p><p>When it comes to Azure Service Connections, many tutorials focus on generating a client secret that is used to authenticate a service principal and perform actions on its behalf. However, this approach has disadvantages:</p><p>1. <strong>Secret Rotation</strong><br> Client secrets should ideally be rotated regularly. This manual process can be error-prone, and any misstep might cause your service connection to suddenly stop working.</p><p>2. <strong>Secret Leakage</strong><br> If the secret is compromised, unauthorized users could potentially impersonate the service principal and perform malicious actions in your Azure environment.</p><h3>The Solution: Workload Identity Federation</h3><p>To overcome these issues, Workload Identity Federation offers a more secure and efficient alternative. When you create an Azure Resource Manager service connection in Azure DevOps, you’ll notice that Workload Identity Federation is recommended. Even when using the options that automatically create resources, the Workload Identity Federation flow is used:</p><figure><img alt="Azure DevOps Service Connection Creation Dialog" src="https://cdn-images-1.medium.com/max/482/1*5qIAU9VVjh4_64Jm1NnXTw.png" /><figcaption>This image shows the dialog for manually creating a new service connection in Azure DevOps.</figcaption></figure><p>But how does this authentication method work?<br>Below is a diagram that illustrates the overall authentication flow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OvV2RYUzhy5zSENOpCDFZw.png" /><figcaption>A sequence diagram which shows how Workload Identity Federation works</figcaption></figure><p>The key advantage here is that with Workload Identity Federation, you no longer need to store static credentials. Instead, Azure DevOps issues short-lived tokens to authenticate against Azure, securely using its own token. When you click ‘Next’ in the service connection creation dialog, you’ll see the options for configuring this token-based authentication. Moreover, this approach is flexible and can also integrate with other identity providers if needed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*ufNt2j4bJbBcgnfa4gDuwg.png" /><figcaption>Here you can see some of the information required to set up Workload Identity Federation. In this tutorial, however, we will use Terraform to configure these settings automatically.</figcaption></figure><h3>Setup in Terraform</h3><p>Now we jump into the practical part of this article :) Here are the requirements for this example:</p><ul><li>Terraform</li><li>An Azure Devops Organization</li><li>An Azure subscription that is accessible from your local machine via Azure CLI</li></ul><h4>1. Terraform Provider Configuration</h4><p>The first step is to set up your Terraform provider configuration. For example:</p><pre>terraform {<br>  required_version = &quot;~&gt; 1.0&quot;<br>  required_providers {<br>    azurerm = {<br>      source  = &quot;hashicorp/azurerm&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br><br>    azuredevops = {<br>      source  = &quot;microsoft/azuredevops&quot;<br>      version = &quot;~&gt; 1.0&quot;<br>    }<br>  }<br>}<br><br>provider &quot;azurerm&quot; {<br>  subscription_id = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot; # The subscription id where we want to create the managed identity<br>  features {}<br>}<br><br>provider &quot;azuredevops&quot; {<br>  org_service_url       = &quot;https://dev.azure.com/your-organization&quot;<br>  personal_access_token = &quot;PAT from Azure Devops&quot; # A PAT from Azure Devops which allows us to create and maintain service connections<br>}</pre><p>To generate a PAT from Azure DevOps, you have multiple options. You can either create one directly in the Azure DevOps UI or use the following Azure CLI command (make sure to specify the tenant where your Azure DevOps subscriptions are bound):</p><pre>az account get-access-token --tenant &lt;tenant-id&gt; --resource &quot;499b84ac-1321-427f-aa17-267ca6975798&quot; --query accessToken --output tsv</pre><h4>2. Creating a Managed Identity</h4><p>Next, create a user-assigned Managed Identity in Azure, which will be used for the federated authentication:</p><pre># Create a resource group<br>resource &quot;azurerm_resource_group&quot; &quot;this&quot; {<br>  name     = &quot;example-resource-group&quot;<br>  location = &quot;Germany West Central&quot;<br>}<br><br># Create a managed identity (user-assigned) for workload identity federation<br>resource &quot;azurerm_user_assigned_identity&quot; &quot;this&quot; {<br>  name                = &quot;workload-identity&quot;<br>  resource_group_name = azurerm_resource_group.this.name<br>  location            = &quot;Germany West Central&quot;<br>}</pre><h4>3. Configuring the Service Connection and Federated Identity Credentials</h4><p>Now we combine everything by creating the service connection in Azure DevOps and configuring the federated identity credentials in Azure. This configuration ensures that when Azure receives a JWT, it knows which claims to expect for successful authentication.</p><pre># Retrieve the current subscription details configured in the provider<br>data &quot;azurerm_subscription&quot; &quot;this&quot; {}<br><br># Create a service connection using Workload Identity Federation and the managed identity created above<br>resource &quot;azuredevops_serviceendpoint_azurerm&quot; &quot;this&quot; {<br>  project_id                             = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;<br>  service_endpoint_name                  = &quot;Tutorial Service Connection&quot;<br>  description                            = &quot;Service connection which use Workload Identity Federation&quot;<br>  service_endpoint_authentication_scheme = &quot;WorkloadIdentityFederation&quot;<br>  credentials {<br>    serviceprincipalid = resource.azurerm_user_assigned_identity.this.client_id<br>  }<br>  azurerm_spn_tenantid      = data.azurerm_subscription.this.tenant_id<br>  azurerm_subscription_id   = data.azurerm_subscription.this.subscription_id<br>  azurerm_subscription_name = data.azurerm_subscription.this.display_name<br>}<br><br># Create a Federated Identity Credential which will be used to authenticate the service connection. <br># The issuer and subject are the values from the service connection and makes sure that Azure knows which claims to expect.<br>resource &quot;azurerm_federated_identity_credential&quot; &quot;this&quot; {<br>  name                = &quot;example-federated-identity-credential&quot;<br>  resource_group_name = azurerm_resource_group.this.name<br>  audience            = [&quot;api://AzureADTokenExchange&quot;]<br>  parent_id           = azurerm_user_assigned_identity.this.id<br>  issuer              = azuredevops_serviceendpoint_azurerm.this.workload_identity_federation_issuer<br>  subject             = azuredevops_serviceendpoint_azurerm.this.workload_identity_federation_subject<br>}</pre><p>Below is the complete Terraform configuration:</p><pre>terraform {<br>  required_version = &quot;~&gt; 1.0&quot;<br>  required_providers {<br>    azurerm = {<br>      source  = &quot;hashicorp/azurerm&quot;<br>      version = &quot;~&gt; 4.0&quot;<br>    }<br>    azuredevops = {<br>      source  = &quot;microsoft/azuredevops&quot;<br>      version = &quot;~&gt; 1.0&quot;<br>    }<br>  }<br>}<br><br>provider &quot;azurerm&quot; {<br>  subscription_id = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;<br>  features {}<br>}<br><br>provider &quot;azuredevops&quot; {<br>  org_service_url       = &quot;https://dev.azure.com/your-organization&quot;<br>  personal_access_token = &quot;&quot;<br>}<br><br># Create a resource group<br>resource &quot;azurerm_resource_group&quot; &quot;this&quot; {<br>  name     = &quot;example-resource-group&quot;<br>  location = &quot;Germany West Central&quot;<br>}<br><br># Create a managed identity (user-assigned) for workload identity federation<br>resource &quot;azurerm_user_assigned_identity&quot; &quot;this&quot; {<br>  name                = &quot;workload-identity&quot;<br>  resource_group_name = azurerm_resource_group.this.name<br>  location            = &quot;Germany West Central&quot;<br>}<br><br># Retrieve the current subscription details configured in the provider<br>data &quot;azurerm_subscription&quot; &quot;this&quot; {}<br><br># Create a service connection using Workload Identity Federation and the managed identity created above<br>resource &quot;azuredevops_serviceendpoint_azurerm&quot; &quot;this&quot; {<br>  project_id                             = &quot;xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&quot;<br>  service_endpoint_name                  = &quot;Tutorial Service Connection&quot;<br>  description                            = &quot;Service connection that uses Workload Identity Federation&quot;<br>  service_endpoint_authentication_scheme = &quot;WorkloadIdentityFederation&quot;<br>  credentials {<br>    serviceprincipalid = azurerm_user_assigned_identity.this.client_id<br>  }<br>  azurerm_spn_tenantid      = data.azurerm_subscription.this.tenant_id<br>  azurerm_subscription_id   = data.azurerm_subscription.this.subscription_id<br>  azurerm_subscription_name = data.azurerm_subscription.this.display_name<br>}<br><br># Create a Federated Identity Credential which will be used to authenticate the service connection. <br># The issuer and subject are the values from the service connection and makes sure that Azure knows which claims to expect.<br>resource &quot;azurerm_federated_identity_credential&quot; &quot;this&quot; {<br>  name                = &quot;example-federated-identity-credential&quot;<br>  resource_group_name = azurerm_resource_group.this.name<br>  audience            = [&quot;api://AzureADTokenExchange&quot;]<br>  parent_id           = azurerm_user_assigned_identity.this.id<br>  issuer              = azuredevops_serviceendpoint_azurerm.this.workload_identity_federation_issuer<br>  subject             = azuredevops_serviceendpoint_azurerm.this.workload_identity_federation_subject<br>}</pre><h4>Applying the Configuration</h4><p>Once you have initialized your Terraform project with:</p><pre>terraform init</pre><p>apply the configuration with:</p><pre>terraform apply</pre><p>When everything is set up correctly, you should now see in your Azure DevOps project a new service connection and, in the Azure portal, a new Managed Identity with the corresponding federated credentials:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YuoUjB2OVQGiE5JR4iHrRA.png" /><figcaption>Our newly created service connection in Azure DevOps, configured using Terraform</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X61Kthemqw0-hGQTEsleCg.png" /><figcaption>Our newly created Managed Identity in Azure, configured using Terraform</figcaption></figure><p>If you click on the newly created service connection in Azure DevOps, you will see that it uses Workload Identity Federation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/886/1*O9FoPuX_lbIctT-drmTYSg.png" /><figcaption>We can see that our service connection use Workload Identity Federation</figcaption></figure><p>And if you inspect the Managed Identity in Azure, you will also find the federated credentials that were generated with Terraform:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JQiCOTa1uWrrWU8w3ynZrA.png" /><figcaption>Here we can see that our Terraform code executed successfully, creating the federated credentials.</figcaption></figure><p>Now you’re all set and can use this service connection in your pipeline. Various tasks — such as <em>AzureCLI@2</em> — can utilize this connection.</p><h3>Summary and What’s Next</h3><p>In this article, we’ve explored how to configure Workload Identity Federation in Azure DevOps using Terraform. By setting up a managed identity along with federated credentials, we established a secure, token-based authentication mechanism that eliminates the need for long-lived client secrets. This approach not only enhances security but also simplifies the management of service connections within your CI/CD pipelines.</p><blockquote><strong>Spoiler: Part 2 Coming Soon!</strong><br>In the next part, I’ll show you how to use the same method to set up a Service Principal with an Application Registration. This will let you create a service connection that works across multiple tenants, making your Azure DevOps deployments even more flexible.</blockquote><p>Stay tuned for Part 2, where we dive deeper into multi-tenant authentication using Workload Identity Federation with Service Principals!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e1a7c0633a0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>