{"id":9941,"date":"2015-10-04T08:07:00","date_gmt":"2015-10-04T08:07:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/visualstudioalm\/2015\/10\/04\/automating-azure-resource-group-deployment-using-a-service-principal-in-visual-studio-online-buildrelease-management\/"},"modified":"2022-08-02T01:27:17","modified_gmt":"2022-08-02T09:27:17","slug":"automating-azure-resource-group-deployment-using-a-service-principal-in-visual-studio-online-buildrelease-management","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/devops\/automating-azure-resource-group-deployment-using-a-service-principal-in-visual-studio-online-buildrelease-management\/","title":{"rendered":"Automating Azure Resource Group deployment using a Service Principal in Visual Studio Online: Build\/Release Management"},"content":{"rendered":"<h2>Connect\u00a0your Azure subscriptions to VSTS in 3 clicks<\/h2>\n<p><strong>New improved user experience to setup Azure Resource Manager based service connection in VSTS.<\/strong><\/p>\n<p>Follow the post below to configure\u00a0Visual Studio Team Services to communicate with Azure in order to provision or deploy Azure Resource Manager resources such as virtual machines<\/p>\n<ul>\n<li>To setup Azure Service end point in VSTS, from your Visual Studio Account, navigate to your Team Project and click on gear icon.<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"774\" height=\"88\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/3821.task4-step1.png\" border=\"0\" \/><\/p>\n<p>\u00a0<\/p>\n<p><img decoding=\"async\" width=\"781\" height=\"185\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/7571.task4-step2.png\" border=\"0\" \/><\/p>\n<ul>\n<li>Navigate to Services tab and click on &#8216;New Service Endpoint&#8217; in the left pane.<\/li>\n<li>From the drop-down, select &#8216;Azure Resource Manager&#8217; option.<\/li>\n<\/ul>\n<p><strong>Now, setting up an Azure Service endpoint is easy, you just need to select the subscription on which to create a service endpoint, and you are ready to deploy to Azure.<\/strong><\/p>\n<p><img decoding=\"async\" width=\"537\" height=\"331\" class=\"alignnone wp-image-25155\" alt=\"Auto create SPN\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/SPNMSA-300x185.png\" \/> <strong>Note<\/strong>:<\/p>\n<p>A new Azure Service Principal will be created and assigned with the &#8216;Contributor&#8217; role. The default role assignment will have\u00a0access to all the resources in the selected subscription. You can modify the Service Principal access from <a href=\"http:\/\/portal.azure.com\/\">Azure portal <\/a>> Subscriptions > Users > Roles.<\/p>\n<p>If your subscription is not listed or to specify an existing service principal, click the link in the dialog, which will switch to manual entry mode.<\/p>\n<p>\u00a0<\/p>\n<hr \/>\n<h3>Manual configuration:<\/h3>\n<p>Follow the post below to manually configure\u00a0Visual Studio Team Services<\/p>\n<ul>\n<li>Download &amp; run <span style=\"color: #3366ff\"><a href=\"https:\/\/github.com\/Microsoft\/\"><span style=\"color: #3366ff\">this PowerShell script<\/span><\/a><\/span> in an Azure Powershell window to generate required data for Service Principal based Azure service connection. Running this script would prompt you for: \n<ul>\n<li>The name of your Azure Subscription name<\/li>\n<li>A password that you would like to set for the Service Principal that is going to be created<\/li>\n<li>Note: the script has been tested with Azure PowerShell version <a target=\"_blank\" href=\"https:\/\/github.com\/Azure\/azure-powershell\/releases\/tag\/v1.0.2-December2015\" title=\"Azure PowerShell 1.0.2\" rel=\"noopener\">1.0.2<\/a>.<\/li>\n<\/ul>\n<\/li>\n<li>Once successful, the script would output the following details for the Azure Service Endpoint. \n<ul>\n<li>Connection Name<\/li>\n<li>Subscription Id<\/li>\n<li>Subscription Name<\/li>\n<li>Service Principal Client Id<\/li>\n<li>Service Principal key<\/li>\n<li>Tenant Id<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>To setup Azure Service end point in VSTS, from your Visual Studio Account, navigate to your Team Project and click on gear icon.<\/p>\n<p><img decoding=\"async\" width=\"774\" height=\"88\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/3821.task4-step1.png\" border=\"0\" \/><\/p>\n<ul>\n<li>Click Services tab and click on &#8216;New Service Endpoint&#8217; in the left pane.<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"781\" height=\"185\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/7571.task4-step2.png\" border=\"0\" \/><\/p>\n<ul>\n<li>From the drop-down, select &#8216;Azure Resource Manager&#8217; option. In the dialog, click the link at end of the text &#8220;If your subscription is not listed or to specify an existing service principal, click here&#8221;, which will switch to manual entry mode. You can always switch back to &#8220;Auto creation of SPN mode&#8221; by clicking the link in the manual mode dialog.<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"555\" height=\"462\" class=\"alignnone wp-image-19415\" alt=\"AzureSPN03\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/AzureSPN03-300x250.png\" \/><\/p>\n<ul>\n<li>Enter the input values that you generated from the PowerShell<\/li>\n<li>Click \u2018OK\u2019\u2019. We now have established a connection to Azure from your VSTS account.<\/li>\n<li>From Build\/Release hub, now you add &#8220;Resource Group Deployment Task&#8221; (for example) and use the subscription.<img decoding=\"async\" width=\"835\" height=\"340\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/6558.task4-step4.png\" border=\"0\" \/><\/li>\n<\/ul>\n<p>Note: the script has been tested with Azure PowerShell version <a target=\"_blank\" href=\"https:\/\/github.com\/Azure\/azure-powershell\/releases\/tag\/v1.0.2-December2015\" title=\"Azure PowerShell 1.0.2\" rel=\"noopener\">1.0.2<\/a>.<\/p>\n<p>\u00a0<\/p>\n<hr \/>\n<p>The above steps are sufficient for the purpose described. <strong><em>If you would like to rather create the service principal on your own instead of using the above script<\/em><\/strong>, then follow these steps below:<\/p>\n<ul>\n<li>To create an Azure Active Directory application, login to your Azure account through the <a href=\"https:\/\/manage.windowsazure.com\/\">classic portal<\/a>.<\/li>\n<li>Select Active Directory from the left pane.<img decoding=\"async\" width=\"135\" height=\"123\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/7144.task1-step2.png\" border=\"0\" \/><\/li>\n<li>Select the directory that you want to use for creating the new application.<img decoding=\"async\" width=\"805\" height=\"167\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/5305.task1-step3.png\" border=\"0\" \/><\/li>\n<li>To add a new application in your directory, click on Applications and click on Add.<img decoding=\"async\" width=\"780\" height=\"140\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/6082.task1-step4.png\" border=\"0\" \/><\/li>\n<li>Choose &#8220;Add an application that my organization is developing&#8221;.<img decoding=\"async\" width=\"311\" height=\"102\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/0383.task1-step4-2.png\" border=\"0\" \/><\/li>\n<li>Select WEB APPLICATION AND\/OR WEB API and click the next button.<img decoding=\"async\" width=\"347\" height=\"188\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/0550.task1-step5.png\" border=\"0\" \/> Though we intend to automate Azure Resource Group deployment from VSTS, we will have to create a Web App and use its service principal to authenticate with Azure Resource Manager.<\/li>\n<li>Enter a recognizable URL as we will need it later for role assignment.<img decoding=\"async\" width=\"265\" height=\"154\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/0640.task1-step6.png\" border=\"0\" \/><\/li>\n<li>The existence of the web-site is not validated. We are registering a web app name-space so that we can create a Service Principal identity for the application.<\/li>\n<li>Congratulations, you now have an AAD Application. Click on the CONFIGURE tab:<img decoding=\"async\" width=\"696\" height=\"201\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/1072.task1-step7.png\" border=\"0\" \/><\/li>\n<li>To create your service principal password, from CONFIGURE tab, find Client ID and copy it. This will be your Service Principal user name.<img decoding=\"async\" width=\"764\" height=\"71\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/8053.task2-step1.png\" border=\"0\" \/><\/li>\n<li>From the &#8220;keys&#8221; section, from the drop-down select 1 or 2 year duration.<img decoding=\"async\" width=\"645\" height=\"121\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/3465.task2-step2.png\" border=\"0\" \/><\/li>\n<li>After you hit save at the bottom, it will display your key, which is basically your Service Principal account password. Copy and store the key value. You won&#8217;t be able to retrieve it later.<\/li>\n<li>Retrieve Tenant ID for your Azure account. Click &#8220;View End-Points&#8221; on the bottom panel.<img decoding=\"async\" width=\"57\" height=\"64\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/8322.task2-step3.png\" border=\"0\" \/><\/li>\n<li>Copy any of the URLs displayed in the pop-up dialog, The GUID in there is your tenant ID, which you&#8217;ll need later.<a href=\"https:\/\/login.microsoftonline.com\/72921e51-521b-439d-8412-XXXXXXXXXXXX\/federationmetadata\/2007-06\/federationmetadata.xml\">https:\/\/login.microsoftonline.com\/<span style=\"background-color: #ffff00\">72921e51-521b-439d-8412-XXXXXXXXXXXX<\/span>\/federationmetadata\/2007-06\/federationmetadata.xml <\/a><\/li>\n<li>Now we have Service Principal account username, password and tenant id. Client ID: a63e0985-XXXX-XXXX-XXXX-XXXXXXXXXXXX Key: JjLsHGuoQKnWTqv4\/C8Abyg8dQ1eqjXXXXXXXXXXX= Tenant ID: 72921e51-521b-439d-8412-XXXXXXXXXXXX ****<\/li>\n<li>You have now created a service principal in the directory, but the service does not have any permissions or scope assigned. You will need to explicitly grant the service principal permissions to perform operations at some scope. You will need to use <a href=\"http:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/install-configure-powershell\/\">http:\/\/azure.microsoft.com\/en-us\/documentation\/articles\/install-configure-powershell\/<\/a>. Log in as your Microsoft identity in order to grant roles to your Service Principal identity.<code class=\"cplusplus\">PS C:&gt;Switch-AzureMode -Name AzureResourceManager\nPS C:&gt;Add-AzureAccount # This will pop up a login dialog\n<\/code><\/li>\n<li><code class=\"cplusplus\"><\/code>Assign roles to your Service Principal. For now, giving it access to the whole subscription. You can limit the access by providing the scope parameter. You can use either App ID Uri or Client ID as the value for the -ServicePrincipalName parameter.<code class=\"js\">PS C:&gt;New-AzureRoleAssignment -ServicePrincipalName &lt;a href=\"http:\/\/RNWebAppforVSO\">http:\/\/RNWebAppforVSO&lt;\/a> -RoleDefinitionName Contributor\n<\/code><\/li>\n<li><code class=\"js\"><\/code>If you run Get-AzureRoleAssignment, you should see the assignment.<\/li>\n<li>Above commands work with older version Azure PS modules. If you are on latest Azure ARM PS modules, you need to use equivalent RM command-lets. For example, Add-AzureRMAccount, New-AzureRMRoleAssignment etc.<\/li>\n<li>You could also do role assignment to your application from the new <a target=\"_blank\" href=\"http:\/\/portal.azure.com\" title=\"Azure Portal\" rel=\"noopener\">Azure portal<\/a>. Click Browse and select Subscriptions. Select the subscription you are using. Click the Access button.<img decoding=\"async\" alt=\"\" src=\"https:\/\/devblogs.microsoft.com\/devops\/wp-content\/uploads\/sites\/6\/2015\/10\/3187.SPN-Roles.png\" border=\"0\" \/>Click Add. Select Contributor as the role. Search and select the name of the application you just created. Click OK to grant the service principal access to your subscription.<\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<p><strong>Resources:<\/strong><\/p>\n<ul>\n<li><a href=\"https:\/\/azure.microsoft.com\/en-in\/documentation\/articles\/resource-group-create-service-principal-portal\/\">Create new Azure Service Principal using Azure Portal<\/a><\/li>\n<li><a href=\"https:\/\/azure.microsoft.com\/en-in\/documentation\/articles\/role-based-access-control-configure\/\">Role-based access control in the Microsoft Azure portal<\/a><\/li>\n<li>Sample Azure deployment templates from <a href=\"https:\/\/github.com\/Azure\/azure-quickstart-templates\">GitHub quickstart-templates<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Connect\u00a0your Azure subscriptions to VSTS in 3 clicks New improved user experience to setup Azure Resource Manager based service connection in VSTS. Follow the post below to configure\u00a0Visual Studio Team Services to communicate with Azure in order to provision or deploy Azure Resource Manager resources such as virtual machines To setup Azure Service end point [&hellip;]<\/p>\n","protected":false},"author":67,"featured_media":45953,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[224,226,1],"tags":[],"class_list":["post-9941","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-ci","category-devops"],"acf":[],"blog_post_summary":"<p>Connect\u00a0your Azure subscriptions to VSTS in 3 clicks New improved user experience to setup Azure Resource Manager based service connection in VSTS. Follow the post below to configure\u00a0Visual Studio Team Services to communicate with Azure in order to provision or deploy Azure Resource Manager resources such as virtual machines To setup Azure Service end point [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9941","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/users\/67"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/comments?post=9941"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/posts\/9941\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media\/45953"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/media?parent=9941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/categories?post=9941"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/devops\/wp-json\/wp\/v2\/tags?post=9941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}