AWS Amplify REST API with Lambda (python) backend. Build fails with: You must have pipenv installed and available on your PATH as "pipenv". It can be installed by running "pip3 install --user pipenv".

0
  • IDE: AWS Cloud9
  • Python version: 3.9
  • Build runs successfully with fronted react only.

Getting error after adding aws amplify rest api using AWS Lambda with python backend. I'm getting following error regarding pipenv and virtualenv while building app on aws codecommit. Attaching complete build logs downloaded from aws codecommit.

Build logs:

2024-04-23T17:14:23.552Z [INFO]: # Cloning repository: .codecommit......./v1/repos/my_repo
2024-04-23T17:14:50.967Z [INFO]: 
2024-04-23T17:14:51.063Z [INFO]: Cloning into 'my_repo'...
                                 remote: 
remote: Counting objects: 0        
remote: Counting objects: 5459        
remote: Counting objects: 5459, done.
                                 Receiving objects:   0% (1/5459)
Receiving objects:   1% (55/5459)
Receiving objects:   2% (110/5459)
..............
Receiving objects:  80% (4368/5459)
Receiving objects:  81% (4422/5459)
Receiving objects:  82% (4477/5459), 14.01 MiB | 26.27 MiB/s
..............
Receiving objects: 100% (5459/5459), 16.60 MiB | 10.63 MiB/s, done.
                                 Resolving deltas:   0% (0/1108)
Resolving deltas:   1% (12/1108)
..............
Resolving deltas: 100% (1108/1108), done.
2024-04-23T17:14:51.063Z [INFO]: # Switching to commit: c5147ffc66c871266883d26187dd61775fd330
2024-04-23T17:14:51.075Z [INFO]: Note: switching to 'c5147ffc66c871266883d26187dd61775fd330'.
                                 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 c523147 installed virtual env
2024-04-23T17:14:51.120Z [INFO]: Successfully cleaned up Git credentials
2024-04-23T17:14:51.120Z [INFO]: # Checking for Git submodules at: /codebuild/output/src44149293/src/my_repo/.gitmodules
2024-04-23T17:14:51.127Z [INFO]: # Retrieving environment cache...
2024-04-23T17:14:51.176Z [INFO]: # Retrieved environment cache
2024-04-23T17:14:51.176Z [INFO]: ---- Setting Up SSM Secrets ----
2024-04-23T17:14:51.176Z [INFO]: SSM params {"Path":"/amplify/d88arty90ck4/dev/","WithDecryption":true}
2024-04-23T17:14:54.706Z [INFO]: # Patching NPM package '@aws-amplify/cli' from 12.8.2 to 12.11.1...
2024-04-23T17:15:20.585Z [INFO]: # Installed '@aws-amplify/cli@12.11.1'
2024-04-23T17:15:20.676Z [INFO]: # Done patching NPM package '@aws-amplify/cli' to version 12.11.1
2024-04-23T17:15:20.683Z [INFO]: # Retrieving cache...
2024-04-23T17:15:22.227Z [INFO]: # Extracting cache...
2024-04-23T17:15:22.767Z [INFO]: # Extraction completed
2024-04-23T17:15:22.922Z [INFO]: # Retrieved cache
2024-04-23T17:15:27.081Z [INFO]: ## Starting Backend Build
                                 # Starting phase: build
2024-04-23T17:15:29.286Z [INFO]: Amplify AppID found: d88arty90ck4. Amplify App name is: my_repo
2024-04-23T17:15:29.357Z [INFO]: Backend environment dev found in Amplify Console app: my_repo
2024-04-23T17:15:30.676Z [WARNING]: - Fetching updates to backend environment: dev from the cloud.
2024-04-23T17:15:31.148Z [WARNING]: - Building resource api/my_reporest
2024-04-23T17:15:31.169Z [WARNING]: - Building resource auth/my_repo
2024-04-23T17:15:31.256Z [WARNING]: ✔ Successfully pulled backend environment dev from the cloud.
2024-04-23T17:15:31.318Z [INFO]: ✅
2024-04-23T17:15:33.328Z [INFO]: Note: It is recommended to run this command from the root of your app directory
2024-04-23T17:15:34.576Z [WARNING]: - Initializing your environment: dev
2024-04-23T17:15:35.022Z [WARNING]: - Building resource api/my_reporest
2024-04-23T17:15:35.046Z [WARNING]: - Building resource auth/my_repo
2024-04-23T17:15:35.116Z [WARNING]: ✔ Initialized provider successfully.
2024-04-23T17:15:35.649Z [INFO]: No AppSync API configured. Please add an API
2024-04-23T17:15:35.912Z [INFO]: 🛑 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".
2024-04-23T17:15:35.912Z [WARNING]: ✖ There was an error initializing your environment.
2024-04-23T17:15:35.912Z [INFO]: 🛑 Missing required dependencies to package myUserHandler
2024-04-23T17:15:35.913Z [INFO]: Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/
2024-04-23T17:15:35.933Z [INFO]: Session Identifier: feaffeca-9a65-8956-8896-97ffc26dbf63
2024-04-23T17:15:35.933Z [WARNING]: - Creating Zip
2024-04-23T17:15:35.950Z [INFO]: ✅ Report saved: /tmp/my_repo/report-3500892535938.zip
2024-04-23T17:15:35.950Z [WARNING]: - Sending zip
2024-04-23T17:15:37.140Z [WARNING]: ✔ Done
2024-04-23T17:15:37.141Z [INFO]: Project Identifier: 120826ae24d6d78e0826065498d0f276d
2024-04-23T17:15:37.167Z [ERROR]: !!! Build failed
2024-04-23T17:15:37.167Z [ERROR]: !!! Error: Command failed with exit code 1
2024-04-23T17:15:37.167Z [INFO]: # Starting environment caching...
2024-04-23T17:15:37.168Z [INFO]: # Uploading environment cache artifact...
2024-04-23T17:15:37.238Z [INFO]: # Uploaded environment cache artifact
2024-04-23T17:15:37.238Z [INFO]: # Environment caching completed
Terminating logging...

File: amplify.yml

version: 1
backend:
  phases:
    build:
      commands:
        - amplifyPush --simple
frontend:
  phases:
    preBuild:
      commands:
        - npm ci --cache .npm --prefer-offline
    build:
      commands:
        - npm run build
  artifacts:
    baseDirectory: dist
    files:
      - '**/*'
  cache:
    paths:
      - .npm/**/*

  • Having the same issue

1 Answer
0

Hello,

This issue usually occurs if the path defined within amplify.yml isn't able to identify the appropriate python version, pipenv. In order to mitigate the issue, make sure that your system has pipenv installed by running "pip3 install pipenv" command. Further, after adding the python rest api/function to the amplify project using "amplify add api" command. Then, edit the pipfile and make sure you have the python_version set to value "3.9". As next step, edit the cloudformation.json file and make sure the "Runtime": "python3.9" is configured appropriately. Lastly, on amplify console, edit the Amplify.yml file in the build settings and have the below configurations :

--------------------------------------------
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 have also found similar issue over this documentation [1] and kindly refer the steps mentioned over the docs if that helps in mitigating the issue.

Please do let us know if that resolves the issue. If you still encounter the issue, then kindly share the amplify.yml file with us as it will help in identifying the issue.

Thank you.

References:

[1] https://repost.aws/questions/QUof8ty0r_ROenfPjqfKlkJg/aws-amplify-python-rest-api-breaks-the-build-process

AWS
answered 5 months ago
  • I have already updated python version at respective files.

    /my_repo/amplify/backend/function/myUserHandler/Pipfile:
    	12: python_version = "3.9"
    
    /my_repo/amplify/backend/function/myUserHandler/Pipfile.lock:
    	8:             "python_version": "3.9"
    
    /my_repo/amplify/backend/function/myUserHandler/myUserHandler-cloudformation-template.json:
    	81:         "Runtime": "python3.9",
    

    Added initial build file to question. Build logs after updating build file as mentioned above.

    2024-04-26T09:49:53.033Z [INFO]: ## Starting Backend Build
                                     # Starting phase: build
                                     # Executing command: update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.9 15
    2024-04-26T09:49:53.204Z [WARNING]: failed to create /var/lib/alternatives/python3.new: Permission denied
    2024-04-26T09:49:53.205Z [ERROR]: !!! Build failed
    2024-04-26T09:49:53.205Z [ERROR]: !!! Error: Command failed with exit code 2
    

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