Create your first website with serverless in 15 minutes

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+.

Read part 2 of creating your first serverless website.

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 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 log in 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 for this. Focus on steps 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 above
sls create --template  hello-world


You'll end up with something like this:
serverless website terminal
serverless website terminal

Project setup

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

// generate a package.json file
npm 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 Express, a minimalist web framework to get things going faster. You can basically use whatever you want to build your website.

Installing dependencies

//install express - a simple web framework
npm i --save express
//install the body-parser middleware
npm i --save body-parser
//install view engine for express
npm i --save  hbs
//you'll need serverless-http to connect your api to aws
npm 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/json
app.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:

serverless website terminal
serverless website terminal

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, bear with me. Open up your serverless.yml file and paste the following:

service: awesomesauce

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

# The `functions` block defines what code to deploy
functions:
  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”.

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:

serverless website terminal
serverless website terminal

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.

Congratulations!

Since you are reading this on Dashbird.io you might want to start monitoring your serverless website to make sure your lambdas are working properly.

Prevent serverless errors with AI-driven insights

Read our blog

ANNOUNCEMENT: new pricing and the end of free tier

Today we are announcing a new, updated pricing model and the end of free tier for Dashbird.

4 Tips for AWS Lambda Performance Optimization

In this article, we’re covering 4 tips for AWS Lambda optimization for production. Covering error handling, memory provisioning, monitoring, performance, and more.

AWS Lambda Free Tier: Where Are The Limits?

In this article we’ll go through the ins and outs of AWS Lambda pricing model, how it works, what additional charges you might be looking at and what’s in the fine print.

Made by developers for developers

Dashbird was born out of our own need for an enhanced serverless debugging and monitoring tool, and we take pride in being developers.

What our customers say

Dashbird gives us a simple and easy to use tool to have peace of mind and know that all of our Serverless functions are running correctly. We are instantly aware now if there’s a problem. We love the fact that we have enough information in the Slack notification itself to take appropriate action immediately and know exactly where the issue occurred.

Thanks to Dashbird the time to discover the occurrence of an issue reduced from 2-4 hours to a matter of seconds or minutes. It also means that hundreds of dollars are saved every month.

Great onboarding: it takes just a couple of minutes to connect an AWS account to an organization in Dashbird. The UI is clean and gives a good overview of what is happening with the Lambdas and API Gateways in the account.

I mean, it is just extremely time-saving. It’s so efficient! I don’t think it’s an exaggeration or dramatic to say that Dashbird has been a lifesaver for us.

Dashbird provides an easier interface to monitor and debug problems with our Lambdas. Relevant logs are simple to find and view. Dashbird’s support has been good, and they take product suggestions with grace.

Great UI. Easy to navigate through CloudWatch logs. Simple setup.

Dashbird helped us refine the size of our Lambdas, resulting in significantly reduced costs. We have Dashbird alert us in seconds via email when any of our functions behaves abnormally. Their app immediately makes the cause and severity of errors obvious.