Skip to content

R script in lambda

0

I'm coding in Java. I need to run a R script in lambda.

I've created the below Docker file

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 java-17-openjdk java-17-openjdk-devel && \
    yum clean all

# Install AWS Lambda runtime interface emulator (for local testing)
RUN yum install -y unzip curl && \
    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 application JAR and R scripts into Lambda task root
COPY target/awsLambdaHandler-1.0-SNAPSHOT.jar /var/task/
COPY adf_test.R /var/task/

# Set the Lambda handler (Java entry point)
# Replace with your actual handler class name
ENV LAMBDA_TASK_ROOT=/var/task
ENV HANDLER_CLASS=awslambda::handleRequest

# Entrypoint: run Lambda RIC with Java
ENTRYPOINT ["/usr/local/bin/aws-lambda-rie", \
            "java", "-cp", "/var/task/awsLambdaHandler-1.0-SNAPSHOT.jar", \
            "com.amazonaws.services.lambda.runtime.api.client.AWSLambda"]

I manage to build, I manage to push, however when I try to create the lambda function using the ECR image I get the error:

aws: [ERROR]: An error occurred (InvalidParameterValueException) when calling the CreateFunction operation: The image manifest, config or layer media type for the source image <your-account-id>.dkr.ecr.<your-region>.amazonaws.com/my-lambda-adf:latest is not supported.

Am I doing something obviously wrong? Is it actually possible to run R scripts in lambda?

2 Answers
1

The error message concerning "media type" or "manifest" often occurs because modern Docker versions (especially on Mac/Windows) include metadata called build attestations that AWS Lambda's container registry cannot yet process.

To fix the image compatibility issue, try rebuilding and pushing your image with the --provenance=false flag:

docker buildx build --platform linux/amd64 --provenance=false -t your-account-id.dkr.ecr.your-region.amazonaws.com/my-lambda-adf:latest .

Regarding the R runtime in Lambda:

Since you are using Java to trigger R, ensure that your R script does not attempt to write data to the current directory. In AWS Lambda, only the /tmp directory is writable. If your R script generates temporary files or plots, you must explicitly set the working directory or output path to /tmp within your R code or via ProcessBuilder arguments in Java.

Lastly, double-check that you are using Amazon Linux 2023 (if using Java 21) or sticking to Amazon Linux 2 (for Java 17), as the R package dependencies (yum install) vary slightly between these versions.

EXPERT
answered 12 days ago
  • Hi Florian, thanks for the quick answer I confirm that my R script doesn't write to the current directory, and Amazon Linux 2 is the correct one for me. I tried your suggestion to replace the build command, followed by this push command: docker push <my-account>.dkr.ecr.<my-region>.amazonaws.com/my-lambda-adf:latest But I still get the same error. It looks there are still issues with my Docker file (first time trying to use one)

0

Yes, it's definitely possible to run R scripts in AWS Lambda using container images. The error you're encountering suggests an issue with how the Docker image is being built or pushed to ECR, rather than a fundamental limitation.

The main issue with your Dockerfile is the ENTRYPOINT configuration. You're trying to use the Lambda Runtime Interface Emulator (RIE) as the entrypoint, but for actual Lambda deployment, you need to use the AWS Lambda Runtime Interface Client (RIC) for Java.

Here are the key problems and solutions:

1. Use the correct base image approach: For Java Lambda functions with custom dependencies like R, you should either:

  • Start from a proper Lambda base image that includes the Java runtime
  • Or ensure your custom image properly implements the Lambda Runtime API

2. Fix your ENTRYPOINT: Your current ENTRYPOINT is configured for local testing with RIE, but Lambda expects a different setup. For a Java-based Lambda function, you typically need to:

  • Use java to run your handler class directly
  • Ensure your JAR includes the AWS Lambda Java runtime dependencies

3. Add required Lambda libraries: Make sure your Maven/Gradle build includes the necessary Lambda dependencies:

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.2</version>
</dependency>

4. Correct Docker configuration: Your Dockerfile should look more like this:

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 java-17-openjdk java-17-openjdk-devel && \
yum clean all

# Copy application files
COPY target/awsLambdaHandler-1.0-SNAPSHOT.jar ${LAMBDA_TASK_ROOT}/lib/
COPY adf_test.R ${LAMBDA_TASK_ROOT}/

WORKDIR ${LAMBDA_TASK_ROOT}

# Set the CMD to your handler
CMD ["your.package.name.Handler::handleRequest"]

5. Handler specification: When creating the Lambda function, specify your handler in the format: your.package.name.Handler::handleRequest

6. Verify image compatibility: Ensure you're building for the correct architecture (x86_64 or arm64) and that your Docker build command includes the proper platform flag if needed:

docker build --platform linux/amd64 -t my-lambda-adf .

The combination of Java and R in Lambda is valid - your Java handler can invoke R scripts using ProcessBuilder or similar mechanisms to execute the R runtime with your script as an argument.
Sources
Building Lambda functions with Java - AWS Lambda

answered 13 days ago
EXPERT
reviewed 13 days ago

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.