Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 665
Dharmesh Vasani Avatar asked Mar 23 '18 07:03

Dharmesh Vasani


People also ask

Can Lambda run jar file?

Lambda supports two types of deployment packages: container images and . zip file archives. This page describes how to create your deployment package as a . zip file or Jar file, and then use the deployment package to deploy your function code to AWS Lambda using the AWS Command Line Interface (AWS CLI).

Can Lambda run executable?

Running executables Meaning you can use your Lambda function running the mentioned languages, but sometimes you need to execute your function with a tool that is run through the command line, like a bash file.

How do I download a Lambda jar file?

Click on the layer's name and scroll down to the All versions section. Click on the version number that corresponds to your function's layer version. Finally, click on the Download button. Extract the zip file and add the lambda layer to your layers directory.


3 Answers

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
like image 155
Kosmonaut Avatar answered Sep 18 '22 11:09

Kosmonaut


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.

like image 20
Sandeep Singh Avatar answered Sep 18 '22 11:09

Sandeep Singh


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.

like image 28
Zachary Ryan Smith Avatar answered Sep 19 '22 11:09

Zachary Ryan Smith