如何从Python访问AWS Lambda环境变量 [英] How to access an AWS Lambda environment variable from Python

查看:295
本文介绍了如何从Python访问AWS Lambda环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用新的 AWS Lambda中的环境变量支持,我已经通过webui为我的功能添加了一个env变量.

如何从Python访问此文件?我试过了:

import os

MY_ENV_VAR = os.environ['MY_ENV_VAR']

但是我的函数停止工作(如果我对MY_ENV_VAR的相关值进行硬编码,则可以正常工作).

解决方案

可以使用AWS Console,CLI或SDK定义AWS Lambda环境变量.这是您通过AWS CLI定义使用LD_LIBRARY_PATH环境变量的AWS Lambda的方式:

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

创建后,可以使用您的语言提供的访问环境的支持来读取环境变量,例如对Node.js使用process.env.使用Python时,您需要导入os库,如以下示例所示:

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

资源链接:

AWS Lambda现在支持环境变量



假设您在设置模块旁边创建了.env文件.

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

将以下代码添加到您的设置中.py

# settings.py
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)

或者,您可以使用find_dotenv()方法,该方法将尝试通过以下方式查找.env文件:(a)猜测从何处开始使用 file 或工作目录-允许该文件在非文件上下文(例如IPython笔记本和REPL),然后(b)沿目录树查找指定文件-默认情况下称为.env.

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

现在,您可以从系统环境变量或从.env文件中加载变量.

资源链接:

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



gepoggio在此帖子中回答: https://github.com/serverless/serverless/issues/577#issuecomment-192781002

一种解决方法是使用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"))

它尝试加载两次,因为在本地运行时 project/.env,在运行Lambda时,.env位于 项目/组件/.env

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:

import os

MY_ENV_VAR = os.environ['MY_ENV_VAR']

but my function stopped working (if I hard code the relevant value for MY_ENV_VAR it works fine).

解决方案

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

这篇关于如何从Python访问AWS Lambda环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆