The Lambda extension

This service extension integrates AWS Lambda functionality into SPARKL mixes allowing you to run Node.js and Python scripts in a serverless architecture.

See Lambda AWS documentation for further details.

Using the Lambda extension

<service name="Lambda" 
    provision="lambda">
    <prop name="lambda.spec"/>
    <prop name="Test.main.py" 
        reply="replyname" 
        type="lambda.function"><![CDATA[
def handle(event, context):
    n = event.get('n')
    div = event.get('div')	

    if div ** 2  > n:
        return { 
            'replyname':"Yes"
        }
    if n % div == 0:
        return { 
            'replyname':"No"
        }
    return { 
        'replyname':"Maybe"
    }
    ]]></prop>
</service>
...
<request name="Test" 
    service="Lambda" 
    fields="div n">
    <reply name="Yes" 
        fields="YES"/>
    <reply name="No" 
        fields="NO"/>
    <reply name="Maybe" 
        fields="MAYBE"/>
</request>
Note: The above sample is from a Lambda-Python version of the Primes example. Get the full example from the SPARKL public repository.
Table 1. Properties
Property Description Example
lambda.spec The AWS lambda configuration. Specify it on the service level and optionally on the operations too.

If specified on operation level, it overrides the service-level specification for that particular operation.

The property can have the following attributes, all of which are optional:

function (optional)
For operations only. Specifies a function name that should match the [FunName] in the [FunName].[mainFileName] property.
  • Default: The name of the operation. For example: [FunName] would be Test for the sample request on the PackagesTester service if we did not specify the packagetest function.
timeout (optional)
Timeout in seconds.
  • Default: 3
  • Maximum: 300
memory (optional)
Memory allocated to the AWS lambda service in megabytes.
  • Default: 128
runtime (optional)
Runtime environment.
  • Default: nodejs6.10
  • Possible values:
    • python (Python 2.7)
    • nodejs6.10 (Node.js 6.10)
<service name="PackagesTester" 
  provision="lambda">
  <prop name="lambda.spec" 
    timeout="30"/>
  <prop name="packagetest.index.js" 
    type="lambda.function" 
    packages="request"><![CDATA[
exports.handle = function(e, ctx, cb) {
  var request = require('request');
  request('http://httpbin.org/get', 
    function (error, response, body) {
	var json = JSON.parse(body);
       cb(null, {'response': json})
  });
}
  ]]></prop>
</service>
...
<request name="Test" 
  service="PackagesTester" 
  fields="PKG_TEST">
  <prop name="lambda.spec" 
    function="packagetest"/>
  <reply name="Ok" 
    fields="response"/>
</request>
[FunName].[mainFileName] A lambda function on the service. Any lambda service can have one or more such functions.
[FunName]
The name of the function. Any operation can reference it through the lambda.spec property and the function attribute. By default, every operation specifies its name as the value of function.
[mainFileName]
Depending on the language it can be:
  • index.js for Node.js
  • main.py for Python
Possible attributes:
type (mandatory)
Its value must be lambda.function.
reply (optional)
Specifies a key to which you can bind reply name values. By default, the Ok reply name is assumed.
packages (optional)
A list of dependency libraries the script imports.
url (optional)
Instead of including the logic (script) as the content of the property, references it through path.
<prop name="firstDiv.index.js" 
  content-type="application/javascript" 
  reply="replyname"
  packages="math" 
  type="lambda.function"><![CDATA[
​import math
exports.handle = function(e, ctx, cb) {
    var n = e['n'];
    var div = 2;     
    
    cb(null, {'div': div, 'replyname': "Ok"})
}
]]></prop>
[FunName].[depFileName] Additional code for the specified function.

Attributes:

type (mandatory)
Its value must be lambda.dependency.
url (optional)
Instead of including the logic (script) as the content of the property, references it through path.
<prop name="someFun.index.js" 
  content-type="application/javascript" 
  reply="replyname" 
  type="lambda.function"><![CDATA[
​...
]]></prop>
<prop name="someFun.other.js" 
  content-type="application/javascript" 
  type="lambda.dependency"><![CDATA[
​...
]]></prop>