Deploy an SSH Enabled VM with a Template in Go

Автор: Mariana Cardoso
Последнее обновление: 01.02.2017
Редактирование в GitHub

This example demonstrates how to use Go to deploy an Azure Resource Manager Template. 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. Clone the repository.

    git clone https://github.com:Azure-Samples/virtual-machines-go-manage.git
    
  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
    

ParamaterLink instructions

  1. Create an Azure Key Vault using the Azure CLI azure keyvault create --vault-name templateSampleVault --resource-group MyResourceGroup --location westus

  2. Add the public SSH as secret in the vault using the Azure CLI

    azure keyvault secret set --vault-name templateSampleVault --secret-name sshKeyData --value "yourPublicSSHkey"
    
  3. Reference correctly the Key Vault in the parameter file. In vmDeploymentParameter.json, replace subscription_id, resource_group and vault_name inside the "id" value with the correct values.

What does example.go do?

A rescorce group is needed to be able to deploy a template.

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

The sample then gets the template and its parameters. Both template and parameters can be set with a *map[string]interface{} or with a link to a json file.

    deployment := resources.Deployment{
        Properties: &resources.DeploymentProperties{
            Mode: resources.Incremental,
        },
    }

    fmt.Println("\tGet template")
    if useTemplateLink {
        fmt.Println("\tUsing template link")
        deployment.Properties.TemplateLink = &resources.TemplateLink{
            URI:            to.StringPtr("https://raw.githubusercontent.com/Azure-Samples/resource-manager-go-template-deployment/master/vmDeploymentTemplate.json"),
            ContentVersion: to.StringPtr("1.0.0.0"),
        }
    } else {
        fmt.Println("\tUsing local template")
        template, err := parseJSONFromFile("vmDeploymentTemplate.json")
        onErrorFail(err, "parseJSONFromFile failed")
        deployment.Properties.Template = template
    }

    fmt.Println("\tGet parameters")
    if useParameterLink {
        fmt.Println("\tUsing parameter link")
        deployment.Properties.ParametersLink = &resources.ParametersLink{
            URI:            to.StringPtr("https://raw.githubusercontent.com/Azure-Samples/resource-manager-go-template-deployment/master/vmDeploymentParameter.json"),
            ContentVersion: to.StringPtr("1.0.0.0"),
        }
    } else {
        fmt.Println("\tUsing local parameters")
        parameter := map[string]interface{}{}
        // The paramaters map must have this format {key: {"value": value}}.
        addElementToMap(&parameter, "dnsLabelPrefix", dnsPrefix)
        addElementToMap(&parameter, "vmName", "azure-deployment-sample-vm")
        sshKey, err := getSSHkey(sshKeyPath)
        onErrorFail(err, "getSSHkey failed")
        addElementToMap(&parameter, "sshKeyData", sshKey)
        deployment.Properties.Parameters = &parameter
    }

The template is validated...

    validate, err := deploymentsClient.Validate(groupName, deploymentName, deployment)
    onErrorFail(err, "Validate failed")
    if validate.Error == nil {
        fmt.Println("Deployment is validated! Template is syntactically correct")
    } else {
        printValidationError(validate)
        os.Exit(1)
    }

Finally, the template is deployed!

    _, err := deploymentsClient.CreateOrUpdate(groupName, deploymentName, deployment, 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 opencode@microsoft.com with any additional questions or comments.