为令人难以置信的aws lambdas提供有用的实用工具
pyfaaster的Python项目详细描述
皮法斯特
作为服务的python函数的有用实用程序(从aws lambda开始)。
问题
作为一种服务的功能可以是快乐的。当与像python这样简洁的语言搭配时,您将开始 重新考虑像rails、django等“web框架”的需求:将您的函数与 faas提供程序的api网关,您可以用最少的代码运行。当然,在注入环境、格式化响应、检查参数等方面仍有一些样板代码。
概念
pyfaaster的目标是
穿过拐杖,让你和你的Python爱好者一起哼唱。pyfaaster
完成
这个目标是为lambda函数提供有用的中间件(即decorators)。此外,pyfaaster
可用于其优秀的Makefile
和serverless.yml
示例。
干杯!
用法
以下是有用中间件的非详尽列表和详细信息。更多信息可在pyfaaster.aws.handler_decorators
中找到。
环境变量
你不想把os.environ
洒在你的代码中。让我们来帮你吧。
import pyfaaster as faas
@faas.environ_aware(['REQUIRED_ENV'], ['OPTIONAL_ENV'])
def handler(event, context, REQUIRED_ENV=None, OPTIONAL_ENV=None):
assert REQUIRED_ENV == os.environ['REQUIRED_ENV'] # <- faas.environ_aware will return early with a useful message if this is not set
assert OPTIONAL_ENV == os.environ.get('OPTIONAL_ENV')
配置文件
同样,不要担心注入那些s3配置文件。
import pyfaaster as faas
@faas.configuration_aware('config.json', True) # S3 key to a config file, create if not there
def handler(event, context, configuration=None):
assert configuration == < {
'load': lambda : {contents of config bucket},
'save': lambda d : save d into config bucket,
}
响应格式
唉,您必须手动将lambda返回值转换为api网关的预期字典吗?不要忘记将所有json序列化到body
元素中!哦,等等……只需使用
import pyfaaster as faas
@faas.http_response(default_error_message='Failed to handle something.")
def handler(event, context, **kwargs):
return { 'my': 'important', 'json': 'data'} # <- will end up as the json serialized `body` in an API Gateway compatible dict with statusCode.
授权
你得确认一下你的令牌范围,朋友!
import pyfaaster as faas
# Checks the event.requestContext.authorizer for the given scopes. This works nicely with AWS custom
# authorizers. An example one is coming to this library soon.
@faas.scopes('read:profile', 'update:email')
def handler(event, context, **kwargs):
return 'Hello, Secure World!'