<?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 Petr Nemec on Medium]]></title>
        <description><![CDATA[Stories by Petr Nemec on Medium]]></description>
        <link>https://medium.com/@petr.nemec?source=rss-20c7c4e1b23e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*aYmLvfPqtYM73jWCZ9okSQ.jpeg</url>
            <title>Stories by Petr Nemec on Medium</title>
            <link>https://medium.com/@petr.nemec?source=rss-20c7c4e1b23e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:00:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@petr.nemec/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[Azure DevOps with Terraform in a multi-tenant environment]]></title>
            <link>https://medium.com/@petr.nemec/azure-devops-with-terraform-in-a-multi-tenant-environment-c6362c9dc130?source=rss-20c7c4e1b23e------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6362c9dc130</guid>
            <category><![CDATA[azure]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[devops]]></category>
            <dc:creator><![CDATA[Petr Nemec]]></dc:creator>
            <pubDate>Tue, 25 Feb 2025 16:21:06 GMT</pubDate>
            <atom:updated>2025-02-25T16:21:06.355Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*v0fjBf1m6eABFXBF-YPAgw.png" /></figure><p>I’ve recently come across a challenging task to quickly provision a few virtual machines in a subscription assigned to a child tenant to create a safe environment to play around with Entra Sync.</p><p>There’s already been a pipeline to deploy and maintain VMs in the parent tenant, and for the sake of simplicity, I just wanted to tweak it a bit and reuse it in a multi-tenant environment. The main issue was that my Terraform code utilized workspaces, which in turn (as we all know) use one common storage account for their state files.</p><p>That storage account lived in the same tenant as some other resources deployed by Terraform within the parent tenant. With the new workspace, though, an issue with cross-tenant authentication for the service connection arose. The pipeline needed access to the state file stored in another tenant while it still should be able to maintain resources in a different one.</p><p>I like the extension <a href="https://marketplace.visualstudio.com/items?itemName=JasonBJohnson.azure-pipelines-tasks-terraform"><strong>Azure Pipelines Terraform Tasks,</strong> </a>which gives me all the features I need to carry out tasks with respect to Terraform. The approach I’ve chosen in the context of this particular setup was to use two different service connections.</p><p><a href="https://marketplace.visualstudio.com/items?itemName=JasonBJohnson.azure-pipelines-tasks-terraform">Azure Pipelines Terraform Tasks - Visual Studio Marketplace</a></p><p>The whole story ended up from the high perspective with the following tasks:</p><ol><li>Creating the 1st service connection and the service principal in the child tenant.</li><li>Registering the service principal into the parent tenant.</li><li>Granting the service principal from the parent tenant the respective. permissions to access the storage account with Terraform state.</li><li>Creating the 2nd service connection to let the pipeline access Terraform backend</li><li>Modify the pipeline to use different service connections for init and plan/apply</li></ol><h4>The 1st service connection (Service principal in the child tenant)</h4><p>Let’s get started by creating a first service connection in ADO and using secret as a means of authenticating so that the Terraform extension does not time out while waiting for approval. This was the problem that arose when the federated credentials were used instead of secrets. The service principal is automatically granted the contributor role on the scope of the subscription for which it was created.</p><p>After the service connection is created, find the respective SP under App registration.</p><p>Get the Service Principal Application (client) ID in the Child Tenant</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*3E17bK7l6BSTlPdw9yPR7A.png" /></figure><p>Specify who can use this application. Go to Manage &gt; Authentication &gt; Supported account types &gt; Accounts in any organizational directory (Any Microsoft Entra ID tenant — Multitenant)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*ydwm-cIeflQET1cUdwVdgw.png" /><figcaption>Allow to use the app in any directory</figcaption></figure><h4>Registering the service principal into the parent tenant</h4><p>Let’s use the SP created in the previous step to interact with the parent tenant.</p><p>Make the parent tenant accept service principals from the child tenant as the external identities.</p><p>Go to <em>Microsoft Entra admin center (parent tenant), External Identities &gt; Cross-tenant access settings &gt; Organizational settings &gt; Add organization</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YOPgEyTkSmPBk_wtvE9oGA.png" /><figcaption>The child tenant</figcaption></figure><p>Then open up the cloud console and import a service principal from the external child tenant into the parent tenant using Azure CLI. It registers the specified application’s service principal (identified by its Application ID) in the local directory, enabling it to be assigned roles and permissions within the tenant.</p><pre># Import the SP from the child tenant<br>az ad sp create --id 3e3c0000-1111-2222-3333-444455556666</pre><p>Get the value of <strong><em>appDisplayName</em></strong> from the response</p><pre>{<br>  &quot;@odata.context&quot;: &quot;https://graph.microsoft.com/v1.0/$metadata#servicePrincipals/$entity&quot;,<br>  &quot;accountEnabled&quot;: true,<br>  &quot;addIns&quot;: [],<br>  &quot;alternativeNames&quot;: [],<br>  &quot;appDescription&quot;: null,<br>  &quot;appDisplayName&quot;: &quot;foo-bar-Sandbox-83fb0000-0000-0000-0000-0000000000000&quot;,<br>  &quot;appId&quot;: &quot;3e3c0000-1111-2222-3333-444455556666&quot;,<br>...<br>}</pre><p>to grant it the following RBAC roles on the scope of the storage account holding the state files.</p><h4>Grant permissions to access the storage account with Terraform state</h4><p>To let the pipeline read and modify the state file the following two roles are essential</p><ul><li><strong>Storage Account Contributor</strong> — This role is required to allow the service principal access to the storage account keys, which provide full access to storage account data.</li></ul><p>Verify the access from the child tenant’s perspective and make sure it can reach the blobs containing the state files. Navigate to the child tenant and open up the cloud console here.</p><p>Before you do that, remember to create another secret for the SP in the child tenant. The first one without a description was created automatically as a result of the wizard when the first service connection was created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*LN3vgKjk1GhYpW2-gTANpg.png" /></figure><p>The second one was created for the purpose of having the pipeline talk to the Terraform backend, and it will be used in the following steps.</p><pre># AZ login using the local SP and target tenant<br>az login --service-principal --username &lt;Application (client) ID&gt; --password &lt;secret from above&gt; --tenant &lt;parent tenant ID&gt;<br>Cloud Shell is automatically authenticated under the initial account signed-in with. Run &#39;az login&#39; only if you need to use a different account<br>[<br>  {<br>    &quot;cloudName&quot;: &quot;AzureCloud&quot;,<br>    &quot;homeTenantId&quot;: &quot;&lt;parent tenant ID&gt;&quot;,<br>    &quot;id&quot;: &quot;........&quot;,<br>    &quot;isDefault&quot;: true,<br>    &quot;managedByTenants&quot;: [],<br>    &quot;name&quot;: &quot;Sandbox&quot;,<br>    &quot;state&quot;: &quot;Enabled&quot;,<br>    &quot;tenantId&quot;: &quot;&lt;parent tenant ID&gt;&quot;,<br>    &quot;user&quot;: {<br>      &quot;name&quot;: &quot;3e3c0000-1111-2222-3333-444455556666&quot;,<br>      &quot;type&quot;: &quot;servicePrincipal&quot;<br>    }<br>  }<br>]<br><br># List the blobs from the container<br>az storage blob list --account-name &lt;storage account&gt; --container-name &lt;container name&gt; --auth-mode login</pre><h4>2nd service connection</h4><p>Create the 2nd service connection as the type of Azure Resource Manager with a secret created in the previous step and being passed manually.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*XyZziLA_ejrejLJMgj0JMQ.png" /><figcaption>2nd service connection</figcaption></figure><p>The <em>Subscription ID</em> and <em>Directory (tenant) ID</em> used to create the connection must refer to those of the parent tenant to set it up properly.</p><h4>Adjustment of the pipeline</h4><p>Here’s finally the snippet of my pipeline with references to two different service connections to be able to talk to the backend while deploying some new stuff in another tenant.</p><pre>    - task: TerraformCLI@2<br>      displayName: &#39;terraform init&#39;<br>      inputs:<br>        allowTelemetryCollection: false<br>        command: init<br>        workingDirectory: &#39;$(System.DefaultWorkingDirectory)/$(tfRootFolder)&#39;<br>        backendType: azurerm<br>        # Service connection to authorize backend access.<br>        backendServiceArm: $(backendServiceArm)  #&lt;&lt;&lt; Point to the 2nd service connection here<br>        backendAzureRmSubscriptionId: $(tfBackendSubscriptionId)<br>        backendAzureRmResourceGroupName: $(tfBackendRg)<br>        backendAzureRmResourceGroupLocation: $(tfBackendLocation)<br>        backendAzureRmStorageAccountName: $(tfBackendStorage)<br>        backendAzureRmStorageAccountSku: $(tfBackendStorageSku)<br>        backendAzureRmContainerName: $(tfBackendContainer)<br>        backendAzureRmKey: $(tfBackendBlobName)<br><br>    - task: TerraformCLI@2<br>      displayName: &#39;Select workspace&#39;<br>      inputs:<br>        workingDirectory: &#39;$(System.DefaultWorkingDirectory)/$(tfRootFolder)&#39;<br>        command: workspace<br>        workspaceSubCommand: select<br>        workspaceName: ${{ parameters.environment }}<br><br>    - task: TerraformCLI@2<br>      displayName: &#39;terraform plan&#39;<br>      inputs:<br>        allowTelemetryCollection: false<br>        command: plan<br>        workingDirectory: &#39;$(System.DefaultWorkingDirectory)/$(tfRootFolder)&#39;<br>        environmentServiceName: $(ServiceConnectionName) #&lt;&lt;&lt; Point to the 1st service connection here<br>        publishPlanResults: &#39;my_plan_name&#39;<br>        commandOptions: &#39;-out=tfplan -detailed-exitcode&#39;</pre><p>Happy Terraforming across multiple tenants :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6362c9dc130" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>