使用maven为awslamd创建python部署包

2024-10-02 10:25:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在awslambda上部署python包。使用virtualenv和zip工具,我可以创建一个lambda zip文件,以便上传到aws并运行它。我使用以下博客创建包:https://joarleymoraes.com/hassle-free-python-lambda-deployment/。在

但是,我需要使用一个构建工具来将我的代码与我公司的Jenkins集成。我们使用maven构建,我也需要遵循同样的方法。在

我查看了maven-exec-plugin,以了解如何按照博客中的步骤使用virtual env并创建zip文件。但是,我无法遵循maven教程中提到的步骤。在

有人遇到过同样的问题吗?如果是,你是怎么解决的?在中,您如何配置pom.xml文件为pythonawslambdas创建部署包?在

我们将非常感谢您的快速帮助。谢谢


Tags: 文件工具lambdahttpscomawsfreevirtualenv
1条回答
网友
1楼 · 发布于 2024-10-02 10:25:24

让我把这个问题分成两部分。首先,对于python lambda,必须创建一个zip文件。为此,我建议使用Maven Assembly Plugin。在

样品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>
    <parent>
        <groupId>com.mygroup</groupId>
        <artifactId>parent-pom</artifactId>
        <version>0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>LambdaInstall</artifactId>
    <name>Python Lambda Installation Package</name>
    <packaging>pom</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
          <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <id>install-assembly</id>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
                <configuration>
                  <appendAssemblyId>false</appendAssemblyId>
                  <descriptors>
                    <descriptor>assembly.xml</descriptor>
                  </descriptors>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
    </build>
</project>

样品程序集.xml公司名称:

^{pr2}$

如果lambda只使用标准python库,那么这就是您所需要的全部。当您需要bot3或其他一些AWS没有在lambda节点上预先安装的库时,复杂性就会出现。为此,您必须将库添加到zip文件中。在

我希望有一种方法可以在任何开发人员的PC上,甚至在Jenkins节点上都能工作,所以我编写了一个小python应用程序,它可以找到库的位置(在site packages目录下)并将其复制到应用程序目标目录中。在这个例子中,我需要jwt:

import jwt
import distutils
from distutils import dir_util

# This will copy the jwt package into the target directory so it is included in the zip file
jwtPath = jwt.__path__[0]
distutils.dir_util.copy_tree(jwtPath, "./target/jwt")

这个python程序是通过将Maven Exec Plugin添加到my中来调用的pom.xml文件公司名称:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>python-build</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
       <executable>python</executable>
       <arguments>
            <argument>src/main/python/build/copyjwt.py</argument>
       </arguments>    
    </configuration>
</plugin>

现在为了将代码复制到我的zip文件中,我只需要再添加一个文件集到程序集.xml我在上面展示了:

<fileSet>
    <directory>target/jwt</directory>
    <outputDirectory>jwt</outputDirectory>
</fileSet>

相关问题 更多 >

    热门问题