Microsoft Azure CLI Commands | Cloud Init

November 19, 2018

Photo by John Schnobrich on Unsplash

Microsoft Azure CLI Commands | Cloud Init

Gone are the days when you could work in the field of IT without any knowledge of programming. Not only did the configuration of systems manually using the UI not lend itself well to scale, it made it next to impossible to figure out what changes had been made to the environment after enough time had elapsed. The result being that code that worked in a development environment would inevitably fails in production due to the differences in the environment.

What if we could close the gap from when a developer implements new feature to when customers start using the new and improved product. DevOps integrates developer and operations teams in order to improve collaboration and productivity by automating infrastructure, workflows and testing.

Configuration management code could be used to describe how things should be built rather than following a sequence of steps in a document.

Introducing Azure’s Cloud init, a way of installing packages, writing files, configuring users and security during the initial boot process of a VM.

The following configuration file will automatically install nginx, node.js and npm as the Linux VM boots for the first time. It will also configure nginx and start a web server using express.

#cloud-config
package_upgrade: true
packages:
  - nginx
  - nodejs
  - npm
write_files:
  - owner: www-data:www-data
  - path: /etc/nginx/sites-available/default
    content: |
      server {
        listen 80;
        location / {
          proxy_pass http://localhost:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection keep-alive;
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
        }
      }
  - owner: azureuser:azureuser
  - path: /home/azureuser/myapp/index.js
    content: |
      var express = require('express')
      var app = express()
      var os = require('os');
      app.get('/', function (req, res) {
        res.send('Hello World from host ' + os.hostname() + '!')
      })
      app.listen(3000, function () {
        console.log('Hello world app listening on port 3000!')
      })
runcmd:
  - service nginx restart
  - cd "/home/azureuser/myapp"
  - npm init
  - npm install express -y
  - nodejs index.js

Enter sensible-editor cloud-init.txt, select Vi as your code editor, then paste the preceding code and save the file.

Before we can create the virtual machine, we must define a resource group.

az group create --name example --location eastus

You’re going to create a virtual machine like you would normally only this time you pass in the configuration file via the --custom-data flag.

az vm create --resource-group example --name myVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys --custom-data cloud-init.txt

While reading the documentation, I was actually surprised that you cannot open a port in the configuration file like you would with Docker. I digress, you need to open port 80 to allow for HTTP traffic.

az vm open-port --port 80 --resource-group example --name myVM

Now, if you enter the public IP address to your virtual machine in the browser, the server should respond with a Hello World message.

Cory Maklin
_Sign in now to see your channels and recommendations!_www.youtube.com


Profile picture

Written by Cory Maklin Genius is making complex ideas simple, not making simple ideas complex - Albert Einstein You should follow them on Twitter