3

I am trying to run Golang Azure SDK code to get a list of RGs in my subscriptions but I am getting the following error:

2022/01/22 20:25:58 MSI not available exit status 1


import (
    "context"
    "fmt"
    "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
    "github.com/Azure/go-autorest/autorest/azure/auth"
    "github.com/Azure/go-autorest/autorest/to"
    "log"
    "os"
)

func main() {
    authorize, err := auth.NewAuthorizerFromEnvironment()

    if err != nil {
        log.Fatal(err)
    }

    subscriptionID := os.Getenv("AZURE_SUB_ID")

    //Read resource groups
    resGrpClient := resources.NewGroupsClient(subscriptionID)
    resGrpClient.Authorizer = authorize

    //Read resources within the resource group
    resClient := resources.NewClient(subscriptionID)
    resClient.Authorizer = authorize

    for resGrpPage, err := resGrpClient.List(context.Background(), "", nil); resGrpPage.NotDone(); err = resGrpPage.Next() {

        if err != nil {
            log.Fatal(err)
        }

        for _, resGrp := range resGrpPage.Values() {
            fmt.Println("Resource Group Name: ", to.String(resGrp.Name))
            resList, _ := resClient.ListByResourceGroup(context.Background(), to.String(resGrp.Name), "", "", nil)

            for _, res := range resList.Values() {
                fmt.Println("\t- Resource Name: ", to.String(res.Name), " | Resource Type: ", to.String(res.Type))
            }
        }

    }

}

I am using Goland and trying to run the app in WSL Ubuntu

Renm
  • 717
  • 3
  • 10
  • 20
  • I modified the imports as they should have been the following: "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources" "github.com/Azure/go-autorest/autorest/azure/auth" "github.com/Azure/go-autorest/autorest/to" – Renm Jan 22 '22 at 20:07

2 Answers2

2

Please read this documentation Use environment-based authentication

You have a couple of options and they need specific environments variables to be present. In my case, I use Client credentials so I need to have these 3 envs present when I run my code.

  1. AZURE_CLIENT_ID
  2. AZURE_CLIENT_SECRET
  3. AZURE_TENANT_ID
200 not ok
  • 134
  • 10
  • Yup got the same error, thanks for the info. The variables must explicitly be available as environment variables. – cristobal Jan 12 '23 at 21:49
1

Solution is to use auth.NewAuthorizerFromCLI(), as auth.NewAuthorizerFromEnvironment does not use the Cli and MSI stands for managed system identity.

Renm
  • 717
  • 3
  • 10
  • 20
  • this might not work for some other people, whose code run in an environment where there is no Azure CLI available. – 200 not ok Jan 27 '22 at 09:32