Getting started with AWS
Amazon Web Services (AWS) is the leading platform for cloud computing. It has nearly every conceivable thing you’d need. If you just need a server and database — it has that. If you need natural language processing and a bulk emailing platform — it has that too and more! AWS is used extensively by top companies and startups because of its wide range of products, it’s super easy to dynamically scale, and it’s very secure.
We’re going to take a look at how to set up your first server on AWS, how to SSH into that server, and how to host a website on that server.
Step 1: Setting up a Server
If you haven’t signed up for AWS, you should go do that now. Now that you’ve got an account, you need to get a server. AWS’s servers are called EC2 (which stands for Elastic Compute Cloud). So click on EC2 from your home console.
Click on the Launch Instance button to start setting up a virtual server. Virtual servers on EC2 are called “instances”.
We’ll be using Ubuntu Linux on our instance, so select Ubuntu Server from the list of operating systems.
Now you get to choose the type of instance you want. You can get instances with more powerful CPUs (if you’re doing some heavy computation) or instances with top-of-the-line GPUs (if you need to do rendering or blockchain stuff). We can just choose a t2.micro instance for now. This is perfect for any small project and it’s free for your first year on AWS! When we start getting millions of users on our site, we can easily scale up to a bigger instance.
Then click on Configure Security Group at the top. That’s the next step that we need to make a change to.
AWS is all about scalability and security. All of your instances have security groups that determine what traffic is allowed to go in and out of your instance. If you don’t want your server to be accessible from the internet, your security group can stop that from happening.
We’re about to talk a bit about ports. If you’re not familiar with ports, don’t worry. Essentially they’re the doors you can use to communicate with your server. (Here’s a quick intro)
By default your security group will allow traffic on port 22. We will need this to access our server with SSH. If you want to access a website over HTTP (port 80) or HTTPS (port 443), you can add rules to allow that.
Putting a webserver on port 80 or 443 actually requires a bit more setup. So for now we’ll just be putting our webserver on port 3000. Below I add a custom TCP rule allowing all inbound traffic on port 3000 from any IP.
Then click Review and Launch.
Check everything is okay then click Launch.
Next, you’ll have to create and download a private key (.pem) file. You need this key file to connect to your server with SSH. You can call your key file whatever you want, but I’m going to call mine awskey
.
Then finally click Launch Instance.
Click View Instance to see your new virtual server.
Here you can see info about your instance. Take a look at the Public DNS and Public IP. You’ll be using these to access your server.
Step2: Connecting to Your Server
Amazon has full instructions for how to connect to your server using Windows or Mac/Linux. But here’s a quick outline of how to do it on Mac/Linux:
Move your awskey.pem
file into ~/.ssh
for safekeeping. Remember the name of your .pem file might be different if you called it something else when you were setting up your instance.
Make sure it isn't publicly viewable by running chmod 400 awskey.pem
on your terminal. You can now connect to your server using:
$ ssh ubuntu@<public-ip> -i ~/.ssh/awskey.pem
Where <public-ip>
is the Public IP for your server that you can find on the AWS console. If you receive the following error:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/<user-name>/.ssh/awskey.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/<user-name>/.ssh/awskey.pem": bad permissions
Permission denied (publickey).
It’s because you didn’t change the permissions using chmod 400 memegen.pem
correctly.
If everything went to plan, you should see a new command prompt:
ubuntu@<public-ip>:~$
Congratulations! You’ve successfully set up an AWS server and SSH’d into it!
Protip!
To connect to your server a bit quicker, you can add the following to your ~/.ssh/config
file:
Host my-server
HostName <public-ip>
User ubuntu
IdentityFile ~/.ssh/memegen.pem
Port 22
Now you can access your server by just running ssh my-server
. Way quicker!
Step 3: Setting up a Website
Now that you’ve SSH’d into your server, you can set up a website to actually serve. Let’s quickly set up a website using NodeJS. You can install NodeJS by running the following commands:
$ curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
$ sudo apt-get install -y nodejs
(See here for up to date installation instructions.)
Once NodeJS is installed, let’s make a new directory for our website and to keep out code tidy with mkdir server
then move into that directory with cd server
. Once in that directory, start a NodeJS project with:
$ npm init
You can accept all the default options. Next, we want to install Express which we’ll use to set up our website.
$ npm install express
Create a file called index.js
and enter the following code. This code simply starts a server listening on port 3000 and sends the text “Hello World” when you go to the root of the server.
var express = require('express');
var app = express();app.get('/', function(req, res) {
res.send('Hello World');
});var server = app.listen(3000, function() {
console.log('server started');
});
Run the script using:
$ nodejs index.js
You should see “server started” on the console. Now open your browser and navigate to <public-ip>:3000
(where <public-ip>
is the IP or Public DNS of your server you got from the AWS console). You should see the words “Hello World”! Congratulations you have a website public on the internet!
Next Steps
Now that you have your server set up, you can start coding your awesome website! If you want to continue to use NodeJS but aren’t sure what the next steps are, check out this awesome series of tutorials.
You might want to set up your webserver so it uses the default HTTP port 80 rather than port 3000. You can do this quite simply on AWS using load balancers. Find out how to do that in the next blog post of this series: How to serve your website on port 80 or 443 using AWS Load Balancers.
Remember, there’s a lot of documentation out there about AWS. So if you’re stuck, just try googling a few key words and you should be able to find some advice.