Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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.
I just launched a serverless website in 15 minutes. was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.