aws-amplify / aws-amplify/amplify-cli

Prompt for function environment variables on `amplify mock`

Open
#10,642 6 comments 0 reactions 0 assignees View on GitHub
feature-request functions ops-multienv p3
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-cli/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-cli/blob/master/CONTRIBUTING.md#bug-reports).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [X] I have removed any sensitive information from my code snippets and submission.

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

v18.4.0

### Amplify CLI Version

9.0.0

### What operating system are you using?

Ubuntu 20

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

### Amplify Categories

function

### Amplify Commands

pull, update

### Describe the bug

Context: there is a team of developers, they want to work on the same Amplify project. If one developer adds a function with environment variables and pushes changes and when the teammates do `amplify pull` environment variables values are not pulled into the project. So, the person who did `amplify pull` has to update function and provide environment variables manually.

### Expected behavior

1. `amplify pull` in a new folder/PC should pull environment variables values into `amplify/team-provider-info.json`
2. After step 1 `amplify mock function` should run without any problems, all environment variables should be as expected.

### Reproduction steps

1. Create Amplify project/app
2. Add a Python3 function (`amplify add function`), i.e `testFunc`. With following code:
```
import os
import logging

LOGGING_LEVEL = os.environ.get("LOGGING_LEVEL")
logger = logging.getLogger(__name__)
log_level_info = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR
}
logger.setLevel(log_level_info[LOGGING_LEVEL])
```

3. Update function to add environment variable (`amplify update function`), LOGGING_LEVEL=INFO
4. Check `amplify/team-provider-info.json` (should contain "loggingLevel": "INFO") and `function-parameters.json` (should contain "environmentVariableName": "LOGGING_LEVEL").
5. Do `amplify push`.
6. Pretend you are your teammate (or ask a real one). Create a new folder and do `amplify pull` in it.
7. Check `amplify/team-provider-info.json` (there is NO "loggingLevel": "INFO") and `function-parameters.json` (there IS "environmentVariableName": "LOGGING_LEVEL"). Do `amplify mock function testFunc` (see log output).

### GraphQL schema(s)

```graphql
# Put schemas below this line

```

### Log output

```
# Put your logs below this line

$amplify mock function
? Select the function to mock testFunc
? Provide the path to the event JSON object relative to {abs_path_to_func} src/event.json
Could not find ref for "loggingLevel". Using unsubstituted value.
Ensuring latest function changes are built...
Creating a virtualenv for this project...
Pipfile: {abs_path_to_func}/Pipfile
Using /usr/bin/python3.8 (3.8.10) to create virtualenv...
β ¦ Creating virtual environment...created virtual environment CPython3.8.10.final.0-64 in 375ms
creator CPython3Posix(dest=/home/tt/.local/share/virtualenvs/addFeedItem-AwbGh0lK, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/tt/.local/share/virtualenv)
added seed packages: pip==22.1.2, setuptools==62.3.4, wheel==0.37.1
activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

βœ” Successfully created virtual environment!
Virtualenv location: ...
Installing dependencies from Pipfile.lock (a3ee6e)...
🐍 β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰ 9/9 β€” 00:00:06
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Starting execution...
Traceback (most recent call last):
File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 44, in
main(sys.argv[1:])
File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 22, in main
spec.loader.exec_module(handlerModule)
File "", line 848, in exec_module
File "", line 219, in _call_with_frames_removed
File "{abs_path_to_func}/src/index.py", line 27, in
logger.setLevel(log_level_info[LOGGING_LEVEL])
KeyError: 'loggingLevel'
testFunc failed with the following error:
Error: Command failed with exit code 1: pipenv run python3 β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py {abs_path_to_func}/src/index.py handler
Traceback (most recent call last):
File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 44, in
main(sys.argv[1:])
File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 22, in main
spec.loader.exec_module(handlerModule)
File "", line 848, in exec_module
File "", line 219, in _call_with_frames_removed
File "{abs_path_to_func}/src/index.py", line 27, in
logger.setLevel(log_level_info[LOGGING_LEVEL])
KeyError: 'loggingLevel'
at makeError (/snapshot/repo/build/node_modules/execa/lib/error.js:60:11)
at handlePromise (/snapshot/repo/build/node_modules/execa/index.js:118:26)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async pythonInvoke (/snapshot/repo/build/node_modules/amplify-python-function-runtime-provider/lib/util/invokeUtil.js:30:24)
at async timeConstrainedInvoker (/snapshot/repo/build/node_modules/amplify-util-mock/lib/func/index.js:134:12)
at async start (/snapshot/repo/build/node_modules/amplify-util-mock/lib/func/index.js:111:20)
at async Object.run (/snapshot/repo/build/node_modules/amplify-util-mock/lib/commands/mock/function.js:21:5)
at async Object.executeAmplifyCommand (/snapshot/repo/build/node_modules/amplify-util-mock/lib/amplify-plugin-index.js:47:3)
at async executePluginModuleCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:142:5)
at async executeCommand (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/execution-manager.js:40:9)
at async Object.run (/snapshot/repo/build/node_modules/@aws-amplify/cli-internal/lib/index.js:165:13) {
shortMessage: 'Command failed with exit code 1: pipenv run python3 β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py {abs_path_to_func}/src/index.py handler',
command: 'pipenv run python3 β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py {abs_path_to_func}/src/index.py handler',
escapedCommand: 'pipenv run python3 "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py" "{abs_path_to_func}/src/index.py" handler',
exitCode: 1,
signal: undefined,
signalDescription: undefined,
stdout: '',
stderr: 'Traceback (most recent call last):\n' +
' File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 44, in \n' +
' main(sys.argv[1:])\n' +
' File "β–‰β–‰β–‰β–‰/.amplify/lib/amplify-python-function-runtime-provider/shim/shim.py", line 22, in main\n' +
' spec.loader.exec_module(handlerModule)\n' +
' File "", line 848, in exec_module\n' +
' File "", line 219, in _call_with_frames_removed\n' +
' File "{abs_path_to_func}/src/index.py", line 27, in \n' +
' logger.setLevel(log_level_info[LOGGING_LEVEL])\n' +
"KeyError: 'loggingLevel'",
failed: true,
timedOut: false,
isCanceled: false,
killed: false
}
...
Finished execution./
```

### Additional information

os: ubuntu 20
python: python3.8

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue using amplify pull and amplify mock function, then inspect amplify/team-provider-info.json, function-parameters.json, and the Python function's src/index.py. Trace how the loggingLevel value moves from the pulled project configuration into the mock runtime. Done means the pulled environment variable value is preserved and amplify mock function runs with the expected value.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, typescript
Domain
backend, cli
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.