Technical Articles
Writing Function-as-a-Service [3]: Real Local Development
This blog is part of a series of tutorials explaining how to write serverless functions using the Functions-as-a-Service offering in SAP Cloud Platform Serverless Runtime
Quicklinks:
Quick Guide
Sample Project
In the previous blog we’ve learned how to use our local environment for writing functions and how to deploy to the FaaS runtime (Serverless Runtime)
Nevertheless, you might have asked:
Is this really local development…??
You might have been sitting in aircraft flying to Maledives to enjoy working in relaxing environment – but not able to write functions
Disappointed
Positive: you must have enjoyed your vacation much better
Negative: you were just one click away from the answer to your question.
Just clicking this blog….
Yes:
It is possible to run a serverless function on a local laptop
Really local
With other words:
You have a cat which comes to your local home, only to grab some cookies, then deploys itself back, to run in the woods
Or you have a cat running in your local home
Prerequisites
Previous blog already showed that we need
– cf CLI
– Faas CLI
– Node.js
Now, in addition, we need the Faas SDK
Preparation
The FaaS SDK is a Node.js package, available on SAP registry
As such, we need to have the SAP registry in our node/npm configuration
View the config with npm config list
If not already there, you can add it with the following command:
npm config set "@sap:registry=https://npm.sap.com"
Alternatively, you can edit the file .npmrc which is located in your home directory of your system
Add this line to the file
@sap:registry=https://npm.sap.com
Install faas SDK
Once you have access to the SAP registry, you can install the FaaS SDK
Run the following command on command line, in any directory
npm install @sap/faas -g
This makes the SDK available globally on your machine, so you can execute the command without full path
After installation, you can find the packages in your home directory at
C:\Users\kitty\AppData\Roaming\npm\node_modules\@sap\faas
There you can find a README.md which contains a very good documentation.
Of course: my blogs are based on that docu
Which doesn’t mean that you can skip my blogs, now that you have the readme
No no
But it does mean that from now on, I can be ore lazy in describing, because I know that you’ve read the readthat
Create Project
This blog is about how to develop locally, you can use any FaaS project you have already created
If you don’t have a project at hand, you can create a new project according to the appendix
Run FaaS locally
In our command prompt, we jump into our project root folder
cd tmp_faasreallylocal
Then we execute the run-command of the SDK:
faas-SDK run
As a result, we can see the convenient output.
We can see that a server has been started at the default port 8080
And we get the URL of our function
And we can see that the command doesn’t return.
It is waiting for us
So it’s now our turn to call the function URL
We open a browser and call the local URL:
If you have logs in your code, you’ll see them in the console.
Isn’t that wonderful?
It is really fantastic
We have a serverless runtime running on our local machine
Our serverless function is running on a local server
Sounds contradicting
Who cares?
It is like you go to open the door for a cat that desperately wanted to go out – but once you open the door, it doesn’t go out
Contradicting – but it doesn’t care
Note:
We don’t have to install or start any server. The server is still managed by FaaS, even locally
Note:
We can control the port of the FaaS server with flag -r
faas-sdk run -r 3003
Note:
The local runtime is a simulation, it doesn’t support all features.
Pls don’t be disappointed if e.g. triggers aren’t currently supported locally
More Info
More info in the docu.
We’re already used to the FaaS having good docu.
So we execute the help command to get more info:
faas-sdk help
The help output is so nice that it deserves a big screenshot:
The init
The init is a command which initializes a project for us.
That project is generated from a predefined template which contains not only project structure and files, but even runnable code.
Try it:
Navigate to a root-root directory (root for the generated root) and run
faas-sdk init -p ./kitty
This will generate the root directory kitty, containing the FaaS project
Step into the folder, run npm install, then execute it: faas-sdk run
Of course, you can adapt all the files, that’s how it was meant to be used
Finally deploy to the cloud as described in previous blog
The end
Surprisingly, this is not a command, it is a section containing my final words
If you’re as excited as me about the possibility to run the FaaS project locally, then please don’t hesitate to express it with a little like – I’ll deploy it to the development team – and that might trigger more local features ?
-h
This is again a command and contains the quick guide for the quick cats among you
prepare | npm config set “@sap:registry=https://npm.sap.com” |
install | npm install @sap/faas -g |
init | faas-sdk init -p ./kitty |
run | faas-sdk run |
Appendix: All Project files
For your convenience, the screenshot shows the project structure:
faas.json
{
"project": "reallylocalproject",
"version": "1",
"runtime": "nodejs10",
"library": ".",
"functions": {
"myfun": {
"module": "fun.js"
}
}
}
fun.js
module.exports = () => { return "meow" }
package.json
{}