简体   繁体   English

使用 Maven 进行 AWS Java Lambda 依赖项 JAR 大小管理

[英]AWS Java Lambda dependency JAR size management with Maven

I have a Lambda function that takes around ~10 seconds to start up using Java-11.我有一个 Lambda 函数,使用 Java-11 启动大约需要 10 秒。 I did some googling and came across a number of posts that suggest that lowering the JAR size of the package may help with quicker start times (less redundant libraries loaded etc...).我做了一些谷歌搜索,发现了一些帖子,这些帖子表明降低包的 JAR 大小可能有助于加快启动时间(减少加载的冗余库等......)。

I also read in some posts that using below may help and tried to add我还在一些帖子中读到使用下面可能会有所帮助并尝试添加

<scope>provided</scope>

in the AWS related dependencies thinking that well...AWS Lambda would have AWS specific libraries present?在与 AWS 相关的依赖项中考虑得很好……AWS Lambda 会存在 AWS 特定的库吗? Turns out that is not the case!事实证明并非如此! adding scope provided does not work when trying to execute the function.尝试执行该功能时,添加提供的范围不起作用。

My current pom.xml is as follows:我当前的pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>REMOVED</groupId>
    <artifactId>REMOVED</artifactId>
    <version>REMOVED</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.7.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20220320</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.kinesis</groupId>
            <artifactId>amazon-kinesis-client</artifactId>
            <version>2.4.1</version>

        </dependency><dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-kinesis</artifactId>
        <version>1.12.228</version>
        </dependency>
        <!-- <dependency><groupId>software.amazon.awssdk</groupId><artifactId>firehose</artifactId><version>2.17.198</version></dependency> -->
        <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/kinesis -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>kinesis</artifactId>
            <version>2.17.201</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/secretsmanager -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>secretsmanager</artifactId>
            <version>2.17.204</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

AWS Kinesis usage is just to create the KinesisClient and use PutRecordRequest and PutRecordResponse, with AWS Secret Manager , just to access specific secrets. AWS Kinesis 的使用只是创建 KinesisClient 并使用 PutRecordRequest 和 PutRecordResponse 以及 AWS Secret Manager 来访问特定的秘密。

I am not that versed in AWS API, from my understanding I am already including a limited amount of libraries required to perform those tasks based on the dependencies.我不是很精通 AWS API,据我了解,我已经包含了基于依赖项执行这些任务所需的数量有限的库。

The JAR file is about 65 MB large. JAR 文件大小约为 65 MB。 Would I be able to optimise dependency loads further?我可以进一步优化依赖负载吗? And I guess would lowering the JAR file increase start up time of the Lambda function?我想降低 JAR 文件会增加 Lambda 函数的启动时间吗?

Thank you,谢谢,

I think you've accidentally included three copies of the same dependency.我认为您不小心包含了相同依赖项的三个副本。 You just want the latest 2.xx version of the Kinesis library.您只需要最新的 2.xx 版本的 Kinesis 库。

You certainly don't need a 1.xx SDK and a 2.xx SDK.您当然不需要 1.xx SDK 和 2.xx SDK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM