Friday, 31 December 2010

Windows Server 2008 Set-up on Amazon Web Service (AWS)

When you create your instance on Amazon Web Services you choose an AMI (Amazon Machine Image) which is a template to base your instance on. I am using the AMI with code ami-cf517bbb which is a 32 bit version of Windows Server 2008 with SQL Server (2008) Express.

The Micro Instance

I am using a Micro instance, which will give me just over 600MB of memory and hopefully enough processor power to run several small websites. The worry will be whether the memory is enough to run a database server as well.

When you select the AMI to build from AWS will make a copy of that template. Using a micro instance uses storage called EBS (Elastic Block Storage). This storage is persistent. Other instances can work differently with disk space that isn't persistent - if the instance dies you loose your data. But I don't need to worry about that. Later on I'll be looking at the backup and other storage options. The database/website I am deploying will only be used for reporting on the data contained within it and quite a bit of data is cached on the website anyway.

Getting the administrator password

Once the instance is setup you need to retrieve the administrator password. This is done from the Management Console by right clicking on the instance and choosing the option "Get Windows Password". You then need to paste in your private key (that you create earlier).

It can take up to 10 minutes after you start creating your instance until it is available. Part of the process of creating your image is also to create your security settings (firewall) so that you can login.

Logging in

When you login for the first time you should change your administrator password. I didn't do this - I was trying to be smart and create a new user for myself that has administrator privileges. I did do this but had other issues later when logged in as this user to do with permissions. Something I will come back to later.

Best practice would be to use a different username to login and then rename the administrator username to something obscure, then disable it. Partially security by obscurity (by renaming the user and using another username) but an additional layer with disabling. For the moment I am using the administrator user.

You login using Remote Desktop over the internet - hopefully protected by your security settings. There are further settings you can put on using certificates to authenticate yourself.

The initial setup

Disks

The AMI has one 30GByte disk. This is a little bit of a pain. I'm not overly worried (for the stuff that I am running) that all my data will be on one disk. It sort of forces me into thinking carefully about things. However, 30GByte is quite big - the disk has over 20GByte free and since it is pay as you use you are paying for it. I don't really know a way of making the disk size smaller and giving it back to Amazon. Amazon are giving a one year free offer of 10GByte of space - it is probably only a couple of dollars a month, not much when I have just one server.

CPU

The CPU reports as Intel Xeon E5430 @ 2.66GHz (with the words 2.96GHz next to it!) and task manager reports one core. With the micro instance you get 1 Elastic Compute Unit that can burst to 2 units - one unit is described 1.2GHz Opteron. The computer name is set and apparently you can change it - can't see any advantage to doing this at the moment, so I'll leave it. The aim is to change as little as possible.

Remember, as long as you have got your data you can start up another instance with a new AMI and move the databases/sites over and get that running. It will cost you a few cents per hour, then move the site across using the Elastic IP addresses (see article on setting it all up).

The DotNet framework to version 3.5 is installed.

Getting setup

Once the server is up and running, I always get Task Manager up and running to monitor what is going on.

Main initial tasks that needed to be done were:
  • Create a directory where you are going to put all your stuff. Something like c:\data is fine.
  • Setup a default website and move the default website away from c:\inetpub\wwwroot. I just create a directory
mkdir c:\data\web
cd \data\web
mkdir default
cd default
echo "Hello" > index.htm

Then go into IIS and create a site that points to it (I'm not going to give a guide to IIS here) Setup IIS so that the log files are created somewhere other than c:\inetpub\logs, e.g. c:\data\logs. By putting all your stuff in one place makes it easier to move/backup/delete etc.

What also needs to be installed

Chrome

I don't use IE much, just for testing! The default IE installation is fairly hardened so you need to set the security to allow you to download .exe files. This is just a preference, although I do need to check if there are any security reasons for not doing so.

DotNet 4

The application I am installing requires the DotNet Framework version 4 as it uses ASP.NET MVC version 2. I generally try to install from the base installers rather than the web installer. This means a 50MB download from Microsoft.

SQL Server 2008 Express

Below is a quick overview of manually setting up a database and website. I've not used any deployment here as I want to understand how the server is operating first. Also, not quite time for me to worry about using the release configuration of the website I am going to deploy.
My first task was getting SQL running. My application requires a database and I was anxious to use the server, rather than write a little hello world test program using ASP.NET/C#. Anyway the index.htm "Hello" worked.

The first problem I found was that the SQL Server instance for Express was disabled - so start the Service manager (services.msc) and enable it.

Then start Management Studio, right click on the instance name and choose Properties. Goto the Security tabs. I don't like my web applications accessing using Authenticated Security. This is partly as I've always did it using SQL Server authentication. By default Server Authentication is only setup to allow Windows mode. You need to change this.


Then go to Security, Logins and change the sa password. It's up to you whether you enforce the password policy. If I was running Active directory or multiple servers I might think about setting up the security as normal. But again not really worried about it. The other advantage of setting up SQL Server authentication is if you want to access the server remotely.


Now goto the status option and make sure that the user is enabled.


Restore any databases you might have. The add a new login and user to access it. Don't use one you have used before (i.e. on the backup from your development environment). If you do want to use the same user then lookup the stored procedure sp_change_users_login - I'll write a post about this later once I get my deployment tools in place. It is likely that you are using integrated authentication in your development environment anyway (in which case you need to learn about connection strings).

Your website

I zip up the files for the server then create a directory (e.g. c:\data\web\mysite) and unzip it. Check there isn't anything in it you don't think should be there. (I'll be looking at deployment next so that I can make updates to the website quickly - but for the moment I'm just getting it up and running and checking it is all Ok).

Then go into IIS and setup the site. Few things to note
  • If you are going to use Host Headers for your site (i.e. hosting more than one site on your server) then if the DNS hasn't propagated it will be difficult to test. There is a way (editing the hosts file on your local computer). However, for the moment disable your default site and add a new site pointing to your newly created directory.
  • Make sure you setup the site to use the right application pool, in my case the .NET Framework 4.
  • Check the settings for log files.
You need to understand connection strings to access your local database (or add in the integrated NETWORK SERVICE user). As said I tend to use SQL Server authentication anyway. I have two connection strings - one for objects created by the entity model and one for stored procedures being accessed. Look in Web.config - generally connection strings should be like

Data Source=.\SQLEXPRESS;Initial Catalog=<DATABASE>;
      Integrated Security=False;User Id=<SQLUSER>;Password=<SQLPASSWORD>

If you were using SQL Server Express locally you shouldn't need to change the server name. Now test your site - check it locally (http://localhost) or via the IP address or URL (if DNS has propagated).

How to transfer files to your server

You could install FTP on your new server - but I always think that is quite a bit of security hole. What I have done is open up some ports on my broadband firewall that forwards to a site on one of the PC's I have. Then copy files into that directory then from the server browse for them and save them. Once uploaded you can remove them.

Windows Update

Lots of Windows Updates need to be done. Do them, just in case. Also by doing them now and then testing your website you make sure that everything is working Ok. If you do them later and something doesn't work it's a bit harder! There is 143MB of updates to do - remember however, that you are paying for the bandwidth! It takes a long time to do.

Once you have done the Windows Update I advise going running the Service manager (services.msc) and stop and then disable the Windows Update service and the Module Installer service. If you don't do this then these services will keep running. On a machine with only 600MB of RAM you need to reduce the amount running. But, remember by doing this you need to ensure you do the Windows Update yourself.

The other service to disable is the Print Spooler - I'll get back to you on other services you can safely kill. I also remove SQL Server Compact edition

Others changes

A few other things I did
  • Set the computer to work better for better performance (My computer->Properties->Advanced Settings->Settings)
  • Set the computer to work better with background services (My computer->Properties->Advanced Settings->Advanced)
  • Change the page file (My computer->Properties->Advanced->Virtual memory->Change). I set mine to 1.5Gbytes.

Is memory an issue?

At the moment who knows. The server isn't being hit by a lot of traffic yet. I do know that when I install anything (e.g. Windows Update, Subtext or the .NET framework) I know that I have to be patient. As CPU will peak quite quickly at 100% and things go slow. But when nothing is running on the server and I access my test site it all goes quite well (although the site caches all the data that is displayed on the front page and the first few pages (about 4MByte of storage), so I'm not really hammering the site - I think the googlebot has accessed more data on the site than me!

Later

This is all a bit rough and ready for me. Some best practice has went out the window and I need to get processes straight for deploying sites and databases. But doing something manually is good for you.

I need to look at some server hardening (thinks like security policies, permissions on directories etc) and then using the AWS backup facilities.

Once I work out everything I need to do for the server I intend to create another instance from scratch and do them. It'll cost me a few hours of time, but less than 10 cents of cost. Once this is done the aim will be to create a backup that can be used for future instances (or in case of failure of this instance at some point - remember it is Windows!)

Thursday, 30 December 2010

Getting started with Amazon Web Services (AWS)

I am about to launch a new website - more about that in another article. The site is nearly finished (or at least in a state ready for launch). However, I need to get in place the hosting for it as from experience I want to be aware of the problems before rather than after the site is launched.

I've looked at a few options in the last year or so, from shared hosting to dedicated servers to virtual servers and using cloud based services (for things like SQL). Still not sure what I'd do long term. From my time at GTI I know about running a network centre and configuring and using servers.

I've recently joined the Microsoft Website Spark program and found lots of suppliers giving access to dedicated servers for a lower price whilst you are part of the program. But it's not that much cheaper, and it is for the most expensive option - dedicated servers.

Anyway what I need is a Windows 2008 Server running SQL Server. SQL Server express will operate with my new site quite happily, although part of Website Spark gets me SQL Server Web Edition.

For the moment I've chosen Amazon Web Services (AWS) using their Elastic Computing Cloud (EC2). A friend of mine at Swirrl recommended this to me. You basically pay as you go - so if things don't work out then I'm not stuck in a 12 month agreement.

Amazon describe their service as "a web service that provides resizeable compute capacity in the cloud". I'm not too sure I agree with that completely - you buy a share of a large server and can do with it as you want (mostly). Unlike, say Azure you aren't consuming that as a service. At the moment not too sure I'm that comfortable letting go of the control of a server to using a service. I currently host my home website using shared hosting, where all you can do is upload files (via FTP) so I was really looking forward to being in control again.

Amazon operate four network locations (with multiple "availability zones" within each location) giving good redundancy options (if you want to use them). Two of these are on different sides of the USA one in Asia and one in Ireland. The prices for using each location is similar, although the centre in the Virginia seems slightly cheaper. I choose to use the one in Ireland, but nothing to stop me using another later.

I had a server up and running in less than 20 minutes after signing up. The sign up process involves giving over your credit card number and receiving an automated call from Amazon to verify your phone number. From there you are presented with a web site (the AWS console) and can create an instance.

I am using a service called EC2 - the Elastic Computing Cloud. It really means that you create server instances, that you can repeat (i.e. have multiple instances), that you can load balance (using Elastic Load Balancing). Their are several storage options, including EBS - Elastic Block Storage, where you can purchase more storage when you need to or copy your current volumes.

AWS Console
For what I am doing, I simply need a server to provide a web site - not too worried about load balancing (which gives not just capacity but some disaster recovery). I am (for the moment) going to run a few web sites on the one server.

Amazon provide a number of instance types. This is probably one of the biggest drawbacks of the Amazon service in that you are pigeon-holed into a certain process capacity. The elastic part if that you can add more instances, however, for a small site you have a choice of the instances called t1.micro and m1.small. The configuration of these are

Micro - 613MB memory, Up to 2 EC2 compute units (for short bursts)
Small - 1.7GB memory, 1 EC2 compute unit

One EC2 Compute Unit is described as being the equivalent CPU capacity of a 1.0-1.2 GHz 2007 Opteron or 2007 Xeon processor or early-2006 1.7 GHz Xeon processor. For my small application in terms of running as a web server I'm really not that worried about the processing power. The Micro instance seems more suited to a web server as it can "burst" to using more power. However, it is expected that the processor usage will generally be less than 1 unit. The biggest difference is in memory and this is my biggest worry. Can I get a (Windows) server (with SQL Server) running in around 600MB of memory. The key to AWS is that you pay for what you use - so if it doesn't work out I can move to a Small instance or a different provider.

But, what about the cost. Amazon do provide a sort of pay in advance option (called Reserved Instances) where you pay a fee in advance for an instance, and then the pay as you go price is lower. I am currently using an On-Demand Instance, where for a Micro instance I am paying USD 0.035 per hour (For a small instance the cost is nearly four times as much). For a reserved instance the cost will be USD 0.016 per hour - half the price, but you pay either USD 54 for 1 year or USD 82 for a 3 year term. (Note that once you pay your upfront fee, you don't need to actually host anything). Amazon also provide a service called Spot instances where you effectively bid for instances, which will reflect Amazon's spare capacity and will be available at a cheaper price than the on-demand instances. However, from what I read you need to be aware that the capacity may be taken away at any time (as the capacity is needed elsewhere).

You also pay for your storage as well as the bandwidth in and out. For the first year a lot of this cost is fairly covered as a discount is provided. A Micro instance running Windows works out about USD 26 per month and bandwidth/storage hopefully only another couple of dollars. (If you want a micro instance running Linux then AWS really gives it to you for nearly free for a year). There is a decent calculator on the Amazon website to work out your costs.

Getting a server up and running was fairly painless. The documentation is very good and easy to work through. From the console you choose launch a new instance and choose from a number of Amazon Machine Images, which are good templates. I choose a 32-bit Windows 2008 R2, with SQL Server Express. Within 10 minutes it was set-up.

A bit of security is needed where a key pair is created. Once created you then paste in your private key to a window which will decrypt the administrator password. You also need to set-up a firewall (or security rules). This means getting the IP address of your broadband connection and setting this up so that only you can remote desktop to the server.


The next step is optional, I didn't do it initially but wish I did. When an instance is started it is given an IP address. If the instance is stopped (i.e. you aren't paying for it) then when you start it up again later you get a different IP address. What you need to setup is called an Elastic IP Address. This is associated with your account. When an instance is started you can then relate the IP address with an instance. Elastic IP addresses are free - until they aren't associated with any instance, when you pay 1 cent per hour. If you are going to be running a website 100% of the time then it shouldn't be a problem. You can use this IP address for your DNS setup.

Logon to your instance using remote desktop and setup your server. I'll write about that in the next post.