services | platforms | author |
---|---|---|
azure-resource-manager |
go |
mcardosos |
#Deploy an SSH Enabled VM with a Template in Go
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
-
If you don't already have it, install Go 1.7.
-
Clone the repository.
git clone https://github.com:Azure-Samples/virtual-machines-go-manage.git
-
Install the dependencies using glide.
cd virtual-machines-go-manage glide install
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
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 ofexport
. -
Run the sample.
go run example.go
-
Create an Azure Key Vault using the Azure CLI
azure keyvault create --vault-name templateSampleVault --resource-group MyResourceGroup --location westus
-
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"
-
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.
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(¶meter, "dnsLabelPrefix", dnsPrefix)
addElementToMap(¶meter, "vmName", "azure-deployment-sample-vm")
sshKey, err := getSSHkey(sshKeyPath)
onErrorFail(err, "getSSHkey failed")
addElementToMap(¶meter, "sshKeyData", sshKey)
deployment.Properties.Parameters = ¶meter
}
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)
- Azure Resource Manager overview - Template deployment
- Create a template deployment
- Resource Manager template walkthrough
- Pass secure values during deployment
- Using linked templates with Azure Resource Manager
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.