Generating an SSH Key and Using it on Azure

SSH KEYS allow us to connect to VMs without using passwords but by passing a private key that can be managed by you or your organization.

For more about SSH

There are three parts to this tutorial:
A. Generate an SSH Key
B. Create a VM in Azure that uses the public key
C. Connect to VM using SSH keys

Prerequisites:
Bash
ssh-keygen ($ info ssh-keygen to learn more)
An Azure Subscription

A. Generate an SSH Key

Open bash and enter:
$ ssh-keygen -t rsa -b 2048 -C "Ubuntu@azure-server"
Keyname: server-key
Passphrase: somethingMemorable

Copy the contents of server-key.pub
$ cat server-key.pub

Should look like this:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDMlUr7PCEdBmCVZHG5RqI8i7GgYAzd2G/FZ987XXa63vnqxZmZogVmmXrTnBHeM6oDv7v7g495CiiiINhJbGR4o7t4agiHOM43egDv7BbiViTlfVr3y5AxLUvRwHnC3egl8ABVX1anfXXR73x7IS3YRNWkh6gXtlhImw8UKG04UoZEmWB9BLt53lk/9c3Hxz22YZarzImrpQYy1XEUZ096B9mK/Fe+/McH78ZHUpXEgOZBIDP5KdqPk5XKznpwUDJ4/SPXPEWWCCjQ8gOoTFcFMaiMnXp5o5Udsi/DFO1TS/t8BeCRymkr5tdPvzexjkjkjkjkjkjkjkjkjkjkjkjt Ubuntu@azure-server

Here’s what it looks like for me:
keygenandcat

Cool and you’ll also notice that there’s another file in that same directory – server-key
$ ls | grep server
Here’s what that looks like for me:
lskeys

Now that we have our public and private keys let’s get our VM setup.

B. Create a VM in Azure that uses the public key

1. Go to the Azure Portal

2. Select New -> Search: Ubuntu Server
(I’m using 14.04 this time)
selectubuntu1404

3. Make sure you’ve selected Resource Manger and click Create:
resourcemanagecreate

4. Now configure the basics per our ssh-keygen parameters
Name: azure-server
VM Disk Type: Up To You
User name: Ubuntu
Authentication type: SSH public key
SSH public key: Paste the results of $ cat server-key.pub
Subscription: Depends how you want to pay for the server
Resource Group: Up to you – I’m going to create a new one so I can quickly delete it.
Location: Up to you

Should look like this:
basicsconfigurationforssh

Then select OK to go to the next section.

5. Choose Virtual Machine Size
I’m going with the smallest VM for testing.
You can also view all different VM sizes to find the right one for your use case.
pickvmsize

6. Configure optional Features
Setting the Storage account name to something you’ll remember easily is good.
And if you want to configure ports now you can select Network Security group to allow ports specific traffic.
Here’s what that looks like:
optional-azure-settings
Click okay to continue to the Summary of your VM.

Here’s our summary:
summary-click-okay-to-create-vm

Select okay to start your VM.

7. Wait for it to be ready.
Dashboard will have an icon and you’ll get a notification when its ready:
waiting-for-vm-to-spin-up-from-dashboard

8. Once ready select on it to see the overview and the IP address.
Should look like this:
vm-overview-ip

Great! We have a VM and its IP address. Lets use our Private SSH key to connect.

C. Connect to VM using SSH Keys

1. Open bash to file location you created the keys in.
Make sure they’re there:
$ ls | grep server

2. Enter this command to use SSH to connect:
$ ssh -i server-key Ubuntu@52.183.31.11 -v
or more generally
$ ssh -i keyname username@ip.address -v
Make sure you’re using server-key and not server-key.pub
Tip: -v is the verbose option. Not necessary, but it helps to see if the key is being accepted

3. Great, now accept the certificate, and enter your memeroablePassphrase
Whole thing should look like this:
ssh-using-key-to-inbash

And you’ll be in the terminal of your VM:
in-the-terminal-of-the-vm

Yay!
You’ve got the key, you’ve got the VM, now put it to work!
Flask on Ubuntu
Node on Ubuntu
Mongo on Ubuntu
Connecting to VMs from Azure Web Apps

Let me know if you have any questions by posting in the comments below!

These people just want High Fives!
These people just want High Fives!

Connecting your Azure Web App to a Database using Application Settings

You just built an application that connects with a Database on your local machine, but now you want to share it with the world. But you don’t want to share everything about your application with the world.

Azure web apps supports a number of different web platforms, but to effectively build while maintaining code on GitHub we need to keep configuration details of our app private including the connection string for our database.

In this case we’re going to be using a Node JS application connected to a Mongo Database hosted on a Virtual Machine.

Before we get started…
Do you have a MongoDB up and running?
Nope. – Read this post!
Yep! – Great keep going!

For reference, I’m starting with a chat application originally written by fellow evangelist Rami Sayar for an excellent Node Course he taught recently.

Prereqs:
Git
Azure Subscription
Connection string for a DB in Mongo – mine looks like this:
mongodb://40.83.182.555:27017/test

The Steps:
1. Clone or fork the repository
2. Create an App Service
3. Connect App Service to Repository
4. Add app settings in Azure Portal
5. Try it out!

1. Clone or fork the repository

The project I’m using for this demonstration is found here: Github
Now clone the project to your local machine, or fork it in GitHub.

Take a quick look at these files:
– package.json : checkout out the dependencies of the project, notice how we’re using the official mongodb driver
– mongoClientTest.js and dbTest.js : provide other examples/test files to run, just substitute your ip address of where mongo is being hosted to make sure you’re able to connect.
– app.js : this is the main file that will be run by Azure Web Apps (app.js or server.js will automatically be started by the cloud when deployed). Also, in app.js you’ll see we set ‘db’ using process.env app.set('db', process.env.DB ); – we’ll be using that in step 4.

Once comfortable with the files locally or in GitHub we’re ready to spin up our App Service in Azure.

2. Create an App Service

App Service is a PaaS service offered by Azure to make deployment of website/web services simple and easy to maintain, develop against, and scale.

To begin select the ‘Plus’ button in the upper left corner and select web+mobile, then select Web App:
selectnewwebapp

Now configure your web app:
configurewebapp

You may have to create a app service plan and resource group if you haven’t created one yet, for more info on this portion of the architecture click here and here

You’ll see this icon on your dashboard while the resource is spinning up:
deploying

Once running the portal should automatically take you to that resource, if not you’ll see it on your dashboard:
mongoappondashboard

Here’s the overview pane and the items we’ll be using in the rest of this tutorial highlighted:
webappoverview

3. Connect App Service to Repository

Now that we have an app service, let’s connect it to our repository.
There are a number of options for deployment.

In this case we’re going to deploy from GitHub, but we could have also used a local repository by adding Azure as a remote repo.

Azure will detect changes made to the repository on GitHub and redeploy the project. This makes deployments during hackathons super easy.

Select Deployment options from the overview pane -> Choose Source -> GitHub:
deployfromgithub

Note: You can also select Local Git Repository if you’ve liked to keep your code off of GitHub and add Azure as a remote repository git add remote azure "http://git.asdfgafdgasdfgasdf" – will be displayed in overview portal once initialized.

You’ll then be asked to login to GitHub so it can access the list of your repos.
Select your forked repo or the cloned repo you’ve committed to GitHub:
authrorize-andselectyourproject

Once selected you’ll see a notification in the top right corner with a loading bar for the deployment:
waitingfordeployment

Once that’s finished, you have code ready to roll in the cloud.
Azure has automatically installed the packages listed in your package.json and found the file named server.js to deploy. Next let’s configure it to talk to Mongo!

4. Add App Settings in Azure Portal

Now we have a project connected and deployed on Azure, but it won’t store the data anywhere, because a connection string hasn’t been included in the environment variables (aka application settings).

To add our monogodb to the project we just need to include the variable name and the connection string.
In our project, as noted before, we set db using process.env.DB

Select application settings and scroll down to until you see app settings then enter into the two empty fields “DB” and the connection string mentioned earlier: mongodb://40.83.182.555:27017/test
Like this:
appsettingpane

Then hit save!

Go back to your website and try entering some chatter!

Try it out!

If you visited the page it should look something like this:
finishedproduct

In the middle of the above screen shot you can see a terminal with a connection to the VM and same data in the MongoDB.

If you’re running into issues try connecting using dbTest.js or mongoClientTest.js after you add the specific IP for your VM hosting mongo.

Hope this made it super simple to connect your application to a database using Azure Web Apps.
Please let me know if you run into any issues in the comments below.

When Brett Stateham comes to town...
When Brett Stateham comes to town…

Running Mongo on Ubuntu Virtual Machine in Azure

You just setup a VM and you want to store data!

You checked out Document DB, but decided Mongo was more your style.

Prereqs:
An Ubuntu Virtual Machine in Azure

Let’s get started!

There are four steps:
1. Open the appropriate port on Azure
2. Install Mongo
3. Configure Mongo to connect to all external IPs
4. Connect your application

1. Open the appropriate port on Azure

Go to your virtual machine’s landing page and select the resource group in the top left corner:
vmnetworkselection

Resource groups are the way Azure breaks down how our VM interacts with the internet, other VMs, storage, and public/private networks.

To open the port we need to change our Network Security Group, which is represented by the shield and underlined in the screenshot above.

Then, once you’ve selected the Network Security Group, select Settings -> Inbound Security rules

This will allow us to open up our VM to the Public Internet via a port that we'll connect our client side application to.
This will allow us to open up our VM to the Public Internet via a port that we’ll connect our client side application to.

You’ll notice that SSH is already included, that’s what we’re using in our terminal. You may also have other ports opened if you’ve followed some of my other posts.

We’re going to create a new Inbound Security Rule called MongoPort where we’ll set the Destination port range to 27017 (the default port for MongoDB)

You can see the configuration pane in the screenshot above identified as item 3.

Once you hit ‘Okay’ or ‘Save’ the port will be opened in a couple seconds.

You should see a notification in the top right corner once completed. Now the port is available to the open internet, but Mongo isn’t installed or configured to be listening at that port. So let’s get to it.

2. Install Mongo

Installing Mongo on Ubuntu is easy and well documented by the MongoDB group. Just enter a few commands and you’ll be up and running in no time.

I followed the directions provided by docs.mongo.com –
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

Make sure you’re following along with the directions for your specific instance of Ubuntu.

To check your version of Ubuntu enter:
$ lsb_release -a

Once you’ve followed the directions provided by Mongo, here are some other helpful commands to make sure everything is configured properly.

See a log of what MongoDB is doing:
$ cat /var/log/mongodb/mongod.log

See all the processes running on your machine:
$ ps -aux

See all the processes with mongo in the listing:
$ ps -aux | grep mongo

See the status of the ports:
$ netstat -ntlp

Here’s what the bottom of my log file looks like as well as a double checking of the current status of Mongo and what port its running on.

psandportcheckin

Before we access this DB from our application we need to change one setting so Mongo accepts connections from different IP addresses besides the local IP address.

3. Configure Mongo to connect to all external IPs

Before we go on, I need to make it clear that this is not a best practice, and no user data should be stored on a VM with an open port like this. But for development and practice purposes we’re going to make it easy to connect.

If you’re going to go into production, please refer to MongoDBs security checklist HERE.

Okay, having said that.

Open up the mongod.conf file using nano:

$ sudo nano /etc/mongod.conf

And uncomment the binding IP by adding an octothorpe at the beginning of the line like so:

uncomment

Save and close the file. ( Ctrl+X -> y -> enter )

Alright, now MongoDB should be ready to be connected to!

4. Try it out and Connect your application

Open the Mongo Command line by typing mongo in your ssh terminal.
And show your DBs and collections
show dbs
show collections

Here’s the getting started tutorial from Mongo that I found helpful:
https://docs.mongodb.com/v2.6/tutorial/getting-started/

And a great way to generate some test data to get familiar with your Databases and collections:
https://docs.mongodb.com/v2.6/tutorial/generate-test-data/
Here’s what it looked like for me!

usingmongo

The connection string for the VM is the IP address and the DB you’d like to write too. To connect to the test database on the is vm the connecion string to use looks something like this:

mongodb://40.83.182.555:27017/test

In the next blog I’ll show you how to connect your Azure Web App to Mongo on this VM!

Caught a Giants game this week!
Caught a Giants game this week!

Running Node and Express on Ubuntu VM

So you just spun up your first Ubuntu Virtual Machine?
No…
Let’s fix that: “Intro to Ubuntu on Azure”

YEAH!
Let’s put it to work!

The Basic Steps to using node on an Azure VM:
1. Open the Port
2. Install Git and Install Updates
3. Install Node and NVM
4. Code and Install Express
5. Run it and check it out!
6. Use forever to keep it alive

1. Open the Port
We’re not talking battleships or submarines we’re talking Infrastructure as a Service.
Visit the landing page for your Ubuntu Virtual Machine:
vmnumberonelandingpage

And select the resource group in the top left corner:
resourcegroup

Resource groups are the way we break down how our VM interacts with the internet, other vms, storage, and public/private networks.

To open the port we need to change our network security group which is represented by the shield. (Underlined in the above screenshot)
Then we’ll select settings -> Inbound security rules
networksecuritygroupsettings

This will allow us to open up our VM to the Public Internet so we can visit it like any other website.

Under ‘Inbound security rules’ SSH is already included:
defaultssh

We’re going to add a new Inbound security rule named ExpressServerPort where we’ll set the Destination port range to 3000 which we’ll see later when starting our server. Here’s the configuration pane for our ExpressServerPort:
destinationport

2. Install Git and Check Updates
Git is fun!

SSH into our Virtual Machine like we did in the VM intro then enter:
$ sudo apt-get install git
sudoaptgetinstallgit

Yay! We have Git

Let’s run our update command just to double check we’re up to date:

$ sudo apt-get update
sudoaptgetupdate

Great let’s move on with Node!

3. Install NVM and Node

First NVM: https://github.com/creationix/nvm

NVM is a version manager for Node you can install using curl:
Enter this command to install it:
$curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash

Then source your profile to add it to your commands:
$ source ~/.profile

Then check out the version to make sure it installed:
$ nvm --version

It should look like this:
nvmisntallversion

NVM is installed!
Let’s install a version of Node!
$ nvm install 5.0

Then check our version of Node:
$ node -v
nvminstallfive

Sweeeeeeet

Check out NVMs readme on the github for more commands:
https://github.com/creationix/nvm

With node comes ‘npm’ which allows us to install a whole bunch of node awesomeness. One of the more popular packages is express a minimalist web framework, we’ll install this to start coding away.

4. Code and Install Express

We’ll be following along “Express’s” introduction if you get lost/have more questions about express.
http://expressjs.com/en/starter/installing.html

First let’s create our a directory, cd into it and initialize our app using nom.

$ mkdir myapp
$ cd myapp
$ npm init

After entering npm init we’ll be walked through a configuration step for our app. The only one that matters for now is (index.js) which will be the entry point for our app everything else can be the default for now.

If you were actually going to submit/share this code you’d want to accurately fill out this info.

After the initialization step we’ll add express and list it as a dependency:
$ npm install express --save

Here’s what those steps look like:
mkdirmyapp

Weeee now have express and an app ready for your code!

Lets open nano and put the helloworld sample into index.js

$ sudo nano index.js

sudonanohelloworld

Now lets run our app!
$ node index.js
nodeindexjs

Now that its running lets visit it by entering the IP address and port number into our browser.
In my case the URL is: http://13.88.180.170:3000/
ipaddressandport

Neat!

6. Use forever to keep it alive
Unfortunately, if we close our Terminal/SSH Connection our project will stop running.
To solve this, we use another NPM package called forever.
Here’s the link to the repository with clear instructions.

In short we install it globally:
$ sudo npm install forever -g

Then start it:
$ forever start app.js

And stop it:
$ forever stop app.js

That’s it for now!

Now clone one of your node projects and run them in the cloud!
Happy Hacking!

When the Male Roommates are home alone...
When the Male Roommates are home alone…

Intro to Ubuntu Virtual Machines on Azure

When I search:
Node JS Server Azure, Ubuntu, JavaScript, Mongo, Postgres, Flask, VM
I turn up with all sorts of unhelpful results.
So I dedicated a couple days to creating a couple guides for common Cloud Stacks on Azure VMs to make it as simple as possible to start deploying your code to the cloud.

This is the introduction and at the bottom of this blog post you’ll see other workflows fill in.

So, Here’s a guide to deploying an Ubuntu VM on Azure:
1. Gather Materials
2. Create VM
3. Check VM using SSH

1. Gather Materials
Here’s what you’ll need:
An Azure Account
An SSH Client perhaps putty… or even Bash On Windows?

2. Create VM

Head into the Azure Portal: portal.azure.com

And Select Virtual Machines -> Then ‘Add’
selectVirtualmachines

You’ll then see a page like this:
selectubuntu

Select Ubuntu Server 14.04.

There are lots of configurable deployments available if you feel like exploring.

Then select Create, but make sure the deployment model is Resource Manager as its more future ready then the classic model:
createvm

We’ll then get to the basic configuration tab, fill out the info and pick a User name and Password that you’ll remember because you’ll need it later!

configurationbasics

If you’re not familiar with Resource Groups check out THIS ARTICLE

I’ve named my resource group: ResourceGroupOne

Hit Okay to go to the next configuration pane

Select the Size of your VM. To see all the options select ‘View All’
selectvmsize

We’re going to go with the cheapest option A1 Standard:
SelectAOne

Hit Okay to take us to our final configuration Pane, “Settings”.

settingstwo

There are a number of different settings presented here.

First up is Storage:
This will configure what we want to name the storage account for our vm. I’ve changed mine to ‘resourcegrouponestorage’, but I could have selected any of my previous storage account in the same region, in this case westus.

Second is Network:
We can configure a Virtual Network to allow our virtual machines to connect to other resource on our network by default. We can also change this later. So in this case I’m creating the default virtual network.

Again, I could have selected a previously created Virtual Network Called ‘Databases’ which is in the same region.
virtualnetworkdefault

Third is Extensions:
We won’t add any extensions

Fourth is Monitoring:
Which we’ll disable for simplicity sake, but is a very powerful tool one you start needing to make scaling decisions.

Fifth and finally is Availability:
We won’t use an availability set, until we need to scale out our app.

Here’s what the lower portion of our settings pane looks like:
settingstwoend

And we’ll select OK to finish with our settings. This will take us to the summary page so we can do a one more check on our machine, don’t get to anxious about making mistakes because we can always tear this one down and spin up another if we messed something up!
vmsummary

Hit Okay one last time!

You’ll then be taken to your dashboard where you’ll see a nice loading tile:
loadingdashboard

It’ll take ~5 minutes to spin up and then we’ll be ready to take on the world!

Once ready it’ll look like this:
clickthetile

Click the tile to hit the landing page for our VM:
vmnumberonelandingpage

See that public IP address?
We’ll use that to SSH into our machine.

In my case: 13.88.180.170 !

3. Check VM using SSH

Let’s SSH into our box.

Pull out your preferred SSH client. Here’s bash on Windows and Putty Side by Side:
sshintomachine

Notice ‘Timothy’ Triple underlined?
That’s the User Name we set during basic configuration and is paired with the password that we also set in Azure.

When you connect you might have to accept the ras2key fingerprint. It’ll look like this when using putty. Or it’ll be in the terminal using bash. Type ‘yes’ or Select Yes to continue.
sayyestowarning

Then type in your password and marvel and your creation:
typeinyourpassword

Let’s test our vm by installing updates! Yay Updates!

$ sudo apt-get update
sudoaptgetupdate

Now that you have a VM ready let’s put it to work!

Host a Node Server
Host a Python Flask Server

Pradeep Cruising on National Donut Day
Pradeep Cruising on National Donut Day