Search code examples
pythonamazon-web-servicesaws-lambdaaws-amplifyaws-amplify-cli

How do i integrate a Python Lambda Function into the Pipeline of AWS Amplify


So i'm trying to build an Ampliy application with Javascript and a Python Lambda function. Everything works just fine. I have setup my CodeCommit Branch for hosting with continous deployment. I add a API with a Lambda function in Python. With amplify push, amplify successfully deploys the corresponding API Gateway and Lambda and i can successfully interact with my lambda function. So, as soon as i push my commits into my repository, the pipeline gets trigger and crashes during the build phase:

                                 # 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

In the previous step PROVISION step is Python 3.8 though..

## 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

For now i have no idea why it behaves like this. Pushing the changes locally it works. Can anybody help?


Solution

  • Two Solutions from here:

    1. swapping the build image This can be done with this .Go to the Amplify Console, open the menu on the left, click on "Build Settings", scroll down until you see "Build Image Settings", on the drop-down select Custom, then enter the image name on the field just below it

    2.If you want to build from a source like you mentioned: add the following to amplify.yml in the AWS console under App settings -> Build settings:

    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