Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent terraform code for Azure AD SP create-for-rbac?

For an integration, a service defines the following command to run

az ad sp create-for-rbac --role reader --scopes /subscriptions/{subscription_id}

Instead of running the command, I was wondering what the equivalent terraform code for az ad sp create-for-rbac was?

like image 585
StephenG Avatar asked Apr 25 '19 14:04

StephenG


Video Answer


1 Answers

provider "azuread" {
  version = "=0.3.0"
}

resource "azuread_application" "auth" {
  name = "auth"
}

resource "azuread_service_principal" "auth" {
  application_id = "${azuread_application.auth.application_id}"
}

resource "random_string" "password" {
  length = 16
  special = true
  override_special = "/@\" "
}

resource "azuread_service_principal_password" "auth" {
  service_principal_id = "${azuread_service_principal.auth.id}"
  value                = "${random_string.password.result}"
  end_date_relative    = "240h"
}

output "client_secret" {
  value = "${random_string.password.result}"
  description = "Client Secret"
}

provider "azurerm" {
  version = "=1.24.0"
}

data "azurerm_subscription" "primary" {}

data "azurerm_client_config" "current" {}

resource "azurerm_role_assignment" "auth" {
  scope                = "${data.azurerm_subscription.primary.id}"
  role_definition_name = "Reader"
  principal_id         = "${azuread_service_principal.auth.id}"
}
like image 83
StephenG Avatar answered Oct 17 '22 04:10

StephenG