fbpx
Creating your first Spotinst Function

This guide explains how to set up a Spotinst Function followed by an example of a Spotinst function and its output.

To create a Spotinst Function, follow the steps below:

1. Login to the below Spotinst URL:

https://console.spotinst.com/functions/function

2. Click on the Create Function and enter your function details and the function template

You will get a screen as shown below:

Please note: You must adhere to our Function Templates  or the ZIP or JAR format

Running the Function

3. To run the function you need an API Authentication token, you can generate the token from the Spotinst console: Generating API Token

4. To check if the function is working as expected, post the below call with your token:

$ curl http://SPOTINST-FUNCTIONS-URL -H "Authorization: Bearer ${TOKEN}"

You can also test whether the function URL is working using the “Triggers” tab, which is present below the selected function:

Functions Examples

1. A simple Nodejs function that handles HTTP Query input parameters:

Function template:

exports.main = function main (req, res) {
                  return new Promise(function(resolve, reject){
                       var name = req.query.name;
                       console.log("Hello " + name);
                       return resolve({
                                 statusCode: 200,
                                 body: "Hello " +name});
                  });
               };
$ curl http://app-5a3c2366-test.execute-function1.spotinst.io/fx-831b6640?name=Spotinst  -H "Authorization: Bearer ${TOKEN}"
Hello Spotinst

Log Output:

[2017-08-27T17:58:54.195+0000] - [stdout] - SPOTINST_FUNCTION__START_INIT_FUNCTION
[2017-08-27T22:17:46.833+0000] - [stdout] - SPOTINST_FUNCTION__END_INIT_FUNCTION
[2017-08-28T09:36:36.345+0000] - [stdout] - SPOTINST_FUNCTION__START_OF_USER_FUNCTION
[2017-08-28T09:36:52.646+0000] - [stdout] - Hello Spotinst
[2017-08-28T09:41:04.954+0000] - [stdout]-SPOTINST_FUNCTION__END_OF_USER_FUNCTION