Lambda

AWS Lambda is a “serverless” compute service—meaning that it processes functions and returns their results without needing a full operating system running on a server. These functions can be written in a variety of programming languages and can receive inputs from external services. They can return either an error or success response based on the inputs provided. Because of this server-side processing, Lambda functions can be used to extend the capabilities of what Skuid can accomplish in your frontend application experiences.

There are many types of server-side processes which should run on a server—not in the browser—such as complex data transformation, image or video manipulation, document generation, etc. By defining these processes as Lambda functions, you can invoke them from a variety of services, including Skuid. Instead of delving into specific Lambda uses, this doc covers the basics of configuration and use in Skuid.

For more information about how to create your functions, refer to AWS resources:

Configuration

First, configure an authentication provider using your AWS security credentials. After doing so, configure a data source following the instructions listed here—choosing Amazon Lambda as the data source type.

Warning

The region you select within the data source’s configuration must match the region in which you have configured the Lambda functions to be used within Skuid. Be sure to set this correctly.

AWS Access Permissions

When using AWS APIs, it is best practice to utilize strict and well-defined IAM policies so users can only perform actions they have explicit access to.

Below are the permissions this data source type requires to properly function. Use this list to better define your IAM roles.

  • lambda:ListFunctions
  • lambda:InvokeFunction

Data Source-Specific Properties

Data source actions

Every Lambda function configured within the selected AWS region will be listed as a data source action. Once you select a Lambda function to invoke, you will need to configure the following properties:

  • Payload Snippet: A Skuid JavaScript snippet used to construct the input payload that will be sent to, and processed by, the Lambda function.
  • Response handler Snippet: (optional) A Skuid JavaScript snippet that processes the payload, if any, returned by the Lambda function.

Like other Skuid snippets, response handler snippets may be asynchronous by returning a Promise in the snippet. Skuid will wait for the Promise to be resolved before considering the Lambda function invocation to be complete.

Using the AWS Lambda Data Source

Lambda is used exclusively through data source actions, available by selecting the Run Data Source Action action type and selecting a configured Lambda data source.

You must define and set a payload snippet to provide inputs to the Lambda function. You can optionally define a response handler snippet to handle the response from the Lambda function.

Payload snippets

A payload must always be constructed for Lambda actions, even if the function requires no input. To construct a payload, use a payload snippet to return some value. That value’s data type will depend upon your function’s chosen software development kit (SDK)—for example, a Python function may expect a list be returned.

For instances where you do not need input, a simple snippet will do. As an example:

1
return null

Otherwise, construct a more in-depth snippet to send along the proper input. A basic example passing in model data may look like:

1
2
3
var model = skuid.model.getModel('myModel'),
  firstRow = model.getFirstRow();
return firstRow.myFieldname;

This input data would then be available to the Lambda function within the event object.

A basic example of a Lambda function using data within the event object—written in a Node.JS 8.10 runtime—would look like this:

1
2
3
4
5
6
exports.handler = async (event) => {
  const response = {
    body: "I received this input: " + event
  };
  return response;
};

This function would simply respond with a message stating the value of what was passed in by the Skuid payload snippet.

Response handler snippets

If your Lambda function is returning data that will be used by models or components within the Skuid page, then use a response handler snippet to parse the function’s response.

Response handler snippets have access to the response sent by Lambda through arguments[0]. Reference AWS documentation for more information about the response elements.

A basic response handler snippet may look like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
var params = arguments[0],
  // And some variables for models if needed
  model = skuid.model.getModel('myModel'),
  firstRow = model.getFirstRow();

// To see each of the response elements
console.log(params);

// Or to log just the payload as written in the function
console.log(params.Payload);

// If the payload is a JSON object, parse it
var payload = JSON.parse(params.Payload);

// And, if applicable, update a model field with the payload's body
model.updateRow(firstRow, {ResponseFromLambda: payload.body});

Errors in Lambda functions

Note that Skuid will only return an “error” if there is a problem with Skuid receiving a response from Lambda, such as a network request error. Errors that come from logic within the function are considered successes by Skuid since they return a response.

Ensure the function’s error handling logic is within the function itself as opposed to the Skuid action list. Only use Skuid’s on-error actions for alerting the user that a network or processing error has occurred.

Troubleshooting

I do not see my functions available as data source actions. [[]]

  • Ensure you’ve selected the correct region in the data source’s configuration.
  • Ensure you have the appropriate AWS secret ID and key.
  • Ensure you have the appropriate IAM permissions granted.