aws无服务器python系统的配置客户端

serverless-config的Python项目详细描述


https://img.shields.io/pypi/status/serverless-config.svghttps://travis-ci.org/oharaandrew314/serverless-config.svg?branch=masterhttps://img.shields.io/pypi/v/serverless-config.svghttps://img.shields.io/pypi/l/serverless-config.svghttps://img.shields.io/pypi/pyversions/serverless-config.svgcodecov.io

一个用于aws无服务器python系统的简单配置客户机。

还有一个jvm version

安装

pip install serverless-config

pipenv install serverless-config

aws lambda在其环境中包含boto3,因此serverless config不将其作为依赖项包含,以减小部署包的大小。 如果希望在本地使用serverless config,请确保也安装boto3

快速启动

fromserverless_configimportdefault_configconfig=default_config()string_prop=config.get_str('string_prop')int_prop=config.get_int('missing_int_prop',default_value=123)secret_prop=config.get_str('secret_prop',WithDecryption=True)

默认配置将搜索具有以下优先顺序的参数:system environmentaws ssm parameter store。你可以在下面了解更多。

支持的配置源

系统环境

系统环境是存储微服务特定参数的好地方。它们设置在lambda函数本身上。

fromserverless_configimportEnvConfigconfig=EnvConfig()config.get_str('string_prop')

aws ssm参数存储

ssm非常适合存储跨微服务共享的参数,以及存储加密的机密。它是完全管理的,不需要任何配置即可启动。

注意:iam角色需要amazonssmreadonlyaccess策略才能从ssm获取属性。

fromserverless_configimportSsmConfigconfig=SsmConfig()config.get_str('string_prop')

秘密可以在传输过程中被解密。这样,您就不必担心配置iam角色以访问kms密钥。

fromserverless_configimportSsmConfigconfig=SsmConfig()config.get_str('string_prop',WithDecryption=True)

复合配置

默认配置将首先在系统环境中搜索。如果参数不存在,则它将在aws ssm中搜索。

fromserverless_configimportdefault_configconfig=default_config()

自定义配置

您甚至可以实现自己的自定义配置和组合配置!

fromserverless_configimportConfigBase,CompositeConfig,EnvConfigclassDictConfig(ConfigBase):def__init__(self,prop_dict):self.prop_dict=prop_dictdefget_str(prop_name,default_value=None):ifprop_nameinself.prop_dict:returnself.prop_dict[prop_name]elifdefault_value:returndefault_value# You must raise a ValueError if the property is not foundraiseValueError('Property not found: '+prop_name)# You can make a standalone custom configprops=dict(foo='bar',toll='troll')map_config=DictConfig(props)# And you can make a custom composite config with your new configcustom_config=CompositeConfig(map_config,EnvConfig())

缓存

默认配置将缓存属性5分钟。如果希望使用特定或自定义配置,可以将cachedconfig包装起来。

fromserverless_configimportdefault_configconfig=default_config()value=config.get_str('prop')# getting value from env and ssmvalue=config.get_str('prop')# getting cached value
fromdatetimeimporttimedeltafromserverless_configimportEnvConfig,SsmConfig,CachedConfig,CompositeConfigCachedConfig(SsmConfig())# config with default 5 minute cache durationCachedConfig(SsmConfig(),timedelta(hours=1)# config with 1 hour cache durationCachedConfig(CompositeConfig(SsmConfig(),EnvConfig()))# you can even cache a composite config!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何忽略缓冲读取器中在“”之后的行的其余部分,或行上的特定字符?   java在db中创建空对象或稍后保存   java如何实现UI无关的后台工作任务   java未能在Android中从BaseAdapter扩展的类中启动Tactivity?   java斐波那契迭代移动数组[]   安卓从文件读取提供了java。木卫一。StreamCorruptedException:无效的流标头:73720027   java计算矩形中的六边形数?   仅使用Java 1.5(或更早版本)读写XML   java如果所有元素都以相同的bucked结尾,为什么要进行大小调整?   java Apache POI Excel在xx中发现无法读取的内容。xlsx   swing我可以在普通java应用程序中使用GWTGUI吗?   来自自定义Java客户端的http删除请求的行为与邮递员不同   运行批处理文件时,java当前目录无效   使用TypeReference将java字符串转换为ArrayList<STRING>   documentlistener突出显示所有匹配词Java