Azure Service Principal creation step by step approach
Tags: azure, scp, terraform, iac
Published: 2024-03-25 23:37:59

**Step by step approach in creating in Azure Service Principal(SCP)**


1. Open the Azure portal and sign in with your Azure account.
2. Then search for "Microsoft Entra ID" and then select “App registrations” from the submenu.
3. Click on the “New registration” button to create a new app registration.
4. Enter a name for your app registration and select the supported account types. For example, you can choose “Single tenant” if you only want to allow users from your own organization to access the app.
5. Click on the “Register” button to create the app registration.
Once the app registration is created, click on the “Certificates & secrets” option in the left menu and then select the “New client secret” option.
6. Enter a description and expiration date for the client secret, and then click on the “Add” button to create it.
7. Make sure to copy the client secret value somewhere safe, as it will only be shown once and cannot be retrieved later.
8. Finally, go to the resource group you want the service principal to access. When you are there, click on “Access Control (IAM)” and then click on “Add>Add role assignment”.
9.Click on the role you want to assign (i.e. Contributor). Then go to member, select assign access to “User, group, or service principal”, and then “Select member”.
10.Type the name of the service principal and then select it. Afterward, click on “Review + assign”.

###################################################################

Step 1: Open the Azure portal and sign in with your Azure account.
Then search for "Microsoft Entra ID" and then select “App registrations” from the submenu.


Step 2 :

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3f33icbkxqfrzo03md8.png)

Step 3:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkytjcxutbwep9z5tjji.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlp85hec5bk2qn8e0xeb.png)


Step4:
Click on Certificates & Secrets in the left hand menu and then click on  New Client Secret


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smfvfl0ksgegr7cea11r.png)

Step5: Enter description, select expiry then click on Add


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cbsvii1y3bzodv622llz.png)



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7qatftasnjio1zh6n1s.png)

Make sure to copy the secret value and it wont be shown once again.


Step6 : Select the resource group 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ayi0bouokvlk3v5rqvaf.png)

Step7: Click on  IAM -> Click on Add


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m3wcetqxa40ml023dfpx.png)

Step8:Click on the role you want to assign (i.e. Contributor). 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43zkef9jt2aevp0rzq75.png)

Step9:Then go to member, select assign access to “User, group, or service principal”, and then “Select member” .Type the name of the service principal "learnazurewithsrini" and then select it. Afterward, click on “Review + assign”.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0abulutrzd0e6rh7w5ur.png)

###################################################################
We need following details for azure + terraform authentication

1.subscription_id   = "<azure_subscription_id>"
2.tenant_id          = "<azure_subscription_tenant_id>"
3.client_id           = "<service_principal_appid>"
4.client_secret       = "<service_principal_password>"

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ualg6aa83whfxho6rrb.png)

**Terraform Example :**
Step1 : Create a file provider.tf and copy the below code

**Terraform sample code:**

```terraform
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.0"
    }
  }
}

provider "azurerm" {
  features {}

  subscription_id   = "<azure_subscription_id>"
  tenant_id         = "<azure_subscription_tenant_id>"
  client_id         =  "<service_principal_appid>"
  client_secret     = "<service_principal_password>"
}


```
Step2:create a file main.tf and copy the below mentioned code

```

resource "azurerm_resource_group" "az400-training" {
  location = var.resource_group_location
  name     = "az-400-training"
}

```


Step3:create a file,output.tf and copy the below mentioned code

```
output "resource_group_name" {
  value = azurerm_resource_group.az400-training.name
}
```

Step4:Run the terraform commands

`terraform init
`
`terraform plan
`
`terraform apply --auto-approve`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5v4vper8ifp5c5invrgd.png)

Conclusion : Discussed about creating Service principle in Azure and used SCP to authenticate the azure and provision azure resources.

💬 If you enjoyed reading this blog post and found it informative, please take a moment to share your thoughts by leaving a review and liking it 😀 and follow me in [dev.to ](https://dev.to/srinivasuluparanduru), [linkedin](https://www.linkedin.com/in/srinivasuluparanduru/) and [buy me a coffee](https://www.buymeacoffee.com/srinivasuluparanduru)