site"Azure for Go developers| Microsoft Docs"QuickStart"""DeployanAzurevirtualmachinefromGo|MicrosoftDocsI actually tried to run it on macOS.
In the text, specific values and items such as ID are replaced with Japanese text or omitted.
I installed and worked with the Azure CLI locally instead of using the Azure Cloud Shell.
The installation procedure of Azure CLI is posted in the next article.
--Azure SDK for Go setup (macOS) --Azure CLI
Check the version of Azure CLI. 2.0.28 or later is required for this tutorial.
% az version
{
"azure-cli": "2.17.1",
"azure-cli-core": "2.17.1",
"azure-cli-telemetry": "1.0.6",
"extensions": {}
}
Check the version of Go. Azure SDK for Go requires 1.8 (1.9 for Azure Stack profile) or later.
% go version
go version go1.15.6 darwin/amd64
Get & update the latest version of Azure SDK for Go.
% go get -u -d github.com/Azure/azure-sdk-for-go/...
You must be logged in to Azure before running the create command.
Check if you are logged in.
% az account show
Message displayed when not logged in
Please run 'az login' to setup account.
Message displayed when logged in
{
"environmentName": "Environment name",
"homeTenantId": "Tenant ID",
"id": "Subscription ID",
...
"user": {
"name": "mail address",
"type": "user"
}
}
If you are not logged in, log in. A web browser will launch and you will be prompted to log in to Azure. Enter your Azure account and password to log in.
% az login
The default web browser has been opened at https://login.microsoftonline.com/common/oauth2/authorize. Please continue the login in the web browser. If no web browser is available or if the web browser fails to open, use device code flow with `az login --use-device-code`.
You have logged in. Now let us find all the subscriptions to which you have access...
If the login is successful, the web browser will transition to the document site, and the terminal will output the login information. It's okay to close your web browser.
[
{
"cloudName": "Cloud name",
"homeTenantId": "Tenant ID",
"id": "Subscription ID",
...
"user": {
"name": "mail address",
"type": "user"
}
}
]
Create a service principal and store it in the file "quickstart.auth".
% az ad sp create-for-rbac --sdk-auth > quickstart.auth
Check the contents of quickstart.auth.
% cat quickstart.auth
{
"clientId": "Client ID",
"clientSecret": "Client secret",
"subscriptionId": "Subscription ID",
"tenantId": "Tenant ID",
...
}
Set the environment variable "AZURE_AUTH_LOCATION" to the file path of "quickstart.auth".
% export AZURE_AUTH_LOCATION=/Users/username/quickstart.auth
Make sure that the environment variable contains the value.
% echo $AZURE_AUTH_LOCATION
/Users/username/quickstart.auth
Get the sample code as per the documentation.
% go get -u -d github.com/azure-samples/azure-sdk-for-go-samples/quickstarts/deploy-vm/...
Change to the sample code directory.
% cd $GOPATH/src/github.com/azure-samples/azure-sdk-for-go-samples/quickstarts/deploy-vm
The contents of the directory are as follows:
% ls -1
README.md
main.go
vm-quickstart-params.json
vm-quickstart-template.json
In my case, before running the sample code, I checked if I could compile successfully with the acquired Azure SDK for Go and the sample code group.
% go build main.go
The main.go file contains the main () function, so compiling produces an executable binary "main" file.
% ls -1
README.md
main
main.go
vm-quickstart-params.json
vm-quickstart-template.json
If you want to run the sample code in the documented way:
% go run main.go
To run the created executable binary:
% ./main
The execution log is output.
2021/01/10 14:38:43 Created group: GoVMQuickstart
2021/01/10 14:38:43 Starting deployment: VMDeployQuickstart
2021/01/10 14:39:49 Completed deployment VMDeployQuickstart: Succeeded
2021/01/10 14:39:49 Log in with ssh: quickstart@IP address, password:password
The above result was obtained in the second run.
At the first time, I had to wait a few minutes for "Starting deployment" on the second line, so I stopped by pressing [Ctrl] + [C].
Apparently the second line was getting the virtual machine from the Marketplace, which took a long time.
** After this, the information on the 4th line will be used, so be careful not to accidentally close the terminal. ** **
For learning purposes, I logged in to the "Azure Portal" and visually checked whether each component was created.
--Resource group --Service Principal --Virtual machine --Virtual network
Try logging in to the virtual machine with an SSH connection using the value on the 4th line of the execution log to see if the virtual machine has been created. You will be asked for the password on the way, so enter the password and press the [Enter] key.
% ssh quickstart@IP address
The authenticity of host 'IP address(IP address)' can't be established.
ECDSA key fingerprint is SHA256:SHA256 value.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'IP address' (ECDSA) to the list of known hosts.
quickstart@IP address's password:password
Welcome to Ubuntu 16.04.7 LTS (GNU/Linux 4.15.0-1098-azure x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
0 packages can be updated.
0 updates are security updates.
The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
Try running the umane command on the virtual machine.
quickstart@QuickstartVM:~% uname -a
Linux QuickstartVM 4.15.0-1098-azure #109~16.04.1-Ubuntu SMP Wed Sep 30 18:53:14 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
You can see that the local machine is macOS and a Linux virtual machine has been created.
Log out of the virtual machine.
quickstart@QuickstartVM:~% exit
logout
Connection to IP address closed.
Make sure that the target resource group exists.
% az group list --output table
Name Location Status
--------------------------------- ------------- ---------
GoVMQuickstart eastus Succeeded
Perform a resource group deletion.
% az group delete -n GoVMQuickstart
Make sure that the target resource group has been deleted.
% az group list --output table
Name Location Status
--------------------------------- ------------- ---------
In the file "quickstart.auth", copy the value "client ID" of the item "clientId".
% cat ~/quickstart.auth
{
"clientId": "Client ID",
...
}
Set the value of clientId in the environment variable "CLIENT_ID_VALUE".
% export AZURE_AUTH_LOCATION=Client ID
Make sure that the environment variable contains the value.
% echo $CLIENT_ID_VALUE
Client ID
Performs a service principal deletion.
% az ad sp delete --id ${CLIENT_ID_VALUE}
Removing role assignments
--Source code: azure-sdk-for-go-samples/quickstarts/deploy-vm at master · Azure-Samples/azure-sdk-for-go-samples · GitHub
I went through the source code and thought that I would put more effort into explaining it, but since the code explanation in the official document is detailed, there is no turn! (Great!)
Recommended Posts