使用maven为aws lambda创建python部署包

Omk*_*kar 1 python deployment maven aws-lambda aws-step-functions

我目前正在致力于在 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 创建部署包?

快速帮助将受到高度赞赏。谢谢

moj*_*ken 5

让我把这个问题分成两部分。首先,对于 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>
Run Code Online (Sandbox Code Playgroud)

示例 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>
Run Code Online (Sandbox Code Playgroud)

如果您的 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")
Run Code Online (Sandbox Code Playgroud)

通过将Maven Exec 插件添加到我的 pom.xml 中来调用此 python 程序:

<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>
Run Code Online (Sandbox Code Playgroud)

现在,为了将代码复制到我的 zip 文件中,我只需向上面显示的 assembly.xml 添加一个文件集:

<fileSet>
    <directory>target/jwt</directory>
    <outputDirectory>jwt</outputDirectory>
</fileSet>
Run Code Online (Sandbox Code Playgroud)