AWS Lambda - R runtime

0

Hi, Is there a way to create a lambda function for R runtime?

I tried using ARN arn:aws:lambda:$region:131329294410:layer:r-runtime-3_6_0:13 but it didn't work Ref: https://github.com/bakdata/aws-lambda-r-runtime

asked 10 months ago648 views
1 Answer
2

Try to build a custom runtime https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html

Create a Dockerfile that includes the R runtime and any required R packages.

FROM amazonlinux:2

# Install R and dependencies
RUN yum update -y && \
    yum install -y R R-devel gcc-c++ gcc-gfortran zlib-devel bzip2 bzip2-libs bzip2-devel \
    xz-libs xz-devel pcre2 pcre2-devel libcurl libcurl-devel && \
    yum clean all

# Install AWS Lambda runtime interface client
RUN yum install -y unzip && \
    curl -Lo /usr/local/bin/aws-lambda-rie \
    https://github.com/aws/aws-lambda-runtime-interface-emulator/releases/latest/download/aws-lambda-rie && \
    chmod +x /usr/local/bin/aws-lambda-rie

# Copy R scripts
COPY . /var/task

# Set the entrypoint
ENTRYPOINT ["/usr/local/bin/aws-lambda-rie", "sh", "-c", "/usr/bin/Rscript /var/task/lambda_function.R"]

Create a file named lambda_function.R with your R code.

handler <- function(event, context) {
  message("Hello from R!")
  return(list(statusCode=200, body="Hello from R!"))
}

lambda_handler <- function() {
  raw_event <- readLines(file("stdin"))
  event <- jsonlite::fromJSON(raw_event)
  response <- handler(event, NULL)
  jsonlite::toJSON(response, auto_unbox = TRUE)
}

lambda_handler()

Build the Docker image

docker build -t my-r-lambda .

Push the Docker image to Amazon ECR

aws ecr create-repository --repository-name my-r-lambda
$(aws ecr get-login --no-include-email --region <your-region>)
docker tag my-r-lambda:latest <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-r-lambda:latest
docker push <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-r-lambda:latest

Create the Lambda function using the ECR image

aws lambda create-function \
  --function-name my-r-lambda \
  --package-type Image \
  --code ImageUri=<your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-r-lambda:latest \
  --role arn:aws:iam::<your-account-id>:role/<your-lambda-role>
profile picture
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 10 months ago
  • Thanks! I was able to create the docker image and push it to ECR following the exacts steps above.

    Just a note here: I believe this command is deprecated $(aws ecr get-login --no-include-email --region <your-region>) This worked for me: aws ecr get-login-password --region <your-region> | docker login --username AWS --password-stdin <your-account-d>.dkr.ecr.<your-region>.amazonaws.com

    Upon testing the lambda, I received this error "Runtime API Server failed to listen error=listen tcp 127.0.0.1:<port-no>: bind: address already in use" Do you know how to resolve this issue?

    There's no other service listening on that port number: sudo lsof -i -P -n | grep LISTEN

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions