Create Azure Service Principal
Last updated
Last updated
Creating a Service Principal in Azure and retrieving its Client ID and Secret involves several steps. Below is a detailed, step-by-step guide:
1. Open the Azure Portal ().
2. Log in with your Azure credentials.
1. In the left-hand navigation pane, select Azure Active Directory.
2. Under the Manage section, click App registrations.
1. Click New registration at the top of the page.
2. Fill in the required fields:
• Name: Enter a descriptive name for the Service Principal (e.g., “MyServicePrincipal”).
• Supported account types: Choose who can access this application. Typically, select Accounts in this organizational directory only.
• Redirect URI: Leave this blank for Service Principal creation unless required by your application.
3. Click Register.
1. Once the registration is complete, you will be redirected to the application’s overview page.
2. Note down the Application (client) ID and Directory (tenant) ID. You will need them later.
1. In the application’s Manage section, select Certificates & secrets.
2. Under the Client secrets tab, click New client secret.
3. Provide a description (e.g., “DefaultSecret”) and choose an expiration duration (e.g., 1 year, 2 years, or custom).
4. Click Add.
5. Copy the generated value of the secret. This is the Client Secret.
Note: You will not be able to view this secret again once you leave the page, so save it securely.
2. In the left-hand menu, click Roles and administrators > + New custom role.
• Name: Enter a name like “Virtual Machine Manager Role.”
• Description: Provide a brief description, e.g., “Allows listing, getting, starting, and stopping Virtual Machines.”
• Click + Add permissions.
• Search for Microsoft.Compute/virtualMachines.
• Select the following actions:
Microsoft.Compute/virtualMachines/read
Microsoft.Compute/virtualMachines/start/action
Microsoft.Compute/virtualMachines/deallocate/action
• Click Add.
Navigate to the Resource Group:
• Specify the scope where this role can be assigned:
• Subscription: Assign it at the subscription level if you want it to apply to all resource groups and VMs.
• Resource Group: Assign it at the resource group level for more granular control. (Here is the most important part, as we have to specify the Resource Groups for this Credential. It is a Best Practice to create Service Principals and roles for 1 to 2-3 Resource Groups, to limit the overgevernance.
• Click + Add Assignable Scope and select the relevant scope.
• Click Review + create.
• Click Create.
1. Navigate to the resource you want the Service Principal to access (e.g., a subscription, resource group, or specific resource).
2. Go to the Access control (IAM) section of the resource.
3. Click Add role assignment.
4. In the Role field, select the Custom Role created in the Step 7
5. In the Assign access to field, select User, group, or Service Principal.
6. Search for the name of the Service Principal (the application name you created).
7. Select it, and click Save.