You can create a service based on a specific template that specifies which provider and runtime to use.
To create a service with a nodejs
runtime running on aws
just pass the aws-nodejs
template to the create command:
serverless create --template aws-nodejs --name my-special-service
This will create a service and generate serverless.yml
, handler.js
and event.json
files in the current working directory and set the name of the service to my-special-service
in serverless.yml
.
You can also check out the create command docs for all the details and options.
Let's take a closer look at the skeleton Serverless has created for us.
You'll see the following files in your working directory:
serverless.yml
handler.js
event.json
.gitignore
Each Serverless service configuration is managed in the serverless.yml
file. The main responsibilities of this file are:
- Declares a Serverless service
- Defines one or multiple functions in the service
- Defines the provider the service will be deployed to (and the runtime if provided)
- Defines custom plugins to be used
- Defines events that trigger each function to execute (e.g. HTTP requests)
- Defines one set of resources (e.g. 1 AWS CloudFormation stack) required by the functions in this service
- Events listed in the
events
section may automatically create the resources required for the event upon deployment - Allow flexible configuration using Serverless Variables.
You can see the name of our service, the provider configuration and the first function inside the functions
definition which points to the handler.js
file. Any further service configuration will be done in this file.
The handler.js
file includes a function skeleton which returns a simple message. The function definition in serverless.yml
will point to this handler.js
file and the function inside of it.
Check out the code inside of the handler.js
so you can play around with it once we've deployed the service.
This file contains event data we'll use later on to invoke our function.
Take a look at the our list of Serverless services.
We've just created our very first service with one simple create
command. With that in place we're ready to deploy
our service (which now includes one example function) to our provider (in this case Amazon Web Services).