2
votes

I'm trying to execute a sample terraform plan given below.

# Configure the Microsoft Azure Provider
provider "azurerm" {
  subscription_id = "..."
  client_id       = "..."
  client_secret   = "..."
  tenant_id       = "..."
}

    # Create a resource group
    resource "azurerm_resource_group" "production" {
        name     = "production"
        location = "West US"
    }

    # Create a virtual network in the web_servers resource group
    resource "azurerm_virtual_network" "network" {
      name                = "productionNetwork"
      address_space       = ["10.0.0.0/16"]
      location            = "West US"
      resource_group_name = "${azurerm_resource_group.production.name}"

      subnet {
        name           = "subnet1"
        address_prefix = "10.0.1.0/24"
      }

      subnet {
        name           = "subnet2"
        address_prefix = "10.0.2.0/24"
      }

      subnet {
        name           = "subnet3"
        address_prefix = "10.0.3.0/24"
      }
    }`enter code here`

I followed [1] to generate credentials via creating Active Directory application and used the correct subscription_id, client_id, client_secret, tenant_id in the above plan and executed 'terraform plan' against it. But I'm getting below error.

Error refreshing state: 1 error(s) occurred:

  • Credentials for acessing the Azure Resource Manager API are likely to be incorrect, or the service principal does not have permission to use the Azure Service Management API.

[1] https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/

Any idea on this?

1
You need automated deployment rights, it's not because you can create a VM in the portal that you can using terraform. You need to get access to this first.Glenn Plas

1 Answers

2
votes

It seems like in terraform documentation, they haven't included the step of assigning role to the service principal. Follow these steps and it works.

1) Create the service principal through Azure CLI by following this link https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal-cli/ which assigns the role as well to the service principal 2) Go to Azure RM portal-->Active Directory -->App registration --> Create the key 3) Use the appropriate values from above in .tf file.

Then run the command terraform plan.