This is an attempt to get an R runtime and function working on AWS Lambda using a container. My blog post on this subject is available here, but the code has since been updated.
Thanks to @rensa for contributions to the error handling code.
An attempt to get an R runtime and function working on AWS Lambda using a container.
Home Page: https://mdneuzerling.com/post/r-on-aws-lambda-with-containers/
License: MIT License
This is an attempt to get an R runtime and function working on AWS Lambda using a container. My blog post on this subject is available here, but the code has since been updated.
Thanks to @rensa for contributions to the error handling code.
I am trying to understand this code to implement it myself.
One of the parts that I am unclear on is the _HANDLER
. It appears that it somehow identifies the R functions that exist in functions.R
. The runtime.R
calls Sys.getenv("_HANDLER")
however there is not an .Renviron
file to go along with it. Are you able to provide an example value or explain a bit more how it works?
Thanks!
Hello. I have been trying to run this locally in windows machine but was unable to do so.
I follow the creating of the docker file and the r files as per the example in the repository.
Dockerfile:
FROM public.ecr.aws/lambda/provided:latest
ENV R_VERSION=4.0.3
RUN yum -y install wget
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'), repos = 'https://cloud.r-project.org/')"
#COPY runtime.R functions.R ${LAMBDA_TASK_ROOT}/
COPY runtime.r ${LAMBDA_TASK_ROOT}/
COPY functions.r ${LAMBDA_TASK_ROOT}/
RUN chmod 755 -R ${LAMBDA_TASK_ROOT}/
RUN printf '#!/bin/sh\ncd $LAMBDA_TASK_ROOT\nRscript runtime.R' > /var/runtime/bootstrap \
&& chmod +x /var/runtime/bootstrap
functions.R
#' Determine if the given integer is even or odd
#'
#' @param number Integer
#'
#' @return "even" or "odd"
#' @export
#'
#' @examples
#' parity(3) # odd
#' parity(4) # even
parity <- function(number) {
list(parity = if (as.integer(number) %% 2 == 0) "even" else "odd")
}
#' A nullary function that returns the current version of R
#'
#' @return character
#' @export
#'
#' @examples
#' hello()
hello <- function() {
list(response = paste("Hello from", version$version.string))
}
and runtime.R as in the repository,
https://github.com/mdneuzerling/r-on-lambda/blob/main/runtime.R
Then in PowerShell I run the following successfully.
docker build -t mdneuzerling/r-on-lambda .
docker run -p 9000:8080 mdneuzerling/r-on-lambda "functions.parity"
and in a different PowerShell window the following command,
& 'C:\Program Files\Git\mingw64\bin\curl' -i -X POST -H "Content-Type:application/json" -d '{"number": 5}' "http://localhost:9000/2015-03-31/functions/function/invocations"
which gives me the following result:
HTTP/1.1 200 OK
Date: Tue, 08 Mar 2022 14:43:29 GMT
Content-Length: 0
and in the PowerShell where the docker was build and run I get the following message:
08 Mar 2022 15:21:49,924 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/var/task, handler=)
START RequestId: 9274a11f-ad28-4091-adf1-eb9e9ebd0107 Version: $LATEST
08 Mar 2022 15:22:01,741 [INFO] (rapid) extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
08 Mar 2022 15:22:01,741 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
Fatal error: cannot open file 'runtime.R': No such file or directory
08 Mar 2022 15:22:01,759 [WARNING] (rapid) First fatal error stored in appctx: Runtime.ExitError
08 Mar 2022 15:22:01,759 [WARNING] (rapid) Process 15(bootstrap) exited: Runtime exited with error: exit status 2
08 Mar 2022 15:22:01,759 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 2 InvokeID=
08 Mar 2022 15:22:01,759 [WARNING] (rapid) Reset initiated: ReserveFail
08 Mar 2022 15:22:01,759 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
Fatal error: cannot open file 'runtime.R': No such file or directory
08 Mar 2022 15:22:01,775 [WARNING] (rapid) First fatal error stored in appctx: Runtime.ExitError
08 Mar 2022 15:22:01,775 [WARNING] (rapid) Process 30(bootstrap) exited: Runtime exited with error: exit status 2
END RequestId: b86265e8-e17d-4894-a410-dc743cb434bc
REPORT RequestId: b86265e8-e17d-4894-a410-dc743cb434bc Init Duration: 0.14 ms Duration: 33.99 ms Billed Duration: 34 ms Memory Size: 3008 MB Max Memory Used: 3008 MB
Is there something I am missing? cannot get it to return the result 'odd'. The message "First fatal error stored in appctx: Runtime.ExitError" does not seem to appear in the example.
I also tried to push the image to aws-ecr and creating a function in aws-lambda as per the example unsuccessfully.
Thank you in advance
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.