AWS Lambda Connector

This connector can be used to access AWS Lambda. It is implemented using Amazon's Lambda SDK.

Events:

queueComplete Queue processing is complete

Actions:

command Run a CLI command on Lambda

create Create a new Lambda function

createCommandFunction Create a Lambda function to run a CLI command

createFromBuffer Create a new Lambda function from buffer

createFromCode Create a new Lambda function from code

createFromFile Create a new Lambda function from file

delete Delete a Lambda function

enqueue Process a queue of tasks in Lambda functions

getFunctionInfo Get detailed function information

invoke Execute a Lambda function

listFunctions List deployed functions

SDK:

sdk Get direct SDK access

Event Details

Queue Complete event

Example:

async (event) => {
  console.log(event.qid)
  console.log(event.payloads)
  console.log(event.resolutions)
}

This event is fired when a processing queue has completed processing all its payloads. See the enqueue action for details.

Action Details

Command action

(
  functionName: string,
  command: string,
  urls: string | string[] = [],
) => any

Usage:

const mediaInfo = await awsLambdaConnector.command(
  'reshuffle-command-mediainfo',
  `mediainfo --output=JSON <my-video>`,
  's3://<my-bucket>/<my-video>,
)

Run a CLI command deployed with the createCommandFunction action. This action invokes the Lambda function named functionName, loads the files with the specified urls into a temporary folder and runs the specified CLI command.

The standard output from the CLI command is collected and returned to the user. At this point, the action does not support collection of output files generated by the CLI command.

Note that the executable name in command must match the filename in the executable S3 URL passed to createCommandFunction.

Create action

Definition:

interface Payload {
  code?: string
  filename?: string
  buffer?: Buffer
}

(
  functionName: string,
  payload: Payload,
  options: object = {},
) => object

Usage:

const functionInfo = await AWSLambdaConnector.create(
  'toLowerUpperString',
  { code: `
    exports.handler = async (event) => {
      const str = event.str || 'Hello, world!'
      return {
        statusCode: 200,
        body: JSON.stringify({
          lower: str.toLowerCase(),
          upper: str.toUpperCase(),
        }),
      }
    }
  ` },
)

Create a new Lambda function with the given functionName. The code for the newly created function can be specified in on of three ways:

  • buffer a NodeJS buffer with zipped content (a package like JSZip can help)
  • code a string with code
  • filename a name of a file containing code

The optional options object may contain the following properties:

  • env - Environment variables
  • memorySize - Container memory size in MB (defaults to 256)
  • roleName - Function execution role (defaults to lambda_basic_execution)
  • runtime - Runtime environment (defaults to nodejs12.x)
  • tags - Tags
  • timeout - Execution timeout in seconds (default to 3)

The created function can be invoked using the invoke action, or tied to a myriad of AWS supported events.

We note that the AWS SDK provides many more options for creating and configuring Lambda functions. For example, functions can be deployed from an S3 zip file, allowing multiple files and dependencies to be deployed simultaneously. to leverage these capbilities, you can use the sdk action to gain direct access to the SDK and use its createFunction method directly.

Create Command Function action

Definition:

(
  functionName: string,
  executable: string,
  force = false,
) => object

Usage:

await AWSLambdaConnector.createCommandFunction(
  'reshuffle-command-mediainfo',
  's3://<my-bucket>/mediainfo',
)

Create a Lambda function to run a CLI command on given input. This action receives the desired name for the Lambda function and an S3 URL poiting to a command line executable. This executable needs to be compatible with the AWS Lambda Linux runtime environment.

The action packs the code and configuration required to run the command line executable and deploys them to Lambda under functionName. If a Lambda function with this name exists that was not previsouly deployed by the connector, deployment fails. If a Lambda exists that was deployed by the connector, then deployment is skipped unless force is set to true.

See command above on running CLI commands deployed with this action.

Create From Buffer action

Definition:

(
  functionName: string,
  buffer: Buffer,
  options: object = {},
) => object

Usage:

const zip = new require('JSZip')()
// ... add files ...
const buffer = await zip.generateAsync({ type: 'nodebuffer' })
const functionInfo = await AWSLambdaConnector.createFromBuffer(
  'toLowerUpperString',
  buffer,
)

Create a new Lambda function with the given functionName to execute the code in the specified buffer. See create above for more details.

Create From Code action

Definition:

(
  functionName: string,
  code: string,
  options: object = {},
) => object

Usage:

const functionInfo = await AWSLambdaConnector.createFromCode(
  'toLowerUpperString',
  `
  exports.handler = async (event) => {
    const str = event.str || 'Hello, world!'
    return {
      statusCode: 200,
      body: JSON.stringify({
        lower: str.toLowerCase(),
        upper: str.toUpperCase(),
      }),
    }
  }
  `,
)

Create a new Lambda function with the given functionName to execute the code in code. See create above for more details.

Create From File action

Definition:

(
  functionName: string,
  filename: string,
  options: object = {},
) => object

Usage:

const functionInfo = await AWSLambdaConnector.createFromFile(
  'toLowerUpperString',
  './toLowerUpperString.js',
)

Create a new Lambda function with the given functionName to execute the code inside the fiel filename. See create above for more details.

Delete action

Definition:

(
  functionName: string,
) => void

Usage:

await AWSLambdaConnector.delete('toLowerUpperString')

Delete the Lambda function with the name functionName.

Invoke action

Definition:

(
  functionName: string,
  payload: any|any[],
  maxConcurrent: number = 100,
) => string

Usage:

const qid = await AWSLambdaConnector.enqueue(
  'toLowerUpperString',
  [
    { str: 'Alpha' },
    { str: 'Beta' },
    { str: 'Gamma' },
  ],
)

Asynchronously process a series of tasks with the Lambda function named functionName. The payload is an array of elements, each would be passed in turn as an input to the Lambda function. If payload is scalar, only a single invocation will ensue.

The maxConcurrent argument can be used to limit the number of simultaneous invocaions of the Lambda function, with a hard limit of 100 per queue. Currently the connector does not enforce a global limit on the number of functions it invokes through this action.

When all payloads have been processed, the action triggers a queueComplete event with the queue ID, the payloads array and a resolutions array in the event object. Each resolution is either the value returned by the Lambda function or an Error object if the invocation failed.

Invoke action

Definition:

(
  functionName: string,
  requestPayload: any = {},
) => any

Usage:

const { lower, upper } = await AWSLambdaConnector.invoke(
  'toLowerUpperString',
  { str: 'My Awesome String' },
)

Invoke the Lambda function with the name functionName, passing it the payload provided in requestPayload. The payload can be any JSON serializable JavaScript object.

The invoke action returns the response payload returned by the Lambda function. In case of an error during invocation or execution of the function, this action throws an error.

Get Function Info action

Definition:

(
  functionName: string,
) => any

Usage:

const info = await AWSLambdaConnector.getFunctionInfo(
  'toLowerUpperString',
)

Get detailed information about the specified function.

List Functions action

Definition:

() => any[]

Usage:

const list = await AWSLambdaConnector.listFunctions()

Get information about deployed Lambda functions.

SDK Details

SDK action

Definition:

(
  options ?: object,
) => object

Usage:

const lambda = await AWSLambdaConnector.sdk()

Get the underlying SDK object. You can specify additional options to override or add to the required fields in the connector's configuration.