3

I'm using the useDotenv property on my serverless.yml but it is not loading in AWS:

org: xyz
app: route-learner
service: route-learner-cron
frameworkVersion: '3'

useDotenv: true

provider:
  name: aws
  runtime: python3.8
  stage: ${opt:stage, 'dev'}

functions:
  extract_route:
    handler: src.functions.extract_route.run
    events:
      # Invoke Lambda function at every 5th minute.
      - schedule:
          rate: cron(5,15,25,35,45,55 * * * ? *)
    vpc:
      securityGroupIds:
        - ...
      subnetIds:
        - ...

plugins:
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: true

I have in the root directory a .env.prod file with the following:

DB_URI=my_db_url
DB_VERBOSE=True

I'm deploying the function with the command:

serverless deploy --stage prod --verbose

Once the function is called, I see the following error:

[ERROR] KeyError: 'DB_URI'
Traceback (most recent call last):
  File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler
    return user_handler(event, context)
  File "/var/task/s_extract_route.py", line 25, in error_handler
    raise e
  File "/var/task/s_extract_route.py", line 20, in <module>
    user_handler = serverless_sdk.get_user_handler('src.functions.extract_route.run')
  File "/var/task/serverless_sdk/__init__.py", line 56, in get_user_handler
    user_module = import_module(user_module_name)
  File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/task/src/functions/__init__.py", line 8, in <module>
    from src.db import engine
  File "/var/task/src/db.py", line 4, in <module>
    DATABASE_URI = os.environ['DB_URI']
  File "/var/lang/lib/python3.8/os.py", line 675, in __getitem__
    raise KeyError(key) from None
[ERROR] KeyError: 'DB_URI' Traceback (most recent call last):   File "/var/task/serverless_sdk/__init__.py", line 144, in wrapped_handler     return user_handler(event, context)   File "/var/task/s_extract_route.py", line 25, in error_handler     raise e   File "/var/task/s_extract_route.py", line 20, in <module>     user_handler = serverless_sdk.get_user_handler('src.functions.extract_route.run')   File "/var/task/serverless_sdk/__init__.py", line 56, in get_user_handler     user_module = import_module(user_module_name)   File "/var/lang/lib/python3.8/importlib/__init__.py", line 127, in import_module     return _bootstrap._gcd_import(name[level:], package, level)   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import   File "<frozen importlib._bootstrap>", line 991, in _find_and_load   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import   File "<frozen importlib._bootstrap>", line 991, in _find_and_load   File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked   File "<frozen importlib._bootstrap>", line 671, in _load_unlocked   File "<frozen importlib._bootstrap_external>", line 843, in exec_module   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed   File "/var/task/src/functions/__init__.py", line 8, in <module>     from src.db import engine   File "/var/task/src/db.py", line 4, in <module>     DATABASE_URI = os.environ['DB_URI']   File "/var/lang/lib/python3.8/os.py", line 675, in __getitem__     raise KeyError(key) from None

What is missing in my serverless configuration?

1 Answer 1

18

I have found the issue, the useDotenvattribute simply includes the content of .env file to the serverless.yml file. So, I still need to declare the environment variable inside the template:

environment:
    DB_URI: ${env:DB_URI}
    DB_VERBOSE: ${env:DB_VERBOSE}

The .env file is not included in the final package deployed to AWS.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so so much for updating your question with this. I was stuck for like two hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.