Services

A service is our unit of project. It's our top-level name for the service we are creating and contains all the functions, events, and resources we need for the project. You can build everything you need to in one service, or have multiple services for a single serverless application. It's up to you to define what the delineation is and where the domain boundaries are.

We only need to create the service once, and some boilerplate files are created for us to get going quickly. Using the serverless CLI command, we can run the following code. This will create our service in the directory path we specify:

sls create     
--template aws-nodejs
--path serverless-hello-world

At this point, we can choose which language we are writing our functions in using the template option. There are service templates for all of the supported languages.

Take care when creating the service name because this drives a lot of the naming conventions when things are deployed to AWS.

The files that are created will depend on the template you choose. For Node.js, for example, you get the resulting project structure. The following is a directory structure with our service folder at the top level, followed by two files that were created by the framework:

serverless-hello-world
├ handler.js
└ serverless.yml

You'll recognize the hander file straight away, but what's this new YAML file for? This is where the service, provider, functions, events, resources, and plugins are all declared and configured. When you go to deploy your new service to your chosen cloud provider, the Serverless Framework uses this file to decide how to assemble the deployment template—in the case of AWS, it is a CloudFormation template and what needs to go into it.

Once we have a service, we can define functions. We'll explore this in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.237.29