Complete the steps in this guide to install the Serverless Framework open-source CLI and deploy a sample Service on AWS that reports deployment information and operational metrics to the Serverless Framework Dashboard.
There are a few prerequisites you need to install and configure:
If you already have these prerequisites setup you can skip ahead to deploy an example Service.
node -v
from your command line and get a result like this...$ node -v
vx.x.x
npm -v
from your command line and should see...$ npm -v
x.x.x
npm install -g serverless
serverless -v
from your command line and get a result like this...$ serverless -v
x.x.x
Now that you’ve completed your setup, let’s create and deploy a serverless Service.
Use the Serverless Framework open-source CLI to create a new Service.
# Create a new Serverless service/project
$ serverless
# Change into the newly created directory
$ cd your-service-name
The serverless
command will guide you through creating a new Node or Python Service, configuring your AWS Account to work with the Serverless Framework, and setting up a free Serverless Framework Dashboard account so you can monitor, troubleshoot, and test your new service.
Looking to get started with something other than Node or Python? No problem. You can use the create
command to get started with a variety of other languages.
An Event is anything that can trigger your serverless functions. In this case, you need to define an endpoint in your serverless.yml
that will trigger your serverless function.
functions:
hello:
handler: handler.hello
# Add the following lines:
events:
- http:
path: hello
method: post
Use this command to deploy your service for the first time and after you make changes to your Functions, Events or Resources in serverless.yml
and want to deploy all changes within your Service at the same time.
serverless deploy -v
Replace the URL in the following curl command with your returned endpoint URL, which you can find in the sls deploy
output, to hit your URL endpoint.
$ curl -X POST https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello
Invokes a Function and returns logs.
serverless invoke -f hello -l
Open up a separate tab in your console and stream all logs for a specific Function using this command.
serverless logs -f hello -t
Use either of the two commands below to generate mock errors that you will then be able to visualize in the Serverless Framework Dashboard. If you use the curl command remember to replace the URL in the command with your returned endpoint URL, which you can find in your sls deploy
output.
serverless invoke -f hello -d '{"body": "not a json string"}' # causes a JSON parsing error so error Insights will populate
$ curl https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/hello --data-binary 'not a json string' # causes a JSON parsing error so error Insights will populate
If at any point you no longer need your Service, you can run the following command to remove the Functions, Events and Resources that were created. This will delete the AWS resources you created and ensure that you don't incur any unexpected charges. It will also remove the Service from your Serverless Framework Dashboard.
serverless remove
Now you are ready to leverage the hundreds of Service Examples available to you from Serverless, Inc. and our large and growing community to build your own Services.
The serverless
introduction above guided you through creating a new service for a simple Node.js project. However, if you want to create a project for a more advanced use case and you don’t want to start from scratch, check out the list of available examples.
Clone a Service from the Serverless Inc. repository of Examples
# replace folder-name below with the folder name of the example you want to use
$ serverless create -u https://github.com/serverless/examples/tree/master/folder-name -n my-project
Or, clone a Service example from the Serverless open-source community
$ serverless create -u https://github.com/author/project -n my-project
Run the serverless
command in your new project to connect it to your Serverless Framework Dashboard account to enable the monitoring, troubleshooting and testing features.
$ serverless
Deploy your service
$ sls deploy
Want to learn more? Check out a list of available tutorials here.
Product