The Azure Code Samples are currently available in English

Manage Azure resources and resource groups with Go

Dernière mise à jour : 18/01/2017
Modifier sur GitHub

This package demonstrates how to manage resources and resource groups using Azure SDK for Go.

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.

On this page

Run this sample

  1. If you don't already have it, install Go 1.7.

  2. Get the sample. You can use either

    go get


    git clone
  3. Install the dependencies using glide.

    cd virtual-machines-go-manage
    glide install
  4. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

  5. Set the following environment variables using the information from the service principle that you created.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}

    [AZURE.NOTE] On Windows, use set instead of export.

  6. Run the sample.

    go run example.go

What does example.go do?

Create resource group

    rg := resources.ResourceGroup{
        Location: to.StringPtr(location),
    _, err := groupsClient.CreateOrUpdate(groupName, rg)

Update the resource group

The sample updates the resource group with tags.

    rg.Tags = &map[string]*string{
        "who rocks": to.StringPtr("golang"),
        "where":     to.StringPtr("on azure"),
    _, err := groupsClient.CreateOrUpdate(groupName, rg)

List resource groups in subscription

groupsList, err := groupClient.List("", nil)

Create a generic resource

In this sample, a Key Vault is created, but it can be any resource.

    genericResource := resources.GenericResource{
        Location: to.StringPtr(location),
        Properties: &map[string]interface{}{
            "sku": map[string]string{
                "Family": "A",
                "Name":   "standard",
            "tenantID":             tenantID,
            "accessPolicies":       []string{},
            "enabledForDeployment": true,
    _, err := resourcesClient.CreateOrUpdate(groupName, namespace, "", resourceType, resourceName, genericResource, nil)

Update the resource with tags

    gr.Tags = &map[string]*string{
        "who rocks": to.StringPtr("golang"),
        "where":     to.StringPtr("on azure"),
    _, err := resourcesClient.CreateOrUpdate(groupName, namespace, "", resourceType, resourceName, gr, nil)

List resources inside the resource group

    resourcesList, err := groupsClient.ListResources(groupName, "", "", nil)

Export resource group template to a json file

Resources can be exported into a json file. The asterisk * indicates all resources should be exported. Later, the json file can be used for template deployment.

    // The asterisk * indicates all resources should be exported.
    expReq := resources.ExportTemplateRequest{
        Resources: &[]string{"*"},
    template, err := groupsClient.ExportTemplate(groupName, expReq)
    onErrorFail(err, "ExportTemplate failed")

    prefix, indent := "", "    "
    exported, err := json.MarshalIndent(template, prefix, indent)
    onErrorFail(err, "MarshalIndent failed")

    fileTemplate := "%s-template.json"
    fileName := fmt.Sprintf(fileTemplate, groupName)
    if _, err := os.Stat(fileName); err == nil {
        onErrorFail(fmt.Errorf("File '%s' already exists", fileName), "Saving JSON file failed")
    ioutil.WriteFile(fileName, exported, 0666)

Delete a generic resource

    _, err := resourcesClient.Delete(groupName, namespace, "", resourceType, resourceName, nil)

Delete the resource group

    _, err := groupsClient.Delete(groupName, nil)

More information

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact with any additional questions or comments.