Call a function from another function
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 it is 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
$ 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 MyFunction
Deployed function MyFunction
Invoke with one of:
"bn invoke MyFunction"
"curl -H X-Binaris-Api-Key:$(bn show apiKey) https://run.binaris.com/v2/run/<Your_Account_Number>/MyFunction"
$ bn invoke 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 MyFunction
asynchronously.
$ npm install request request-promise-native
Edit ‘MyCaller’ to call ‘MyFunction’
Replace the code in function.js
with
const rp = require('request-promise-native');
const https = require('https');
const keepAliveAgent = new https.Agent({ keepAlive: true });
exports.handler = async (body, context) => {
const name = context.request.query.name || body.name || 'World';
const options = {
url: `https://run.binaris.com/v2/run/${process.env.BINARIS_ACCOUNT_ID}/MyFunction?name=${name}`,
headers: {
'X-Binaris-Api-Key': `${process.env.BINARIS_API_KEY}`,
'Content-Type': 'application/json',
},
agent: keepAliveAgent,
};
return rp.post(options);
};
Note: keep alive agent usage is recommended for improved performance when invoking the same function multiple times.
Use an environment variable
Before deploying MyCaller
you should store your Binaris Account API Key in an environment variable. Binaris Account ID is available in the BINARIS_ACCOUNT_ID
variable automatically. 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 ID portion of the URL from the BINARIS_ACCOUNT_ID
environment variable (process.env.BINARIS_ACCOUNT_ID
). 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 variable, edit the binaris.yml
file as below:
functions:
MyCaller:
file: function.js
entrypoint: handler
runtime: node8
+ env:
+ BINARIS_API_KEY:
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_API_KEY
in your environment.
Note: if you used bn login
during the first tutorial, the following command is sufficient to set the variable value.
$ export BINARIS_API_KEY=$(bn show apiKey)
You can populate the values for environment variables 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:$(bn show apiKey) https://run.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!
3. Use SDK
Instead of hand-crafting the request options and calling a function directly by URL, it is possible to use Binaris SDK to invoke a function from another function. In this section, you will edit the MyCaller
function to use SDK to call the MyFunction
function.
Install the ‘binaris’ module
This module contains Binaris SDK.
$ npm install binaris
Edit ‘MyCaller’ to use Binaris SDK
Replace the code in function.js
with
const { invoke } = require('binaris/sdk');
const {
BINARIS_ACCOUNT_ID,
BINARIS_API_KEY,
} = process.env;
exports.handler = async (body, context) => {
const name = context.request.query.name || body.name || 'World';
const request = { name };
const response = await invoke(BINARIS_ACCOUNT_ID, 'MyFunction', BINARIS_API_KEY, JSON.stringify(request));
return JSON.parse(response.body);
};
Note that the same environment variables are used for the invoke
function.
Deploy and Invoke the function
$ bn deploy MyCaller
Deployed function MyCaller
Invoke with one of:
"bn invoke MyCaller"
"curl -H X-Binaris-Api-Key:$(bn show apiKey) https://run.binaris.com/v2/run/<Your_Account_Number>/MyCaller"
bn invoke MyCaller --data '{ "name" : "binaris sdk" }'
"Hello BINARIS SDK!"
The function call still works, even though there is no hard-coded URL in the code.
4. Cleanup
$ bn remove MyFunction
Removed function MyFunction
$ bn remove MyCaller
Removed function MyCaller
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: