4. Connect to Linux virtual machine using SSH

In this section of the tutorial, use SSH in a terminal to connect to your virtual machine. SSH is a common tool provided with many modern shells, including the Azure Cloud Shell.

Get your IP address

  1. Get your IP address using the Azure CLI command, az vm list-ip-addresses:

    az vm list-ip-addresses \
      --resource-group rg-demo-vm-eastus \
      --name demo-vm
    

    The results include your public IP address:

    [
       {
        "virtualMachine": {
          "name": "demo-vm",
          "network": {
            "privateIpAddresses": [
              "YOUR-VM-PRIVATE-IP"
            ],
            "publicIpAddresses": [
              {
                "id": "/subscriptions/YOUR-SUBSCRIPTION-ID/resourceGroups/YOUR-RESOURCE-GROUP-NAME/providers/Microsoft.Network/publicIPAddresses/YOUR-RESOURCE-NAME-ip",
                "ipAddress": "YOUR-VM-PUBLIC-IP",
                "ipAllocationMethod": "Static",
                "name": "YOUR-RESOURCE-NAME-ip",
                "resourceGroup": "YOUR-RESOURCE-GROUP-NAME",
                "zone": "1"
              }
            ]
          },
          "resourceGroup": "YOUR-RESOURCE-GROUP-NAME"
        }
      }
    ]
    

Connect with SSH and change web app

Use the same terminal or shell window as with previous steps.

  1. Connect to your remote virtual machine with the following command.

    Replace YOUR-VM-PUBLIC-IP with your own virtual machine's public IP.

    ssh azureuser@YOUR-VM-PUBLIC-IP
    

    This process assumes that your SSH client can find your SSH keys, created as part of your VM creation and placed on your local machine. If you are asked if you want to continue connecting, answer yes. When the connection is complete, the terminal prompt should change to indicate the remote virtual machine.

  2. If you are asked if you are sure you want to connect, answer y or yes to continue.

  3. Use the following command to understand where you are on the virtual machine. You should be at the azureuser root: /home/azureuser.

    pwd
    
  4. The response should be /home/azureuser.

  5. Your web app is in the subdirectory, myapp. Change to the myapp directory and list the contents:

    cd myapp && ls -l
    
  6. You should see contents like, representing the GitHub repository cloned into the virtual machine and the npm package files:

    -rw-r--r--   1 root root   891 Nov 11 20:23 cloud-init-github.txt
    -rw-r--r--   1 root root  1347 Nov 11 20:23 index-logging.js
    -rw-r--r--   1 root root   282 Nov 11 20:23 index.js
    drwxr-xr-x 190 root root  4096 Nov 11 20:23 node_modules
    -rw-r--r--   1 root root 84115 Nov 11 20:23 package-lock.json
    -rw-r--r--   1 root root   329 Nov 11 20:23 package.json
    -rw-r--r--   1 root root   697 Nov 11 20:23 readme.md
    

Install Monitoring SDK

  1. In the SSH terminal which is connected to your virtual machine, install the Azure SDK client library for Application Insights.

    sudo npm install --save applicationinsights
    
  2. Wait until the command completes before continuing.

Add Monitoring instrumentation key

  1. In the SSH terminal, which is connected to your virtual machine, use the Nano editor to open the package.json file.

    sudo nano package.json
    
  2. Add a APPINSIGHTS_INSTRUMENTATIONKEY environment variable to the beginning of your Start script. In the following example, replace REPLACE-WITH-YOUR-KEY with your instrumentation key value.

    "start": "APPINSIGHTS_INSTRUMENTATIONKEY=REPLACE-WITH-YOUR-KEY pm2 start index.js --watch --log /var/log/pm2.log"
    
  3. Still in the SSH terminal, save the file in the Nano editor with control + X.

  4. In the Nano editor, enter Y to save, when prompted.

  5. In the Nano editor, accept the file name when prompted.

  6. Stop PM2, which is a production process manager for Node.js applications, with the following commands:

    sudo npm run-script stop 
    

    The Azure client library is now in your node_modules directory and the key is passed into the app as an environment variable. The next step is to add the required code to index.js.

  7. Restart the app with PM2 to pick up the next environment variable.

    sudo npm start
    

Next step