Friday, 25 September 2015

How to set up Mono in Amazon Web Services

We want to run Mono in an AWS EC2 Linux instance but the default yum package for Mono is really out of date: soooo out of date that it won't even compile the latest source code (remember that to compile Mono source you need an already functioning, recent version of Mono)!

So we're going to have to build from scratch. Before we start, you will need to:
This article was pulled together from multiple sources - but Brad's post was particularly useful.


Setting up the Prerequisites


We want the latest version of all build components, plus the Extra Packages For Enterprise Linux for which yum is already configured but which is disabled by default.  So, edit the repository definition to enable the first EPEL entry (which is the run-time) by entering the following commands:
cd /etc/yum.repos.d
sed '0,/^enabled=0/s/enabled=1/' <epel.repo >/tmp/epel.repo
sudo cp /tmp/epel.repo .
Now we can update all repositories to their latest versions:
sudo yum update
And get some build tools that aren't installed by default:
sudo yum install autoconf libtool gettext make automake gcc gcc-c++


Getting the Sources


Install git so that we can download the sources (the git install at time of writing is about 10MB):
sudo yum install git
Make somewhere to download the Mono sources and download them (be cognisant of the size):
mkdir src
cd src
git clone https://github.com/mono/mono


Compiling the Sources


Build the Mono bootstrapper:
cd mono
./autogen.sh --prefix=/usr/local
make get-monolite-latest
Use the bootstrapper to build Mono, then install it:
make EXTERNAL_MCS="${PWD}/mcs/class/lib/monolite/basic.exe"
sudo make install
But that's not enough.  If you were to return to the source tree and make check you would find that one or two of the tests fail - in particular, one around unhandled exceptions in mscorlib.  We can't accept that but at least the newly installed Mono is better than the monolite version.  So let's clean up and compile again: this time with our newly built version as the compiler.
make clean
./autogen.sh --prefix=/usr/local
make
sudo make install
You should make check now if you have time, just to prove that the new version is clean.
If you wish to try out your new toy, copy the text below into a file called HelloWorld.cs:
public class HelloWorld {
    public static void Main() {
        System.Console.WriteLine("Hello World");
    }
}
Then compile and execute, by:
mcs HelloWorld.c
mono HelloWorld.exe

Monday, 21 September 2015

Connecting to an AWS Linux Instance

Connecting to a Linux EC2 Instance over SSH


So you've created a Linux EC2 instance in Amazon Web Services, with a key-pair, and now you need to connect to it over SSH.  For this we'll need:
  • A copy of the PuTTY and PuTTYgen programs, downloaded from the internet.
  • Your key file that was generated when you created the instance.
  • Your instance's IP address, which you can get from the Amazon EC2 page.

Downloading PuTTY and PuTTYgen

PuTTY is a free Telnet and SSH client which we will use to connect to our Linux instance.
PuTTYgen is a tool which will convert our key-file into a form suitable for PuTTY.
The tools are in beta, but they have been for years, like Google products.  They're stable.

Download PuTTY and PuTTYgen from the PuTTY download page by clicking each EXE.
Save them to your local hard-drive wherever is easily accessible: no installation required!



Converting your .PEM key-file to .PPK

To connect to your instance using PuTTY you need a private key file (ending in .ppk).
We'll use PuTTYgen to convert our Amazon key-file (ending in .pem) to that format.
Click the PuTTYgen binary to start the conversion program:


Ensure that SSH-2-RSA is selected as the type, then click Load.
Pick the .pem key-file from Amazon (choose All Files in the file-type drop-down).
Then click Open to load the key:


Once the key is loaded, it can be saved in .ppk format.
You can add a passphrase if you are happy to type that every time you connect.
Then create the private key file in a memorable location by clicking Save private key.


Now close the PuTTYgen application.


Connecting using PuTTY

Open the PuTTY application by clicking it.
In the Host Name box enter ec2-user@ followed by the IP address for your AWS instance.

Then expand the SSH node (under Connection in the left hand side-bar) and expand Auth.
Click Browse and select the private key (.ppk) file that you saved earlier.
Then click Open to connect to your Amazon instance.


And we're done.

Creating an AWS Linux Instance

Creating an Amazon EC2 Linux Micro Instance

We're going to create a Elastic Compute (EC2) micro instance in AWS running Amazon's own cut of Linux.  It's free-tier eligible but if you aim to try to stay free, remember to set up alarms and alerts to keep you informed of your usage and optionally shut down the instance if you hit the free-tier limit.

Log on to AWS and click the EC2 link (main pane, top left, under Compute):


Choose your closest region (menu-bar, top right) and click Launch Instance:


Tick the Free Tier Only checkbox (left side-bar) and choose your image.
Choose Amazon Linux AMI, in my case it's top of the list.  Click Select to the right of it:


Select a free tier Amazon Linux instance (in my case the t2.micro at the top of the list).
Then click Review and Launch:


This generates a mostly complete configuration.  Check it over to be sure.
If your desktop machine has a fixed IP address (or range), edit your security group.
Edit the tags to give your instance a name (add a tag called Name and set its value).
Then click Launch.


Select to Create A New Key Pair from the drop-down and enter a memorable name.
Then click Download Key Pair and save it to a safe location which you'll remember.
If you lose the downloaded file you won't be able to login to your instance so keep it safe!
Now click Launch Instances to finally start-up your new virtual machine!


It will take about 5 minutes to launch your instance: go make a cup of your beverage of choice.
And we're done!  Now we need to connect to it!

Next up: Connecting to an AWS Linux Instance