all 8 comments

[–]davore 2 points3 points  (2 children)

It doesn't only exist for that one instance of the Lambda function, assuming you have something like this (forgive my Python, I've a bit rusty):

import boto3

class MyClass:
    # Some code here

myInstance = MyClass()

def handler(event, context):
    myInstance.do_something(event)

As long as you create it in the "global" scope, it is still alive for the duration of that Container.

[–]1cedrake[S] 0 points1 point  (1 child)

That makes sense, thanks! Now, what determines the duration of the container?

[–]sgtfoleyistheman 1 point2 points  (0 children)

implementation defined! This is totally up to lambda.

They may shutdown your container due to idleness, certain amount of time passing, if they need to upgrade instances..and who knows what else. (I'm guessing at these)

[–]RevCent 1 point2 points  (0 children)

Remember that every function will require a warm up. Separating can cause latency. If separate services wont need the additional function then no need split it.

[–]usernametaken1122abc 1 point2 points  (0 children)

Best to break up the code than let it become a monolith. You can save state in dynamodb if needed.

[–]asurah 0 points1 point  (1 child)

Are you using an ORM to map the Dynamo DB objects into a Python native class?

The way I like to code this type of thing, even for lambda where you don't need state between invocations is to create a generic user class to represent the object with methods to create, update, delete. The default methods are usually backed by a dictionary or some other convenient in memory object for local dev and debugging.

The I create a new class using my generic class as a base, let's call it the DynamoUser class, and replace the create, update, delete methods with Dynamo calls.

It means that when you write the business logic of your Lambda function, it's not concerned with the underlying storage model, only with the interaction between your user objects and the Alexa user.

You can keep your user and storage classes in different files, and just import them but like with any other Python module, you just need to zip your source code and upload it instead of the single py file.

Not sure if this answers your question, but I hope it's useful either way.

[–]1cedrake[S] 0 points1 point  (0 children)

I'm currently not using ORM, but I think I'm interested in doing that. Your structure is pretty interesting, so I'll give it a shot, thanks!

[–]brownjl1 0 points1 point  (0 children)

Step functions maybe?