LoginSignup
19
5

More than 3 years have passed since last update.

AWS AmplifyでPythonのFunctionをCI/CDするとbuildに失敗する問題の対処方法

Posted at

題名の通り、AWS AmplifyでPythonのFunctionをCI/CDするとbuildに失敗する問題に遭遇しました。

結論(対処方法)

ビルド設定のamplify.ymlに以下を追記することでbuildを通すことができました。

version: 1
backend:
  phases:
    build:
      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
        # <<< ここまで追記
        - '# Execute Amplify CLI with the helper script'
        - amplifyPush --simple

発生した問題

CI/CDのビルドフェーズにて、下記のようにPython3.8以上が必要な旨が出力されています。

2020-10-27T19:21:49.678Z [INFO]: python3 found but version Python 3.7.4 is less than the minimum required version.
                                 You must have python >= 3.8 installed and available on your PATH as "python3". It can be installed from https://www.python.org/downloads
                                 You must have pipenv installed and available on your PATH as "pipenv". It can be installed by running "pip3 install --user pipenv".
2020-10-27T19:21:49.679Z [WARNING]: ✖ An error occurred when pushing the resources to the cloud
                                    ✖ There was an error initializing your environment.
2020-10-27T19:21:49.687Z [INFO]: init failed
2020-10-27T19:21:49.688Z [INFO]: Error: Missing required dependencies to package tatamifmmusiclibrary7312699f
                                 at buildResource (/root/.nvm/versions/node/v10.16.0/lib/node_modules/@aws-amplify/cli/node_modules/amplify-provider-awscloudformation/src/build-resources.js:30:11)
                                 at process._tickCallback (internal/process/next_tick.js:68:7)
2020-10-27T19:21:49.699Z [ERROR]: !!! Build failed
2020-10-27T19:21:49.699Z [ERROR]: !!! Non-Zero Exit Code detected
2020-10-27T19:21:49.699Z [INFO]: # Starting environment caching...
2020-10-27T19:21:49.700Z [INFO]: # Environment caching completed

原因

Amplify CLIで amplify add function してランタイムにPythonを選択すると、内部的にはPython 3.8が選択されるようです。
一方、CI/CDで使用するAmazon Linux2でインストールされるPythonは3.7.4のため、バージョンの不一致が起こってしまいます。

解決のヒント

経験上、Amplifyの問題はGitHub Issueにあがっているケースが多いです。
本件も以下Issueにて既に議論がされていました。

Amplify can't find Python3.8 on build phase of CI/CD #595

カスタムイメージを作成した人もいるようですが、あまり非公式なイメージは使いたくないので、多少buildに時間がかかってもPython 3.8をインストールしたほうが良いと判断しました。
いずれAmazon Linux2でインストールされるPythonのバージョンが上がると思うので、暫定対処で良いでしょう。

19
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
5