AWS Amplify Python Rest API breaks the build process

0

I'm trying to create an AWS Amplify project and for some reason when I add a rest api and choose python the build process breaks. It works fine if I add a NodeJS api though. I tried to just use NodeJS but to be honest everything would be easier if I can use Python. To make sure that it wasn't my project I create one from scratch to test.

  1. Created a vanilla vite project
> npm create vite@latest
✔ Project name: … vite-project
✔ Select a framework: › React
✔ Select a variant: › JavaScript
> cd vite-project
> npm install
  1. added aws amplify and amplify UI
> npm install aws-amplify @aws-amplify/ui-react
  1. initialized amplify and added auth with default settings
> amplify init
? Enter a name for the project viteproject
? Initialize the project with the above configuration? Yes
? Select the authentication method you want to use: AWS profile
? Please choose the profile you want to use default

✔ Help improve Amplify CLI by sharing non-sensitive project configurations on failures (y/N) · no
Deployment state saved successfully.
✔ Initialized provider successfully.
✅ Initialized your environment successfully.
✅ Your project has been successfully initialized and connected to the cloud!

> amplify add auth
The current configured provider is Amazon Cognito. 
 
 Do you want to use the default authentication and security configuration? Default configuration
 Warning: you will not be able to edit these selections. 
 How do you want users to be able to sign in? Email
 Do you want to configure advanced settings? No, I am done.
✅ Successfully added auth resource viteproject74fd3ab5 locally

> amplify push
  1. Added amplify, and Authenticator imports to the app.jsx file
  2. wrapped the return in the Authenticator component
  3. Modified the index.html file to include the gobal crap so vite will work
<script>
var global = global || window;
var Buffer = Buffer || [];
var process = process || {
    env: { DEBUG: undefined },
    version: []
};
</script>
  1. Create a github repo pushed the current state.
  2. In the amplify console I went to the app and added the hosting to the backend project I just pushed and sync'd with Github repo.
  3. Modified the build settings base directory from '/' to 'dist' so vite will work

At this point I confirmed that the app builds properly, the frontend loads and I can create accounts and login.

Next up the API!

back to the CLI

  1. Added a python rest api, pushed to amplify(successfully)
> amplify add api

✔ Provide a friendly name for your resource to be used as a label for this category in the project: · PythonAPITest
? Select from one of the below mentioned services: REST
✔ Provide a path (e.g., /book/{isbn}): · /test
Only one option for [Choose a Lambda source]. Selecting [Create a new Lambda function].
? Provide an AWS Lambda function name: PythonAPITest
? Choose the runtime that you want to use: Python
? Do you want to configure advanced settings? No
? Do you want to edit the local lambda function now? No
✅ Successfully added resource PythonAPITest locally.
✅ Succesfully added the Lambda function locally
✔ Restrict API access? (Y/n) · yes
✔ Who should have access? · Authenticated users only
✔ What permissions do you want to grant to Authenticated users? · create, read, update, delete
✔ Do you want to add another path? (y/N) · no
✅ Successfully added resource PythonAPITest locally

> amplify push
  1. Sync'd Github Repo
  2. Build process now fails It fails on cloning repository. It claims failure is error connecting to repository but the logs say missing required dependencies.
2023-09-09T15:04:52.086Z [INFO]: Cloning into 'vite-project'...
2023-09-09T15:04:52.086Z [INFO]: # Switching to commit: 9cb3e715ec6435a49d46b29f006f86b79d8a25b7
2023-09-09T15:04:52.102Z [INFO]: Note: switching to '9cb3e715ec6435a49d46b29f006f86b79d8a25b7'.
                                 You are in 'detached HEAD' state. You can look around, make experimental
                                 changes and commit them, and you can discard any commits you make in this
                                 state without impacting any branches by switching back to a branch.
                                 If you want to create a new branch to retain commits you create, you may
                                 do so (now or later) by using -c with the switch command. Example:
                                 git switch -c <new-branch-name>
                                 Or undo this operation with:
                                 git switch -
                                 Turn off this advice by setting config variable advice.detachedHead to false
                                 HEAD is now at 9cb3e71 added api
2023-09-09T15:04:52.136Z [INFO]: Successfully cleaned up Git credentials
2023-09-09T15:04:52.136Z [INFO]: # Checking for Git submodules at: /codebuild/output/src295826851/src/vite-project/.gitmodules
2023-09-09T15:04:52.143Z [INFO]: # Retrieving environment cache...
2023-09-09T15:04:52.547Z [INFO]: # Retrieved environment cache
2023-09-09T15:04:52.548Z [INFO]: ---- Setting Up SSM Secrets ----
2023-09-09T15:04:52.548Z [INFO]: SSM params {"Path":"/amplify/d3bz27hej9k4tn/dev/","WithDecryption":true}
2023-09-09T15:05:09.798Z [INFO]: # Patching NPM package '@aws-amplify/cli' from 10.5.2 to 12.4.0...
0
023-09-09T15:04:50.761Z [INFO]: # Cloning repository: git@github.com:<>/vite-project.git
2023-09-09T15:04:52.084Z [INFO]: 
2023-09-09T15:04:52.086Z [INFO]: Cloning into 'vite-project'...
2023-09-09T15:04:52.086Z [INFO]: # Switching to commit: 9cb3e715ec6435a49d46b29f006f86b79d8a25b7
2023-09-09T15:04:52.102Z [INFO]: Note: switching to '9cb3e715ec6435a49d46b29f006f86b79d8a25b7'.
                                 You are in 'detached HEAD' state. You can look around, make experimental
                                 changes and commit them, and you can discard any commits you make in this
                                 state without impacting any branches by switching back to a branch.
                                 If you want to create a new branch to retain commits you create, you may
                                 do so (now or later) by using -c with the switch command. Example:
                                 git switch -c <new-branch-name>
                                 Or undo this operation with:
                                 git switch -
                                 Turn off this advice by setting config variable advice.detachedHead to false
                                 HEAD is now at 9cb3e71 added api
2023-09-09T15:04:52.136Z [INFO]: Successfully cleaned up Git credentials
2023-09-09T15:04:52.136Z [INFO]: # Checking for Git submodules at: /codebuild/output/src295826851/src/vite-project/.gitmodules
2023-09-09T15:04:52.143Z [INFO]: # Retrieving environment cache...
2023-09-09T15:04:52.547Z [INFO]: # Retrieved environment cache
2023-09-09T15:04:52.548Z [INFO]: ---- Setting Up SSM Secrets ----
2023-09-09T15:04:52.548Z [INFO]: SSM params {"Path":"/amplify/d3bz27hej9k4tn/dev/","WithDecryption":true}
2023-09-09T15:05:09.798Z [INFO]: # Patching NPM package '@aws-amplify/cli' from 10.5.2 to 12.4.0...
2023-09-09T15:05:26.098Z [INFO]: # Done patching NPM package '@aws-amplify/cli' to version 12.4.0
2023-09-09T15:05:26.240Z [INFO]: # Retrieving cache...
2023-09-09T15:05:34.901Z [INFO]: # Extracting cache...
your environment.
2023-09-09T15:05:52.019Z [INFO]: 🛑 Missing required dependencies to package PythonAPITest
                                 Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
2023-09-09T15:05:52.043Z [INFO]: Session Identifier: 8b09b8aa-da68-44c8-b34b-5d00d6d20093
2023-09-09T15:05:52.068Z [ERROR]: !!! Build failed
2023-09-09T15:05:52.068Z [ERROR]: !!! Non-Zero Exit Code detected
2023-09-09T15:05:52.068Z [INFO]: # Starting environment caching...
2023-09-09T15:05:52.069Z [INFO]: # Uploading environment cache artifact...
2023-09-09T15:05:52.150Z [INFO]: # Uploaded environment cache artifact
2023-09-09T15:05:52.150Z [INFO]: # Environment caching completed
Terminating logging...

It's gotta be me missing something right?

Hopefully this provides enough information. Please help!!!

2 Answers
1
Accepted Answer

So I was able to finally find the solution to this.

Looking at the provision logs I noticed that the image it was provisioning was including python 3.7, 3.8, and 3.9. So if I left the default when adding an API it was defaulting to the 3.7 error. I was changing it to push 3.10 instead which wasn't a built in option to the image. For now, I am just going to use the 3.9 until Amplify updates the image.

  • First I cleaned my own system
pip3 uninstall pipenv
rm -rdf /home/<user>/.pyenv
  • Then I reinstalled pipenv
pip3 install pipenv
  • Then reinstall pyenv
curl https://pyenv.run | bash
  • Then added the python rest api/function to the amplify project
amplify add api
  • Then edited the pipfile
[requires]
python_version = "3.9"
  • Then edited the cloudformation.json file
"Runtime": "python3.9",
  • Next I went to the Amplify console and edited the Amplify.yml file in the build settings
backend:
  phases:
    build:
      commands:
        - update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 15
        - /usr/local/bin/python3.9 -m pip install --upgrade pip
        - /usr/local/bin/pip3.9 install --user pipenv
        - amplifyPush --simple
  • I then pushed to Amplify
amplify push

Finally the entire build process worked and things are up and running with a Python based API added to the project. Hopefully this helps anyone else running into this error.

EDIT: Found this that helped resolve it https://github.com/aws-amplify/amplify-hosting/issues/595

answered 8 months ago
profile picture
EXPERT
reviewed 2 months ago
  • Its Not working in my case

    Executing command: update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 15 2024-04-24T06:58:41.387Z [WARNING]: failed to create /var/lib/alternatives/python3.new: Permission denied

-1

This is actually not an error. Its level is just Info. If you add requirements.txt to your code directory, I think this message should be eliminated.

2023-09-09T15:05:52.019Z [INFO]: 🛑 Missing required dependencies to package PythonAPITest
                                 Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
answered 9 months ago
  • So I assume you mean putting that in the amplify\backend\function\<projectname>\ directory I tried putting one there and it didn't seem to make a difference.

  • I encountered the following error when I tried to amplify to add the python api:

    🛑 You must have pipenv installed and available on your PATH as "pipenv". It can be installed by running "pip3 install --user pipenv".
    You must have virtualenv installed and available on your PATH as "venv". It can be installed by running "pip3 install venv".
    🛑 Missing required dependencies to package blog1802734b
    

    Then I fixed this error by executing command like below:

    pip3 install --user pipenv
    pip3 install --user virtualenv
    

    I don't know what your code looks like, maybe you can try updating amplify to the latest version and try again.

  • So I updated Amplify however it still breaks. I see this in the build logs

    2023-09-28T02:42:04.324Z [INFO]: 🛑 python3 found but version Python 3.7.15 is less than the minimum required version.
                                     You 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
                                     You must have pipenv installed and available on your PATH as "pipenv". It can be installed by running "pip3 install --user pipenv".
                                     You must have virtualenv installed and available on your PATH as "venv". It can be installed by running "pip3 install venv".
    2023-09-28T02:42:04.325Z [WARNING]: ✖ There was an error initializing your environment.
    2023-09-28T02:42:04.325Z [INFO]: 🛑 Missing required dependencies to package viteproject1b8d2931
    2023-09-28T02:42:04.325Z [INFO]: Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
    2023-09-28T02:42:04.345Z [INFO]: Session Identifier: 7c7879d5-7f21-47dd-be1e-f80bb819f749
    2023-09-28T02:42:04.367Z [ERROR]: !!! Build failed
    2023-09-28T02:42:04.368Z [ERROR]: !!! Non-Zero Exit Code detected
    2023-09-28T02:42:04.368Z [INFO]: # Starting environment caching...
    2023-09-28T02:42:04.368Z [INFO]: # Uploading environment cache artifact...
    

    However, I confirmed that on my laptop every thing is correct. I'm not sure how I can fix the build process for the AWS side.

  • you are almost done, Just do the following things:

    1. Install python after version 3.8

    2. Install "pipenv": pip3 install --user pipenv

    3. Install "venv": pip3 install --user virtualenv

  • for pipenv and virtualenv I get requirement already satisfied If I run python --version I get python command not found if I run python3 --version I get version: 3.10.12

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions