This is part of an ongoing series about the Serverless framework. For those following along, part 1 and part 2 have been updated for the current latest version of Serverless 0.3.1.
In this post, we’ll discuss how a Serverless function actually works.
The guts of a serverless function
When we visited the deployed endpoint at the end of part 1, it correctly returned some JSON content.
1 2 3 |
|
Where does this message come from? Look at index.js in the component’s lib folder.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
And it’s the handler.js file in the function’s subfolder which calls it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
In our case we’re coding a password checking function. The URI will look something like this:
http://something.amazonaws.com/development/potd/check?password=P455w0rd
We’ll modify lib/index.js to retrieve the value from the query parameter password
and return true
if the password is correct and false
otherwise. But first we need to set up the parameter.
Configuring the function parameter
In each function’s directory, there is a file named s-function.json which allows you to specify the details of the function call. Add a section to the requestTemplates
as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
Note that the s-function.json file is also where you can configure if the service accepts POST
or PUT
or DELETE
requests. Here we are only interested in GET
.
You can easily tailor the requestTemplates
in this file to extract whatever parameters you need in your lambda function.
Retrieving the parameter value
Now back in index.js you will find that the function’s event
parameter has a property password
which is set to the value of the querystring parameter.
1 2 3 4 5 6 7 8 9 |
|
Redeploy.
$ serverless dash deploy
Visit the URI.
http://something.amazonaws.com/development/potd/check?password=P455w0rd
The response is:
1 2 3 |
|
Ready for implementation
We now have all the pieces we need. Serverless, Typescript, Mocha and AWS. In the next post I’ll show how to wire up everything get it working.