Consider the following runtime.R file:

parity <- function(number) {
  list(parity = if (as.integer(number) %% 2 == 0) "even" else "odd")
}

lambdr::start_lambda()

The parity function accepts a number argument and returns its parity as a named list, for example:

parity(5)
# $parity
# [1] "odd"


parity(8)
# $parity
# [1] "even"

This function can then be placed into a Docker image. An example is provided below, but the key components are:

  • Start from the public.ecr.aws/lambda/provided parent image, which provides the basic components necessary to serve a Lambda
  • Install R and dependencies, both system dependencies and R packages, including the lambdr package
  • Copy across runtime.R and any other necessary files
  • Generate a simple bootstrap which runs runtime.R with R
  • Set the handler as the CMD. The lambdr package interprets the handler as the name of the function to use, in this case, “parity”. The CMD can also be set (or overriden) when setting up the Lambda in AWS.

The image is built and uploaded to AWS Elastic Container Registry (ECR). First, a repository is created:

This provides a URI, the resource identifier of the created repository. The image can now be pushed:

In either the AWS console or the command line, a Lambda can be created from this image. Call the Lambda “parity” to match the function name. Tests can be executed within the console. Alternatively the Lambda can be invoked from the CLI:

The output is now available in the generated file: