paint-brush
I just launched a serverless website in 15 minutes.by@john.c.demian
19,038 reads
19,038 reads

I just launched a serverless website in 15 minutes.

by John DemianJune 18th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Alright, we talked the “serverless is awesome” talk but now it about time to walk the walk. In the next 15 minutes, I’ll take you through the entire process of creating your first website running on serverless and you better believe it’s going to be awesome.

People Mentioned

Mention Thumbnail

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - I just launched a serverless website in 15 minutes.
John Demian HackerNoon profile picture

Alright, we talked the “serverless is awesome” talk but now it about time to walk the walk. In the next 15 minutes, I’ll take you through the entire process of creating your first website running on serverless and you better believe it’s going to be awesome.

So without further ado let’s start with what you’ll need. First off you’ll need to sign up for Amazon. There are a few steps you’ll have to take in order to be squared away but the entire process should take less than 5 minutes.

Now that that’s squared away make sure you’ve got NodeJs installed on your computer. I’m using version 8 so you might want to have at least NodeJs v8+.

Prerequisite

If you don’t have it you can download NodeJs here. You’ll have to download the installer, run it following the onscreen instructions and then restart your computer. Once that’s done let’s test it to see if everything is running correctly. Open your terminal or favorite command-line tool and type in node -v. You should see a message saying v8.xx.x or something similar. Next up we need to double check our that npm is installed correctly. As you might have already guessed the command for that is npm -v.

Serverless setup

Right, since everything is looking good let’s start by installing the serverless framework. In your terminal type the following:

npm install -g serverless

You’ll notice the ‘-g’ in there. It stands for global. On my Windows machine, I’ve had problems installing serverless and had to install it globally in order to get it to work properly.

Next up we’ll login into our newly installed serverless platform

serverless login//sls login is a shorthand that works too

You’ll have a new browser window open up asking you to log in (you can use GitHub to do that)

After that’s done we need to get our AWS credentials configured. The process is simple but requires multiple steps. Luckily there’s an awesome serverless tutorial made by Adnan Rahic. Focus on step 2 and 3. That’s where the magic is.

If that doesn’t cut it here’s a video showing with a play by play of the entire process.

Alright, now that we’ve got all our basic stuff out of the way let’s get down to brass tacks.

Open your file explorer and create a new folder for the project. I’m calling mine ‘serverless-app’. In the newly created folder open your terminal to create a simple serverless boilerplate.

//create the boilerplate mentioned abovesls create --template  hello-world

You’ll end up with something like this:

Project setup

Before we start installing our dependencies we’ll have to create our package.json file.

// generate a package.json filenpm init

You’ll be asked to provide names and descriptions and a lot of other information. Since this is a test just press “Enter” and leave all the fields empty.

We are going to use Expres a minimalist web framework to get things going faster. You can basically use whatever you want to build your website.

Installing dependecies

//install express - a simple web frameworknpm i --save express//install the body-parser middlewarenpm i --save body-parser//install view engine for expressnpm i --save  hbs//you'll need serverless-http to connect your api to awsnpm i --save serverless-http

Now we’re getting somewhere.

Open up the handler.js file on your computer and paste in the following code:

const serverless = require("serverless-http");const hbs = require("hbs");const express = require("express");const bodyParser = require("body-parser");

const app = express();app.use(bodyParser.urlencoded({ extended: false }));// parse application/jsonapp.use(bodyParser.json());app.set("view engine", "hbs");

app.get("/", function(req, res) {  res.status(200).render("index");});

module.exports.awesomesauce= serverless(app);

Next up: the part everyone is familiar with, the HTML code. You’ll have to create a new folder in the root of your project called “views”. Open the folder and create you handlebars template called index.hbs

Your project should look something like this:

Create the website

Here’s the code I’m adding to my website. Creative, I know.

<!DOCTYPE html><html><head><meta charset="UTF-8"><title>What's all the FaaS about?</title><style>body{text-align:center;}</style></head><body>

<h1>What's all the FaaS about?</h1><p>Get it? It's a punn.</p><p>Unfunny joke brought to you by John Demian</p>

</body></html>

You can create js files, css files, basically whatever you want at this stage.

Create your service

Almost done, bare with me. Open up your serverless.yml file and paste the following:

service: awesomesauce

# The `provider` block defines where your service will be deployedprovider:  name: aws  runtime: nodejs8.10

# The `functions` block defines what code to deployfunctions:  app:    handler: handler.awesomesauce    # The `events` block defines how to trigger the http events    events:        - http: ANY /        - http: 'ANY {proxy+}'

What you just did is set our runtime environment, nodjs8.10, we specified the name of our app, intuitively called “app” and then we specified the handler which coincidentally is the name of our service: “awesomesauce”.

Important: Please make sure you indent your code correctly.

Deployment

We’ve made it to the final step. Once you deploy this to AWS you’ll have your own serverless website. How awesome is that? Back to the terminal for one more line:

sls deploy

You’ll see the terminal doing all kind of geeky stuff but at the end, you’ll see something like this:

Copy and paste the endpoint in your browser and voila, you’ve got your first serverless website up and running.

Nicely done! High five all around! It’s been quite a journey but it’s worth it. You are the proud owner of a brand new website.

Monitoring your application

AWS Lambda is freaking awesome but it lacks severely in some aspects. One of those aspects is the monitoring. Their tool is not terrible but it can be overwhelming for anyone that’s not accustomed to it.

To get around this issue I’m using Dashbird.io, a serverless monitoring tool for AWS Lambda that lets me to see everything that happens behind the scenes thus allowing me to react quickly in the event of a breakdown or if I need to keep tabs on costs.

Signup takes but a couple of minutes and I strongly suggest you get it going, especially if you are just starting out working with serverless as it provides insight tailored for human beings.

Almost forgot to mention this but Dashbird.io has a free tier that you can start using right now. Using the free version will give allow you to monitor up to 1gb of logs from AWS and an unlimited number of invocations.