GithubHelp home page GithubHelp logo

shavo007 / serverless-nodejs-starter Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 2.0 3.34 MB

A Node.js starter for the Serverless Framework with webpack, custom domain and unit test support

JavaScript 85.02% Shell 14.98%
lambda serverless-framework serverless-nodejs-starter webpack aws aws-apigateway babel-webpack mochajs renovate circleci2

serverless-nodejs-starter's Introduction

serverless-nodejs-starter

CircleCI

Coverage Status

A Node.js starter for the Serverless Framework with webpack, custom domain and unit test support

Once installed, you can create and deploy functions with the latest ES6 features in minutes, with linting and formatting baked in.

asciicast

Serverless Node.js Starter uses the serverless-webpack plugin, Babel, and Mocha. It supports:

  • ES7 syntax in your handler functions
    • Use async/await
    • And much more!
  • Run API Gateway locally
    • Use yarn serve
  • Support for unit tests
    • Run yarn test:unit to run your tests
  • Sourcemaps for proper error messages
    • Error message show the correct line numbers
    • Works in production with CloudWatch
  • Automatic support for multiple handler files
    • No need to add a new entry to your webpack.config.js
  • Add environment variables for your stages

Install

  • Yarn
  1. curl -o- -L https://yarnpkg.com/install.sh | bash
  2. yarn bash completion - https://github.com/dsifford/yarn-completion
# If you don't already have the serverless cli installed, do that
yarn global add serverless

# Use the serverless cli to install this repo
serverless install --url https://github.com/shavo007/serverless-nodejs-starter

# cd into project and set it up
cd serverless-nodejs-starter

# The bootstrap command renames the project folder and project in package.json and serverless.yml
# and initializes a git repo
yarn bootstrap your-project-name

# Install dependencies
yarn install

Development

Creating and deploying a new function takes two steps, which you can see in action with this repo's default Hello World function (if you're already familiar with Serverless, you're probably familiar with these steps).

1. Add your function to serverless.yml

In the functions section of ./serverless.yml, you have to add your new function like so:

functions:
  hello:
    handler: src/hello.default
    events:
          - http:
              path: hello
              method: get
              cors: true

we're setting up a function named hello with a handler at src/hello.js (the .default piece is just indicating that the function to run will be the default export from that file). The http event says that this function will run when an http event is triggered (on AWS, this happens via API Gateway).

2.a Create your vanilla function

This starter kit's Hello World function (which you will of course get rid of) can be found at ./src/hello.js. There you can see a basic function that's intended to work in conjunction with API Gateway (i.e., it is web-accessible). Like most Serverless functions, the hello function accepts an event, context, and callback. When your function is completed, you execute the callback with your response. (This is all basic Serverless; if you've never used it, be sure to read through their docs.

2.b Express wrapper function

On the other hand if you want to use express and proxy through api gateway have a look at ./src/index.js

For more info check out the tutorial at serverless and how it can help with cold starts serverless blog

3. Create your custom domain

customDomain:
    domainName: shane.shanelee.xyz
    certificateName: '*.shanelee.xyz'
    basePath: ''
    stage: ${self:provider.stage}
    createRoute53Record: true
    endpointType: 'regional'

To create the custom domain

Make sure and change the values to suit your needs

sls create_domain --stage dev

Custom alerts

A Serverless plugin to easily add CloudWatch alarms to functions

alerts:
  stages:
    - dev
  topics:
    alarm:
      topic: ${self:service}-${opt:stage}-alerts-alarm
      notifications:
        - protocol: email
          endpoint: [email protected] # Change this to your email address

This creates an SNS topic and triggers based on certain cloudwatch metrics and sends via email.

For more info check out their docs

API Gateway-like local dev server

To spin up a local dev server that will more closely match the API Gateway endpoint/experience:

yarn serve

Adding new functions/files to Webpack

When you add a new function to your serverless config, you don't need to also add it as a new entry for Webpack. The serverless-webpack plugin allows us to follow a simple convention in our serverless.yml file which is uses to automatically resolve your function handlers to the appropriate file:

functions:
  hello:
    handler: src/hello.default

As you can see, the path to the file with the function has to explicitly say where the handler file is. (If your function weren't the default export of that file, you'd do something like: src/hello.namedExport instead.)

Renovate

Automated Dependency Updates

Renovate runs continuously to detect the latest available versions. And automagicaly creates PR on your github project with changelog and release notes.

For more info and how to authorise the github app check out onboarding guide

Deploy

Assuming you've already set up your default AWS credentials

yarn deploy:dev will deploy to "dev" environment. You can deploy to stage or production with:

yarn deploy:stage

# -- or --

yarn deploy:production

After you've deployed, the output of the deploy script will give you the API endpoint for your deployed function(s), so you should be able to test the deployed API via that URL.

serverless-nodejs-starter's People

Contributors

dependabot[bot] avatar renovate-bot avatar renovate[bot] avatar shavo007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

lantrix nadabasha

serverless-nodejs-starter's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update babel monorepo (@babel/core, @babel/plugin-transform-runtime, @babel/preset-env, @babel/runtime)
  • chore(deps): update dependency aws-sdk to v2.1623.0
  • chore(deps): update dependency husky to v9
  • chore(deps): update dependency prettier to v3
  • chore(deps): update dependency prettier-eslint to v16
  • chore(deps): update dependency rimraf to v5
  • chore(deps): update dependency serverless-domain-manager to v7
  • chore(deps): update dependency serverless-offline to v13
  • chore(deps): update dependency sinon to v18
  • fix(deps): update dependency serverless-http to v3
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

circleci
.circleci/config.yml
  • circleci/node 10.24.1
npm
package.json
  • @babel/runtime 7.23.8
  • express 4.19.2
  • serverless-http 1.10.1
  • source-map-support 0.5.21
  • winston 3.5.0
  • @babel/core 7.23.7
  • @babel/plugin-transform-runtime 7.23.7
  • @babel/preset-env 7.23.8
  • @babel/register 7.23.7
  • aws-sdk 2.1125.0
  • babel-loader 8.3.0
  • babel-plugin-istanbul 5.2.0
  • babel-plugin-source-map-support 2.2.0
  • chai 4.4.1
  • commitizen 3.1.2
  • coveralls 3.1.1
  • cross-env 5.2.1
  • cz-conventional-changelog 2.1.0
  • husky 1.3.1
  • lint-staged 8.2.1
  • mocha 6.2.3
  • mocha-lcov-reporter 1.3.0
  • nyc 13.3.0
  • prettier 1.19.1
  • prettier-eslint 8.8.2
  • rimraf 2.7.1
  • serverless 1.83.3
  • serverless-domain-manager 2.6.13
  • serverless-offline 4.10.6
  • serverless-plugin-aws-alerts 1.7.5
  • serverless-webpack 5.9.1
  • sinon 7.5.0
  • sinon-chai 3.7.0
  • webpack 4.47.0
  • webpack-node-externals 1.7.2
  • node >=8
nvm
.nvmrc

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.