如何将 Python Lambda 函数集成到 AWS Amplify 的管道中

Posted

技术标签:

【中文标题】如何将 Python Lambda 函数集成到 AWS Amplify 的管道中【英文标题】:How do i integrate a Python Lambda Function into the Pipeline of AWS Amplify 【发布时间】:2021-05-20 10:33:54 【问题描述】:

所以我正在尝试使用 javascript 和 Python Lambda 函数构建一个 Ampliy 应用程序。一切正常。我已经设置了我的 CodeCommit 分支,以便通过持续部署进行托管。我在 Python 中添加了一个带有 Lambda 函数的 API。通过放大推送,放大成功部署了相应的 API 网关和 Lambda,并且我可以成功地与我的 lambda 函数进行交互。因此,一旦我将提交推送到我的存储库中,管道就会在构建阶段触发并崩溃:

                                 # Starting phase: build
                                 # Executing command: amplifyPush --simple
2021-02-17T14:01:23.680Z [INFO]: [0mAmplify AppID found: d2l0j3vtlykp8l. Amplify App name is: documentdownload[0m
2021-02-17T14:01:23.783Z [INFO]: [0mBackend environment dev found in Amplify Console app: documentdownload[0m
2021-02-17T14:01:24.440Z [WARNING]: - Fetching updates to backend environment: dev from the cloud.
2021-02-17T14:01:24.725Z [WARNING]: ✔ Successfully pulled backend environment dev from the cloud.
2021-02-17T14:01:24.758Z [INFO]: 
2021-02-17T14:01:26.925Z [INFO]: [33mNote: It is recommended to run this command from the root of your app directory[39m
2021-02-17T14:01:31.904Z [WARNING]: - Initializing your environment: dev
2021-02-17T14:01:32.216Z [WARNING]: ✔ Initialized provider successfully.
2021-02-17T14:01:32.829Z [INFO]: [31mpython3 found but version Python 3.7.9 is less than the minimum required version.[39m
                                 [31mYou must have python >= 3.8 installed and available on your PATH as "python3" or "python". It can be installed from https://www.python.org/downloads[39m
                                 [31mYou must have pipenv installed and available on your PATH as "pipenv". It can be installed by running "pip3 install --user pipenv".[39m
2021-02-17T14:01:32.830Z [WARNING]: ✖ An error occurred when pushing the resources to the cloud
2021-02-17T14:01:32.830Z [WARNING]: ✖ There was an error initializing your environment.
2021-02-17T14:01:32.832Z [INFO]: [31minit failed[39m
2021-02-17T14:01:32.834Z [INFO]: [0mError: Missing required dependencies to package documentdownload[0m
                                 [0m    at Object.buildFunction (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-category-function/src/provider-utils/awscloudformation/utils/buildFunction.ts:21:11)[0m
                                 [0m    at processTicksAndRejections (internal/process/task_queues.js:97:5)[0m
                                 [0m    at prepareResource (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-provider-awscloudformation/src/push-resources.ts:474:33)[0m
                                 [0m    at async Promise.all (index 0)[0m
                                 [0m    at Object.run (/root/.nvm/versions/node/v12.19.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-provider-awscloudformation/src/push-resources.ts:106:5)[0m
2021-02-17T14:01:32.856Z [ERROR]: !!! Build failed
2021-02-17T14:01:32.856Z [ERROR]: !!! Non-Zero Exit Code detected
2021-02-17T14:01:32.856Z [INFO]: # Starting environment caching...
2021-02-17T14:01:32.857Z [INFO]: # Environment caching completed

在上一步中,PROVISION 步骤虽然是 Python 3.8..

## Install python3.8
RUN wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
RUN tar xvf Python-3.8.0.tgz
WORKDIR Python-3.8.0
RUN ./configure --enable-optimizations --prefix=/usr/local
RUN make altinstall

现在我不知道它为什么会这样。将更改推送到本地它可以工作。有人可以帮忙吗?

【问题讨论】:

【参考方案1】:

here 的两个解决方案:

    交换构建映像 这可以通过this 完成。转到 Amplify 控制台,打开左侧菜单,单击“构建设置”,向下滚动直到看到“构建映像设置”,在下拉菜单中- 向下选择自定义,然后在其下方的字段中输入图像名称

2.如果您想从您提到的来源构建:将以下内容添加到 AWS 控制台中的应用程序设置 -> 构建设置下的amplify.yml

backend:
  phases:
    preBuild:
      commands:
        - export BASE_PATH=$(pwd)
        - yum install -y gcc openssl-devel bzip2-devel libffi-devel python3.8-pip
        - cd /opt && wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz
        - cd /opt && tar xzf Python-3.8.2.tgz 
        - cd /opt/Python-3.8.2 && ./configure --enable-optimizations
        - cd /opt/Python-3.8.2 && make altinstall
        - pip3.8 install --user pipenv
        - ln -fs /usr/local/bin/python3.8 /usr/bin/python3
        - ln -fs /usr/local/bin/pip3.8 /usr/bin/pip3
        - cd $BASE_PATH

【讨论】:

我想尝试一下,但似乎 apmlify 没有使用 amplify.yml 文件,也没有在 UI 中使用我的更新版本 :( 当我尝试时它响应“更新应用程序:文档下载成功”要重新部署,构建步骤保持不变,无需额外的命令 swapping 构建镜像怎么样? 我会试试这个,例如,我是否必须构建容器并将其上传到 ecr?或者构建映像的参考是什么意思? 好吧,不知何故放大与我不同的 git 分支混淆了。它终于提取了 amplify.yml 文件并且您的 preBuild 工作正常,谢谢!

以上是关于如何将 Python Lambda 函数集成到 AWS Amplify 的管道中的主要内容,如果未能解决你的问题,请参考以下文章

如何将 jQuery Validate 与我的 aws lambda 函数集成?

python lambda

如何通过 Kinesis 将数据从 Lambda (Python) 发送到 Redshift

API Gateway 方法与 lambda 函数集成,但 lambda 函数声称它没有触发器

Python lambda函数

python tkinter, 通过lambda表达式传递参数到按钮的点击事件函数