skip to Main Content

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

2

Answers


  1. 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
    }
    
    Login or Signup to reply.
  2. 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)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search