Maven Deployment package and upload to AWS-Lambda

2020-06-06 20:29发布

I'm facing issue in uploading the maven deployment package to Amazon s3.

From Eclipse, I'm able to generate the .jar file successfully, however I'm facing issue in uploading to server.

Here is my Java code:

package main.java.mavantestproj;

import java.util.Map;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.lambda.runtime.Context;

public class LambdaFunctionHandler {


    public String handleRequest(Map<String,Object> input, Context context) {
        context.getLogger().log("Input: " + input);

        AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider("mytest"));

        client.setRegion(com.amazonaws.regions.Region.getRegion(Regions.US_WEST_2));

        client.describeTable("ProductCatalog");
        // TODO: implement your handler
        return null;
    }

}

in target folder i have got 2 jar's. ie lambda-java-example-1.0-SNAPSHOT.jar & original-lambda-java-example-1.0-SNAPSHOT.jar

In this first jar is 35MB & second one is 4KB. I'm not getting which one to upload to S3 to run my lambda function.

3条回答
Melony?
2楼-- · 2020-06-06 20:47

You definitely need the large "uber-jar" so your dependency classes will be included, but there is an alternative way to package things up for AWS-Lambda using the Maven assembly plugin instead of the Shade plugin. You end up with an AWS lambda deployment package in .zip format instead of a single .jar file. It will look a little more like a JEE .war file with all the original .jar dependencies kept intact, and you can include other stuff like properties files that end up unpacked in the file-system where the lambda runs (which may be a little easier to find and load in your code). If you're interested in the details, there's a blog post about it here: http://whirlysworld.blogspot.com/2016/03/aws-lambda-java-deployment-maven-build.html Also, packaging a Lambda function app this way makes it WAY easier to peek into the zip file and figure out which dependency jars are being included, and then identify the ones you might be able to exclude.

This still doesn't get Maven to handle the actual deployment of the package to AWS (create or update). Deploying, and capturing the deployment info (ARN, API-gateway app-id-url, etc.), seems to be the next thing for which Amazon hasn't provided a very clear answer or solution.

查看更多
够拽才男人
3楼-- · 2020-06-06 20:58

It seems standalone jar file built using shade plugin is sufficient as per this AWS documentation

查看更多
Deceive 欺骗
4楼-- · 2020-06-06 21:04

The larger JAR file that is being generated includes all of the library dependencies baked in. This is the one you will want to upload to S3 for use by AWS Lambda as these dependencies are required to run.

If you want to make this file smaller you can ensure you are only including libraries you need and remove any unnecessary ones. A common way to do this is with the AWS SDK only include the libraries for the specific services you need to call such as DynamoDB instead of including the entire AWS SDK.

查看更多
登录 后发表回答