Package: serverless-config
A simple configuration client for AWS serverless Python systems.There is also ajvm version.Installationpipinstallserverless-configorpipenvinstallserverless-configAWS lambda includesboto3in its environment, soserverless-configdoes not include it as a dependency in order to decrease the deployment package size.
If you wish to useserverless-configlocally, be sure to installboto3as well.Quickstartfromserverless_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)The default config will search for a parameter with the following order of precedence:System Environment,AWS SSM Parameter Store.  You can learn more about them below.Supported Config SourcesSystem EnvironmentThe System environment is a good place to store microservice-specific parameters.  They are set on the lambda function itself.fromserverless_configimportEnvConfigconfig=EnvConfig()config.get_str('string_prop')AWS SSM Parameter StoreSSM is perfect for storing parameters that are shared across microservices, and for storing encrypted secrets.  It is fully managed, and does not require any configuration to get started.Note: the IAM role requires theAmazonSSMReadOnlyAccesspolicy to get properties from SSM.fromserverless_configimportSsmConfigconfig=SsmConfig()config.get_str('string_prop')A secret can optionally be decrypted in transit.  That way, you do not need to worry about configuring your IAM role for access to the KMS Key.fromserverless_configimportSsmConfigconfig=SsmConfig()config.get_str('string_prop',WithDecryption=True)Composite ConfigsThedefault_configwill first search in thesystem environment.  If the  parameter is not there, then it will search inAWS SSM.fromserverless_configimportdefault_configconfig=default_config()Custom ConfigsYou can even implement your own custom configs and composite configs!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())CachingThedefault_configwill cache properties for 5 minutes.  If you wish to use a specific or custom config, you can wrap theCachedConfigaround it.fromserverless_configimportdefault_configconfig=default_config()value=config.get_str('prop')# getting value from env and ssmvalue=config.get_str('prop')# getting cached valuefromdatetimeimporttimedeltafromserverless_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!