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:
This function can then be placed into a Docker image. An example is provided below, but the key components are:
public.ecr.aws/lambda/provided:al2
parent image, which provides the basic components necessary to serve a
Lambdalambdr
packageruntime.R
and any other necessary
filesruntime.R
with
RCMD
. 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.FROM public.ecr.aws/lambda/provided:al2
ENV R_VERSION=4.0.3
RUN yum -y install wget git tar
RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
&& wget https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm \
&& yum -y install R-${R_VERSION}-1-1.x86_64.rpm \
&& rm R-${R_VERSION}-1-1.x86_64.rpm
ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/"
# System requirements for R packages
RUN yum -y install openssl-devel
RUN Rscript -e "install.packages(c('httr', 'jsonlite', 'logger', 'remotes'), repos = 'https://packagemanager.rstudio.com/all/__linux__/centos7/latest')"
RUN Rscript -e "remotes::install_github('mdneuzerling/lambdr')"
RUN mkdir /lambda
COPY runtime.R /lambda
RUN chmod 755 -R /lambda
RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \
&& chmod +x /var/runtime/bootstrap
CMD ["parity"]
The image is built and uploaded to AWS Elastic Container Registry (ECR). First, a repository is created:
aws ecr create-repository --repository-name parity-lambda --image-scanning-configuration scanOnPush=true
This provides a URI, the resource identifier of the created repository. The image can now be pushed:
docker tag mdneuzerling/r-on-lambda:latest {URI}/parity-lambda:latest
aws ecr get-login-password | docker login --username AWS --password-stdin {URI}
docker push {URI}/parity-lambda:latest
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:
aws lambda invoke --function-name parity \
--invocation-type RequestResponse --payload '{"number": 8}' \
/tmp/response.json --cli-binary-format raw-in-base64-out
The output is now available in the generated file: