Lambda Tips
@WIP
Taking advantage of a running Lambda function and it’s state
https://aws.amazon.com/blogs/compute/sharing-secrets-with-aws-lambda-using-aws-systems-manager-parameter-store/ under the section Lambda function has a nice “trick” of setting above the class app = None
then later on it will see if that is set
def lambda_handler(event, context):
global app
# Initialize app if it doesn't yet exist
if app is None:
print("Loading config and creating new MyApp...")
config = load_config(full_config_path)
app = MyApp(config)
return "MyApp config is " + str(app.get_config()._sections)
If it is set it will not try to set it again but take advantage of the state and use it.
Keep Warm
https://read.acloud.guru/how-to-keep-your-lambda-functions-warm-9d7e1aa6e2f0
You can set a bunch of schedulers and your Lambda function can check for the context of the request. If it is a scheduler event then just reply OK otherwise it should do what it normally would do.
import boto3
from config import Config
class KeepAwake:
def __init__(self):
""" keey awake """
self.config = Config()
self.region = self.config.region
self.app_env = self.config.app_env
self.client = boto3.client('lambda', region_name=self.region)
self.functions = [
"foo",
"bar",
]
def run(self):
""" interate over lambda functions """
for lam in self.functions:
print("Invoking ", lam)
self.client.invoke(
FunctionName=lam,
InvocationType="Event"
)
print("Invoked ", lam)
Is another way to look around and call those functions.
comments powered by Disqus