Dharmesh Vasani
Dharmesh Vasani

Reputation: 475

How to execute .jar file from AWS Lambda Serverless?

I have tried with following code.

var exec = require('child_process').execFile;
  var runCmd = 'java -jar ' + process.env.LAMBDA_TASK_ROOT + '/src/' + 'myjar.jar'

  exec(runCmd,
    function (err, resp) {
      if (err) {
        cb(null, { err: err})

      } else {
        cb(null, { resp: resp})
      }
    )

Here, I have put my jar file in the root folder and src folder also. but it is giving my following error. I have already added the.jar file with the code.but i got following error.

"err": {
    "code": "ENOENT",
    "errno": "ENOENT",
    "syscall": "spawn java -jar /var/task/src/myjar.jar",
    "path": "java -jar /var/task/src/myjar.jar",
    "spawnargs": [],
    "cmd": "java -jar /var/task/src/myjar.jar"
}

So How, Can I execute this .jar file in AWS Lambda environment? Please help me.

Upvotes: 9

Views: 12916

Answers (3)

Zachary Ryan Smith
Zachary Ryan Smith

Reputation: 2758

In addition to the other answers: Since 2020 December, Lambda supports container images: https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/

Ex.: I created a container image using AWS's open-source base image for python, adding a line to install java. One thing my python code did was execute a .jar file using a sys call.

Upvotes: 1

Kosmonaut
Kosmonaut

Reputation: 2290

With Lambda Layers you can now bring in multiple runtimes.

https://github.com/lambci/yumda and https://github.com/mthenw/awesome-layers both have a lot of prebuilt packages that you can use to create a layer so you have a second runtime available in your environment.

For instance, I'm currently working on a project that uses the Ruby 2.5 runtime on top of a custom layer built from lambci/yumbda to provide Java.

    mkdir dependencies
    docker run --rm -v "$PWD"/dependencies:/lambda/opt lambci/yumda:1 yum install -y java-1.8.0-openjdk-devel.x86_64
    cd dependencies
    zip -yr ../javaLayer .
  1. upload javaLayer.zip to aws lambda as a layer
  2. add layer to your function
  3. within your function, java will be located at /opt/lib/jvm/{YOUR_SPECIFIC_JAVA_VERSION}/jre/bin/java

Upvotes: 7

Sandeep Singh
Sandeep Singh

Reputation: 783

AWS Lambda lets you select a runtime at the time of creation of that lambda function, or later you can change it again. enter image description here

So, as you are running the Lambda function with NodeJs runtime, the container will not have Java runtime available to it.

You can only have one type of runtime in one container in case of AWS Lambda.

So, Create a separate Lambda with the Jar file that you want to run having Java as the runtime and then you can trigger that lambda function from your current NodeJS lambda function if that's what you ultimately want.

Following is an example of how you can call another Lambda function using NodeJS

var aws = require('aws-sdk');
var lambda = new aws.Lambda({
  region: 'put_your_region_here'
});

lambda.invoke({
  FunctionName: 'lambda_function_name',
  Payload: JSON.stringify(event, null, 2)
}, function(error, data) {
  if (error) {
    context.done('error', error);
  }
  if(data.Payload){
   context.succeed(data.Payload)
  }
});

You can refer to the official documentation for more details.

Upvotes: 5

Related Questions