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 can use Python packages installed with pip tool. To demonstrate that, you will build a function that leverages the python-string-utils Python package.

Create the function

$ mkdir MyFunction
$ cd MyFunction
$ bn create python2 MyFunction

Install the python-string-utils package to the function folder

$ pip install python-string-utils -t .

Edit your function to use python-string-utils

Replace the code in function.py with:

import string_utils

def handler(body, req):
    name = req.query.get('name') or body.get('name') or 'World'
    rev = string_utils.reverse(name)
    return 'Hello {}!'.format(rev)

Deploy and Invoke the function

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

$ bn invoke MyFunction --data '{"name":"world"}'
"Hello dlrow!"

You have just reversed your input string by using the python-string-utils 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 python2 MyCaller

Install the ‘requests’ package

You will need this module to call MyFunction.

$ pip install requests -t .

Edit ‘MyCaller’ to call ‘MyFunction’

Replace the code in function.py with

import os
import requests

def handler(body, req):
    name = req.query.get('name') or body.get('name') or 'World'
    url = 'https://run-sandbox.binaris.com/v2/run/{}/MyFunction?name={}'.format(os.environ['BINARIS_ACCOUNT_ID'], name)
    headers = { 
      'X-Binaris-Api-Key': os.environ['BINARIS_API_KEY'],
      'Content-Type': 'application/json'
    }
    resp = requests.post(url, headers=headers)
    return resp.json()

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 (os.environ['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.py
    entrypoint: handler
    runtime: python2
+   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=$(awk 'BEGIN {FS=":"}{if ($1 == "apiKey") print substr($2,2)}' ~/.binaris.yml)

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:<Your_API_Key> https://run-sandbox.binaris.com/v2/run/<Your_Account_Number>/MyCaller"

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

You now have a function deployed that calls another function!

3. 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: