This page will walk you through building a function that uses dependencies and environment variables to call another function.

If you are new to Binaris is it recommended that you look through the previous getting-started tutorials:

Note: the instructions below assume you are using MacOS, Ubuntu or Debian.

1. Add dependencies to your function

Binaris functions support npm modules to install dependencies. To demonstrate that, you will build a function that leverages the lodash npm package.

Create the function

$ mkdir MyFunction
$ cd MyFunction
$ bn create node8 MyFunction

Install the lodash npm package

$ cd MyFunction
$ npm install lodash

Edit your function to use lodash

Replace the code in function.js with:

const _ = require("lodash");

exports.handler = async (body, context) => {
  const name = context.request.query.name || body.name || 'World';
  return `Hello ${_.upperCase(name)}!`;
};

Deploy and Invoke the function

$ bn deploy public_MyFunction
Deployed function public_MyFunction
Invoke with one of:
  "bn invoke public_MyFunction"
  "https://run-sandbox.binaris.com/v2/run/<Your_Account_Number>/public_MyFunction"

$ bn invoke public_MyFunction --data '{"name":"world"}'
"Hello WORLD!"

You have just changed your input string to upper case by using the lodash npm package.

2. Call a function from another function

Typically, applications are built from many functions. In this section you will add a new function (MyCaller) that will call the MyFunction function you created earlier.

Create the function

$ mkdir MyCaller
$ cd MyCaller
$ bn create node8 MyCaller

Install the ‘request-promise-native’ module

You will need this module to call public_MyFunction asynchronously.

$ npm install request request-promise-native

Edit ‘MyCaller’ to call ‘public_MyFunction’

Replace the code in function.js with

const rp = require('request-promise-native');

exports.handler = async (body, context) => {
  const name = context.request.query.name || body.name || 'World';
  const options = {
    url: `https://run-sandbox.binaris.com/v2/run/${process.env.BINARIS_ACCOUNT_NUMBER}/MyFunction?name=${name}`,
    headers: {
      'X-Binaris-Api-Key': `${process.env.BINARIS_API_KEY}`,
      'Content-Type': 'application/json',
    }
  };
  return rp.post(options);
};

Use an environment variable

Before deploying MyCaller you should store your Binaris Account Number and API Key in environment variables. Storing configuration information in environment variables rather than in code is a best practice in terms of security and manageability. In the code above, the URL for MyFunction is constructed by pulling the Account Number portion of the URL from the BINARIS_ACCOUNT_NUMBER environment variable (process.env.BINARIS_ACCOUNT_NUMBER). Authentication is taken care of by setting the X-Binaris-Api-Key header when invoking MyFunction. Binaris functions provide access to environment variables by declaring them in the binaris.yml file and by setting their value either in the file itself or by reading them directly from environment variables on the machine they are deployed from. To declare the environment variables, edit the binaris.yml file as below:

functions:
  MyCaller:
    file: function.js
    entrypoint: handler
    runtime: node8
+   env:
+     BINARIS_API_KEY: 
+     BINARIS_ACCOUNT_NUMBER: 

At deploy time the CLI reads and parses the binaris.yml. If the value of a variable is missing it will use an environment variable of the same name. This means you’ll need BINARIS_ACCOUNT_NUMBER and BINARIS_API_KEY in your environment.

Note: if you used bn login during the first tutorial, the following commands are sufficient to set the variable values.

$ export BINARIS_ACCOUNT_NUMBER=$(awk 'BEGIN {FS=":"}{if ($1 == "accountId") print substr($2,3,length($2)-3)}' ~/.binaris.yml)
$ export BINARIS_API_KEY=$(awk 'BEGIN {FS=":"}{if ($1 == "apiKey") print substr($2,2)}' ~/.binaris.yml)

You can populate the values for an environment variable directly in the binaris.yml file as well.

Deploy and Invoke the function

$ bn deploy MyCaller
Deployed function MyCaller
Invoke with one of:
  "bn invoke MyCaller"
  "curl -H X-Binaris-Api-Key:<Your_API_Key> https://run-sandbox.binaris.com/v2/run/<Your_Account_Number>/MyCaller"

bn invoke MyCaller --data '{ "name" : "binaris" }'
"\"Hello BINARIS!\""

You now have a function deployed that calls another function!

Tip

You can use bn --help to see all the available CLI commands.

Conclusion

Welcome to Binaris! By following the getting started tutorials you have:

  • Deployed your first function.
  • Edited and monitored it.
  • Learned how to call a function from a function.
  • Used dependencies in your code.
  • Leveraged environment variables to store sensitive information.

What’s next

To learn more about Binaris functions, review the below documents: