/serverless-functions

Showcases using Netlify serverless functions to power a front-end application.

Primary LanguageHTML

Netlify Status

Serverless Functions

Introduction

Demonstration

This repository showcases using serverless architecture in order to perform back-end tasks without having to manage / run / maintain a service running a back-end application. A serverless platform provides a clear separation between infrastructure services and applications running on top of the platform.

There are a number of benefits in choosing serverless over a traditional service, such as:

  • Reduced operational costs - Functions are "pay per execution". Compared to running an application on a server 24/7, functions will be cheaper as you'll only be charged for the time your functions ran.
  • Automatic Scaling - FaaS providers create more instances of functions as required when more traffic comes in. The provider will keep lambdas running for a period of time once they've completed, incase more traffic arrives.
  • Faster development - More time can be spent in the logic of the application rather than managing services and their infrastructure.

Instructions

The application is a Preact application that fetches show information upon load, and fires off voting requests whenever buttons are clicked. The front-end application and the serverless functions are powered by Netlify, and use a Postgres database from Heroku with a GraphQL layer on top powered by Hasura.

  1. Ensure your heroku database is populated with the contents of schema.sql.

  2. Link your Hasura GraphQL API to the Heroku database.

  3. Create a Netlify site for this project and add the following environment variables into the site settings panel:

    name description
    HASURA_GRAPHQL_ENDPOINT Used by functions to fetch and push data.
    HASURA_ADMIN_SECRET Acts as authorization to interact with GraphQL API.
  4. Install node dependencies and ensure the Netlify CLI is installed:

    yarn global add netlify-cli && yarn install

  5. Start the application:

    yarn start

Note: If your local project doesn't have a .env file then Netlify CLI will automatically source values from the remote project.

Note: Console logs from inside of serverless functions are available to view in the Netlify site's "Functions" section, and will stay available for up to an hour after they're run.

Further Information