使用 maven 为 aws lambda 创建 python 部署包
Using maven to create python deployment package for aws lambda
我目前正致力于在 aws lambda 上部署 python 包。使用 virtualenv 和 zip 工具,我可以轻松创建一个 lambda zip 文件以上传到 aws 并 运行 它。我使用以下博客创建包:https://joarleymoraes.com/hassle-free-python-lambda-deployment/.
但是,我需要使用构建工具将我的代码与我公司的 Jenkins 集成。我们使用 maven 进行构建,我需要遵循相同的方法。
我查看了 maven-exec-plugin 以了解如何按照博客中的步骤使用虚拟环境并创建 zip 文件。但是,我无法按照 maven 教程中提到的步骤进行操作。
有没有人遇到过同样的问题?如果是这样,你是如何解决的?比如,您如何配置 pom.xml 来为 python aws lambdas 创建部署包?
快速帮助将不胜感激。谢谢
让我把这个问题分成两部分。首先,对于 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>
样本assembly.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>lambda-zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/python</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
如果您的 lambda 仅使用标准 python 库,那么这就是您所需要的。当您需要 boto3 或 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 添加到我的 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 文件中,我只需要再添加一个文件集到上面显示的 assembly.xml:
<fileSet>
<directory>target/jwt</directory>
<outputDirectory>jwt</outputDirectory>
</fileSet>
我目前正致力于在 aws lambda 上部署 python 包。使用 virtualenv 和 zip 工具,我可以轻松创建一个 lambda zip 文件以上传到 aws 并 运行 它。我使用以下博客创建包:https://joarleymoraes.com/hassle-free-python-lambda-deployment/.
但是,我需要使用构建工具将我的代码与我公司的 Jenkins 集成。我们使用 maven 进行构建,我需要遵循相同的方法。
我查看了 maven-exec-plugin 以了解如何按照博客中的步骤使用虚拟环境并创建 zip 文件。但是,我无法按照 maven 教程中提到的步骤进行操作。
有没有人遇到过同样的问题?如果是这样,你是如何解决的?比如,您如何配置 pom.xml 来为 python aws lambdas 创建部署包?
快速帮助将不胜感激。谢谢
让我把这个问题分成两部分。首先,对于 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>
样本assembly.xml:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>lambda-zip</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>src/main/python</directory>
<outputDirectory></outputDirectory>
</fileSet>
</fileSets>
</assembly>
如果您的 lambda 仅使用标准 python 库,那么这就是您所需要的。当您需要 boto3 或 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 添加到我的 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 文件中,我只需要再添加一个文件集到上面显示的 assembly.xml:
<fileSet>
<directory>target/jwt</directory>
<outputDirectory>jwt</outputDirectory>
</fileSet>