Hello World API in 5 minutes

Now, we're ready to get something going. The resources we're going to deploy in this example should be covered by the free tier, and all the services will be of the serverless variety so that the compute doesn't cost when it's sitting idle and not processing responses. Let's get started:

  1. The very first thing we need to do after sorting our prerequisites is create the service boilerplate:
sls create 
--template aws-nodejs
--path serverless-hello-world

Straight away, we have an example serverless.yml configuration file with useful comments in it.

  1. Let's simplify this a little bit and remove the comments for now. What we're left with is a very short file that looks as follows: 
service: serverless-hello-world

provider:
name: aws
runtime: nodejs10.x

functions
:
hello:
handler: handler.hello

What we're looking at is some provider configuration for AWS and a declaration for a function called hello. The handler is a pointer to the function inside our handler.js file. This is the same syntax that we use to represent the handler in a standard lambda configuration.

  1. Because we are now working with a Node.js project, we should also initialize the necessary project files. Inside your service directory, run the following command:
npm init -y && npm install
  1. We also have a hello world handler function that has been created for us, but let's update it to put our own mark on it. Replace the boilerplate function code with the following:
'use strict';

module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello World deployed from Serverless Framework!',
input: event,
},
null,
2
),
};
};

This is cool. If we went to deploy these using the serverless CLI commands, we could, but we haven't specified an event to trigger the function with yet.

  1. Let's add an HTTP event so that serverless will create an API Gateway endpoint for us. Update the function declaration in your serverless.yml file to include the event. It should look as follows:
functions:
hello:
handler: handler.hello
events:
- http:
path: /hello
method: get

Okay, I think we're ready to deploy now!

  1. The command to deploy is super easy, and we'll dig into what is happening behind the scenes in the next section. For now, run the following command in your service directory:
sls deploy

Because I've used a specific AWS CLI profile to set up my new user, I also had to include an option in the command to specify the profile I wanted the CLI to use: --aws-profile <profile name>.

Also, we haven't specified which region to deploy to, so the command line will default to us-east-1. If you want to specify the region, you can add it in the provider declaration or pass it in as an option in the deploy command using --region switch.

After running the command, you should see some log output, and when the process is complete, you will see a summary of what has been deployed. From the following output of my deployment, you can see that we have a new service in us-east-1 that includes a function and an HTTP endpoint. This endpoint is now live—we can send a GET request using curl and get a response! 

Deployment log output

This was quite a basic example to demonstrate how easy it is to create a project and have a deployed function and RESTful interface in less than 5 minutes. It's important to understand the technical process of what is happening underneath during a deployment, so in the next section, we shall go through this.

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

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