discindo / r2lambda

Deploy an R script as AWS lambda from the R console
https://discindo.github.io/r2lambda/
GNU General Public License v3.0
33 stars 3 forks source link

Support for multiple R files and for renv::restore()? #25

Open slodge opened 11 months ago

slodge commented 11 months ago

I'm currently considering putting a PR together to add support for:

I was thinking of changing build_lambda (and create_lambda_dockerfile) to take an additional renv_folder parameter, and to allow multiple files in runtime_path - with either the first file as the main entrypoint or with some other way of indicating which file to treat as the main.

I think these should be fairly straightforward to add, but I thought I'd open the PR to check whether this is of interest to r2lambda - or if there are any special or design considerations - before I started on writing any code :+1:

teofiln commented 11 months ago

Hi @slodge Yes, those would be great additions. Please go ahead. Thanks!

teofiln commented 4 days ago

Hi! I added support for renv.lock. Something like this should work. Assuming a folder with runtime script and an renv.lock file.

runtime_function <- "parity"
runtime_path <- system.file("parity.R", package = "r2lambda")
renvlock_path <- system.file("renv.lock", package = "r2lambda")

# Might take a while, its building a docker image
build_lambda(
  tag = "parity1",
  runtime_function = runtime_function,
  runtime_path = runtime_path,
  renvlock_path = renvlock_path,
  dependencies = NULL
)
teofiln commented 3 days ago

Multi-file Lambdas should now work too. In the below example, we have two scripts.

support.r:

get_iris_summary_by_species <- function(species) {
    iris |>
    dplyr::filter(Species == species) |>
    dplyr::summarise(
      mean = mean(Sepal.Length),
      sd = sd(Sepal.Length)
    )
}

runtime.r:

source("support.r")

iris_summary <- function(species) {
  get_iris_summary_by_species(species)
}

lambdr::start_lambda()

renv is used to capture the dependencies in support.r and runtime.r producing an renv.lock that can be used inside the Lambda docker image.

Then the following should work:

dir("~/Desktop/iris-lambda")
runtime_function <- "iris_summary"
runtime_path <- "~/Desktop/iris-lambda/runtime.r"
support_path <- "~/Desktop/iris-lambda/support.r"
renvlock_path <- "~/Desktop/iris-lambda/renv.lock"
dependencies <- NULL

# Might take a while, its building a docker image
build_lambda(
  tag = "iris7",
  runtime_function = runtime_function,
  runtime_path = runtime_path,
  support_path = support_path,
  renvlock_path = renvlock_path,
  dependencies = dependencies
)

# test
payload <- list(species = "setosa")
tag <- "iris7"
test_lambda(tag = tag, payload)

# deploy

# Might take a while, its pushing it to a remote repository
deploy_lambda(
  tag = "iris7",
  Timeout = 30
)

invoke_lambda(
  function_name = "iris7",
  invocation_type = "RequestResponse",
  payload = list(species = "versicolor"),
  include_logs = FALSE
)

invoke_lambda(
  function_name = "iris7",
  invocation_type = "RequestResponse",
  payload = list(species = "setosa"),
  include_logs = FALSE
)
teofiln commented 3 days ago

Hi, @slodge please have a look at these updates related to renv and multi-file and let me know if things are working on your end.

Many thanks!