Start free trial

Central data platform for your serverless environment.

Get full access to all premium features for 14 days. No code changes and no credit card required.

Password: 8+ characters, at least one upper case letter, one lower case letter, and one numeric digit

By signing up, you agree to our Privacy policy and
Terms and Conditions.

Create your first website with serverless in 15 minutes

Share

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

Made by Developers for Developers

Our history and present are deeply rooted in building large-scale cloud applications on state-of-the-art technology. Dashbird was born out of our own need for an enhanced serverless debugging and monitoring tool, and we take pride in being developers.

10,000+ developers trust Dashbird

Dashbird helped us reduce the time to discovery from 2-4 hours to a matter of seconds. This means we’re saving dozens of hours in developer time, which not only can be funneled into improving the product but also means 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.

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.

Read our blog

AWS Well Architected Framework in Serverless: Cost Optimization

In this post, we’ll talk about the Cost Optimization (COST) pillar of the Well-Architected Framework (WAF) and what you should focus on in order to build cost-effective serverless environments.

How to Build, Deploy, and Debug a Food Delivery App on AWS

We’re getting down and dirty in this hands-on tutorial on how to build and deploy an event-driven Lambda backed food delivery app, and how to monitor it without using AWS products.

AWS Step Functions Error Handling

In this article, you’ll learn the common reasons behind AWS Step Functions errors and how to assess and handle them.

AWS Well-Architected Framework in Serverless: Reliability Pillar

In this article, we will focus on the AWS WAF Reliability (REL) pillar: the Foundations, Failure and Change Management.

AWS Well-Architected Framework in Serverless: Operational Excellence

This article will discuss the second most crucial pillar of the AWS Well-Architected Framework: Operational Excellence (OPS).

Go to blog