This post explains how to quickly and easily get a MVC web application from Visual Studio into AWS. The AWS website covers most of this, but the information is spread out and not necessarily written with a .NET developer in mind.
- Setup Amazon account https://aws.amazon.com
- Setup a key-pair for whatever region you want to use (AWS Management Console: EC2 dashboard, select region, choose Key Pairs and create. Save the private key!).
- Install AWS SDK: http://aws.amazon.com/sdk-for-net/
- Install AWS toolkit for Visual Studio: http://aws.amazon.com/visualstudio/
Wait, am I going to get billed for this? No, not if you stick to the "Free Tier" offerings. This means using the "micro" sized servers and not doing significant file or network operations.
Building the Application
Get started by opening Visual Studio and creating the web project to deploy. Your first instinct is probably to use the installed template "AWS Web Project." This is fine, but, it's a web forms project. Chances are that you would prefer MVC. So instead, create a regular Web Application with MVC.
There are three things to do if you are not using the AWS template:
- If you plan on using the AWS API, you will need to add two appSettings to your web.config:
- Ensure that any referenced assemblies that will not be present by default on the target server are set to "Copy Local = 'True'" This applies to System.Web.Mvc, System.Web.Razor, and any others not part of the basic framework. This should be done be default, but check anyways.
- Again, if you plan on using the AWS API, you need to add the "AWS SDK for .NET" NuGet package to the project itself.
Make any changes you want to the app and ensure that it runs locally.
Deploying to AWS
Backing up for a moment, here are the general options for getting your code into AWS.
- Standalone deployment tool: This basically does deployment through scripts. It can be useful for integrating with build definitions, etc. Check it out here.
- Using AWS Management Console (Elastic Beanstalk, OPS Works, etc.)
- Use AWS Management Console to create a virtual server (EC2 instance) then RDP to it and copy files to IIS directory.
- Using Visual Studio tools.
This article focuses on using the Visual Studio tools.
Deploying with Visual Studio
First, in VS go to View - AWS Explorer. This is a tool very much like the web based AWS Management Console. Keep that open on the side.
Right click on project and choose "Publish to AWS."
A wizard will launch that gives you three options for deploying:
I'll spend some time explaining the differences. First, it's easier to think of this as being two options: Elastic Beanstalk or Cloud Formation (the last two options are just variations of Cloud Formation options).
Without going into too much detail, the differences between the two are:
Cloud Formation is a toolset in AWS that lets you define resources to provision in a JSON template file. You can create a template file that says, for example: "Deploy two Windows 2012 servers on a private subnet behind a load balancer." Then, you upload this file to AWS and AWS handles provisioning and configuration. There is a learning curve here. Thankfully, the VS toolkit for AWS does all this work for you and will create the template file after you run the wizard.
Elastic Beanstalk is a service intended for developers that provides the simplest path to get custom code into AWS. It's a PaaS offering that ultimately does the same thing as Cloud Formation. It handles all the setup tasks, and provides a basic dashboard to manage and monitor your application. You do sacrifice some things by using this, for example, control of the URL, IIS Settings, difficulty with multiple websites, etc. If you just want to get started quickly and/or have no interest in where your app will live, choose this option.
Choose whichever one you would like. You'll find that there is not much difference when you are just doing a simple website.
If this is your first time, go with Elastic Beanstalk. You can find an explanation of all the configuration options here.
In this post though, I will go through Cloud Formation since it's not covered as frequently and it does give some interesting options.
Choose "Single Instance Template" and "Next."
On next screen, you will see this:
- Default AMI: "AMI" is an "Amazon Machine Image," which is basically a VM with some stuff pre-installed. Here you get two choices (IIS 7.5 orIIS 8 on server 2012).
- Instance type: IMPORTANT- choose "Micro" or else you will be charged.
- Key Pair: If you setup a key pair at the beginning you should see it here. If not, verify that you are in the correct region.
- Security Group: You should use a security group that has port 80 open for inbound traffic. This should already exist. If not, the wizard will warn you and you need to open the AWS Management Console, EC2 Dashboard, then choose your region, click on "Security Groups" and add a new one.
You can leave everything as is on this page. The Application Environment refers to adding web.config appSettings. Application Credentials are needed if you are going to access AWS services, such as RDS. One thing to note for future reference- when you get to the point of needing to specify these credentials, I recommend creating an ApplicationAdmin user account from the Visual Studio AWS Explorer. This will enable the "Use an IAM user" option. Alternatively, a good practice is to use Policy instead of Roles.
While it is provisioning, you will see status updates in Visual Studio. It does auto refresh, but takes a long time. Also, this is the same thing you would see on the Elastic Beanstalk page or the Cloud Formation page in the AWS Management Console.
Do not get impatient- this will take some time. If you get bored, open the AWS Management Console and follow along. Look at the Cloud Formation dashboard and you will see pretty much the same thing as in VS:
Go to the EC2 dashboard and click on "Instances" in the left navigation bar, and you will see your new server being provisioned and initialized:
Now go get some coffee (or Mountain Dew, your choice).
Eventually, you will get the notification that it completed successfully.
Once you see this, click on the "Application URL" link and you'll see your website, in the cloud:
That's it, you're done. I will quickly cover some other topics of interest below. If you want to skip those sections, just remember to cleanup your AWS resources to avoid being billed…
If you own a domain name and want to point it to your site, you can get the public IP address for this machine off the EC2 dashboard page. Note however, that each time you shut down and restart this instance, you will get a different public IP. The way around is this is to configure and assign an Elastic IP in AWS Management Console.
Remote Desktop access:
If you want to browse to your server, you can use RDP client. You just need to do a couple things before it will work.
First, you need to modify your security group to allow RDP traffic. Open the AWS Management Console, EC2 Dashboard, and click "Security Groups" on the left. You will need to Edit the Inbound rules to add a rule for RDP, as shown here:
NOTE: it is bad practice to leave the source set to any IP. Instead, you should enter your own IP. Since we are going to delete this anyways it doesn't matter.
Second, you need to setup the user password. Back in Visual Studio, click the "Connect to Instance" button at the top of the monitoring console:
Choose the key pair option:
Click "OK" and RDP should launch and take you to the server. If something goes wrong, it probably has to do with the keypair (when using the toolkit, the SDK stores your secret key in an encrypted file). In that case, go to the EC2 dashboard, click on "Instances" and choose the instance, then press "Connect." The UI will ask for your private key data, then allow you to decrypt the Administrator account password.
Your code will be on the c:\inetpub\wwwroot path.
Cloud Formation Template:
One more neat thing about deploying via Cloud Formation is that you can download the template file that it creates for you. Go to the CloudFormation dashboard, and choose your template, then look at the "Template" tab:
This is the JSON template used to create your instance. You can save this and modify it as needed to make future deployments even easier.
If you used Elastic Beanstalk, open AWS Management Console, browse to the Elastic Beanstalk page, choose the application, then do Actions - Terminate.
Otherwise, open AWS Management Console, browse to the Cloud Formation page, choose the template, then do Actions - Terminate.
In either case, you should also go to the S3 dashboard and delete the buckets that were created to store your deployment packages.
Finally, wait a few minutes, then go to the EC2 dashboard and ensure that your instance shows as "Terminated." NOTE: it will remain visible for up to an hour while in the "Terminated" state but won't count against you.
Overall, the Visual Studio Toolkit for AWS makes deploying applications to the AWS Cloud really easy. Not surprisingly, Windows Azure plug ins are even simpler to use, but it's good to know that your choice of cloud providers does not need to be driven by the developer toolkit.
In the next post, we'll take this a step further by adding a SQL database hosted by Amazon in AWS RDS. Stay tuned-