Home > OS >  Use multiple functions in Lambda
Use multiple functions in Lambda

Time:10-10

I am wondering how you can separate the handler from the Lambda's core logic. I am always putting all my boto3 code inside the Lambda handler but how to define multiple functions in a Lambda function and use these functions inside the handler ? It seems it is a best practice to do that.

For example:

def list_users():
# list iam users here


def list_user_tags():
# list tags of these users here

def something_else():
# do something else


def handler_name(event, context):
# return the result here

CodePudding user response:

You can call the other functions from the handler function, such as:

def list_users():
# list iam users here


def list_user_tags():
# list tags of these users here

def something_else():
# do something else


def handler_name(event, context):
  users = list_users()
  tags = list_user_tags(users)

  return something_else(tags)

CodePudding user response:

You can define a service class that is separate from the handler. Based on the event, you can invoke different methods in service class. You can also extend this idea more via Factory pattern.

lambda(event) {
   ...inspect event
   ...instantiate service class
   ...use service class methods to achieve goal
}
  • Related