aws-amplify / aws-amplify/amplify-hosting

Lambda Backend - does not use python env as defined by Pipenv

Open
#3,030 6 comments 0 reactions 0 assignees View on GitHub
backend-builds question
Dominant language
Dockerfile
Stars
481
Forks
123
PR merge metrics
No merged PRs in 30d

Description

### Before opening, please confirm:

- [X] I have checked to see if my question is addressed in the [FAQ](https://github.com/aws-amplify/amplify-hosting/blob/master/FAQ.md).
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-hosting/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-hosting/blob/master/CONTRIBUTING.md).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

### App Id

d2jhjzrasxv9wi

### Region

eu-central-1

### Amplify Hosting feature

Backend builds

### Describe the bug

I'm developing a react app with a Lambda (Python) Backend. I followed the guide to set up such a lambda function, and to do so I used `pipenv`.
At first, I had problems in Amplify's CI/CD when building the backend because it could not find Python3.8 in the build image. I fixed that by following the solution described here: https://github.com/aws-amplify/amplify-hosting/issues/595#issuecomment-1058057237

After that succeeded, and I saw logs showing that all my dependencies were successfully installed, I noticed that my Lambda function fails because it cannot find the required dependencies.
I added a line in `index.py` to show which dependencies exist and I saw that none of the dependencies configured in the `Pipfile` file, were available.

So it seems like the Python environment that is running in Lambda, is not the same that was configured during the CI/CD process.

### Expected behavior

Python environment having the same packages that were installed during the CI/CD pipeline, based on the `Pipfile`

### Reproduction steps

1. `cd` into the lambda function's directory
2. run `pipenv install flask`
3. import `flask` in `index.py`
4. Use the same build settings that are attached
5. Invoke the lambda function

### Build Settings

```yaml
version: 1
backend:
phases:
build:
commands:
- ln -fs /usr/local/bin/pip3.8 /usr/bin/pip3
- ln -fs /usr/local/bin/python3.8 /usr/bin/python3
- export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain --domain-owner --query authorizationToken --output text)
- pip3 install --user pipenv
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- export CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain --domain-owner --query authorizationToken --output text)
- npm install
build:
commands:
- npm run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
```

### Additional information

One of my dependencies are in a private CodeArtifact repository, and that's why I have `CODEARTIFACT_AUTH_TOKEN` configured.
In my `Pipfile` I have also changed the source `url` to be my artifact repository.

Contributor guide

Open the contributing guide

Research direction

Start with the Lambda function's Pipfile and index.py, then inspect the attached backend build settings and the amplifyPush entry point while reproducing the build. Invoke the deployed function and verify that packages declared in Pipfile, including flask, can be imported; completion means the Lambda runtime uses the dependencies installed during CI/CD.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, python, react
Domain
backend, ci-cd, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.