I tried the site "Deploy Azure virtual machine from Go"

site"Azure for Go developers| Microsoft Docs"QuickStart"""DeployanAzurevirtualmachinefromGo|MicrosoftDocsI actually tried to run it on macOS.

Prerequisites

In the text, specific values ​​and items such as ID are replaced with Japanese text or omitted.

Launch Azure Cloud Shell (https://docs.microsoft.com/ja-jp/azure/developer/go/azure-sdk-qs-vm#launch-azure-cloud-shell)

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": {}
}

Install Azure SDK for Go (https://docs.microsoft.com/ja-jp/azure/developer/go/azure-sdk-qs-vm#install-the-azure-sdk-for-go)

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/...

Create Service Principal

Login to Azure

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"
    }
  }
]

Creating a service principal

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",
  ...
}

Service Principal Settings

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 Code

Get the sample code as per the documentation.

% go get -u -d github.com/azure-samples/azure-sdk-for-go-samples/quickstarts/deploy-vm/...

Code Execution (https://docs.microsoft.com/ja-jp/azure/developer/go/azure-sdk-qs-vm#running-the-code)

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. ** **

Confirm the creation of each component

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

Log in to the virtual machine

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

Log out of the virtual machine.

quickstart@QuickstartVM:~% exit
logout
Connection to IP address closed.

Cleaning up

Delete resource group

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
---------------------------------  -------------  ---------

Deletion of service principal

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

Code details (https://docs.microsoft.com/ja-jp/azure/developer/go/azure-sdk-qs-vm#code-in-depth) and later

--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

I tried the site "Deploy Azure virtual machine from Go"
I tried hitting the Qiita API from go
I tried calling the prediction API of the machine learning model from WordPress
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
I tried to detect the iris from the camera image
I tried scraping the advertisement of the pirated cartoon site
I tried to compress the image using machine learning
I tried to solve the virtual machine placement optimization problem (simple version) with blueqat
I tried using the Python library from Ruby with PyCall
I tweeted from the terminal!
I tried face recognition from the video (OpenCV: python version)
I tried the changefinder library!
[Machine learning] I tried to summarize the theory of Adaboost
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to get various information from the codeforces API
I tried to make a site that makes it easy to see the update information of Azure
I tried sending an email from the Sakura server with flask-mail
I tried installing MySQL on a Linux virtual machine on OCI Compute
I tried to cut out a still image from the video
I tried the TensorFlow tutorial 1st
I tried the Naro novel API 2
I tried machine learning with liblinear
I tried the TensorFlow tutorial 2nd
I tried Grumpy (Go running Python).
I tried task queuing from Celery
I tried the Naruro novel API
I tried to move the ball
I tried using the checkio API
I tried to estimate the interval.
I tried to execute SQL from the local environment using Looker SDK
I tried LeetCode every day 26. Remove Duplicates from Sorted Array (Python, Go)
I tried to learn the angle from sin and cos with chainer
From user registration of Microsoft Azure to creation of virtual machine (free tier)
I tried to visualize the model with the low-code machine learning library "PyCaret"
I tried the TensorFlow tutorial MNIST 3rd
I tried scraping conversation data from Askfm
I tried using Azure Speech to Text.
I tried the asynchronous server of Django 3.0
I tried to summarize the umask command
I tried tensorflow for the first time
I tried to recognize the wake word
I tried the OSS visualization tool, superset
I tried using UnityCloudBuild API from Python
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
Python: I tried the traveling salesman problem
I tried playing with the image with Pillow
I tried using Headless Chrome from Selenium
I tried the Python Tornado Testing Framework
I tried using the BigQuery Storage API
I tried to summarize the relationship between probability distributions starting from the Bernoulli distribution
I tried to organize the evaluation indexes used in machine learning (regression model)
[IBM Cloud] I tried to access the Db2 on Cloud table from Cloud Funtions (python)
I tried to predict the presence or absence of snow by machine learning.
I tried running two Jetson Nano hardware PWMs from the Jetson.GPIO Python library.
I tried to predict the change in snowfall for 2 years by machine learning
I tried to process and transform the image and expand the data for machine learning
I couldn't escape from the futon, so I made a fully automatic futon peeling machine.
[Python] I tried to get the type name as a string from the type function
[Python] I tried the same calculation as LSTM predict with from scratch [Keras]