How to access an AWS Lambda environment variable from Python

后端 未结 3 1422
时光说笑
时光说笑 2020-12-29 00:56

Using the new environment variable support in AWS Lambda, I\'ve added an env var via the webui for my function.

How do I access this from Python? I tried:

         


        
相关标签:
3条回答
  • 2020-12-29 01:26

    AWS Lambda environment variables can be defined using the AWS Console, CLI, or SDKs. This is how you would define an AWS Lambda that uses an LD_LIBRARY_PATH environment variable using AWS CLI:

    aws lambda create-function \
      --region us-east-1
      --function-name myTestFunction
      --zip-file fileb://path/package.zip
      --role role-arn
      --environment Variables={LD_LIBRARY_PATH=/usr/bin/test/lib64}
      --handler index.handler
      --runtime nodejs4.3
      --profile default
    

    Once created, environment variables can be read using the support your language provides for accessing the environment, e.g. using process.env for Node.js. When using Python, you would need to import the os library, like in the following example:

    ...
    import os
    ...
    print("environment variable: " + os.environ['variable'])
    

    Resource Link:

    AWS Lambda Now Supports Environment Variables



    Assuming you have created the .env file along-side your settings module.

    .
    ├── .env
    └── settings.py
    

    Add the following code to your settings.py

    # settings.py
    from os.path import join, dirname
    from dotenv import load_dotenv
    
    dotenv_path = join(dirname(__file__), '.env')
    load_dotenv(dotenv_path)
    

    Alternatively, you can use find_dotenv() method that will try to find a .env file by (a) guessing where to start using file or the working directory -- allowing this to work in non-file contexts such as IPython notebooks and the REPL, and then (b) walking up the directory tree looking for the specified file -- called .env by default.

    from dotenv import load_dotenv, find_dotenv
    load_dotenv(find_dotenv())
    

    Now, you can access the variables either from system environment variable or loaded from .env file.

    Resource Link:

    https://github.com/theskumar/python-dotenv



    gepoggio answered in this post: https://github.com/serverless/serverless/issues/577#issuecomment-192781002

    A workaround is to use python-dotenv: https://github.com/theskumar/python-dotenv

    import os
    import dotenv
    
    dotenv.load_dotenv(os.path.join(here, "../.env"))
    dotenv.load_dotenv(os.path.join(here, "../../.env"))
    

    It tries to load it twice because when ran locally it's in project/.env and when running un Lambda the .env is located in project/component/.env

    0 讨论(0)
  • 2020-12-29 01:45

    Both

    import os
    os.getenv('MY_ENV_VAR')
    

    And

    os.environ['MY_ENV_VAR']
    

    are feasible solutions, just make sure in the lambda GUI that the ENV variables are actually there.

    0 讨论(0)
  • 2020-12-29 01:48

    I used this code; it includes both cases, setting the variable from the handler and setting it from outside the handler.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """Trying new lambda stuff"""
    import os
    import configparser
    
    class BqEnv(object):
        """Env and self variables settings"""
        def __init__(self, service_account, configfile=None):
            config = self.parseconfig(configfile)
            self.env = config
            self.service_account = service_account
    
        @staticmethod
        def parseconfig(configfile):
            """Connection and conf parser"""
            config = configparser.ConfigParser()
            config.read(configfile)
            env = config.get('BigQuery', 'env')
            return env
    
        def variable_tests(self):
            """Trying conf as a lambda variable"""
            my_env_var = os.environ['MY_ENV_VAR']
            print my_env_var
            print self.env
            return True
    
    def lambda_handler(event, context):
        """Trying env variables."""
        print event
        configfile = os.environ['CONFIG_FILE']
        print configfile
        print type(str(configfile))
        bqm = BqEnv('some-json.json', configfile)
        bqm.variable_tests()
        return True
    

    I tried this with a demo config file that has this:

    [BigQuery]
    env = prod
    

    And the setting on lambda was the following:

    Hope this can help!

    0 讨论(0)
提交回复
热议问题