简体   繁体   English

在 Lambda 中使用多个函数

[英]Use multiple functions in Lambda

I am wondering how you can separate the handler from the Lambda's core logic.我想知道如何将处理程序与 Lambda 的核心逻辑分开。 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?我总是将我所有的 boto3 代码放在 Lambda 处理程序中,但是如何在 Lambda function 中定义多个函数并在处理程序中使用这些函数? 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

You can define a service class that is separate from the handler.您可以定义一个与处理程序分开的服务 class。 Based on the event, you can invoke different methods in service class. You can also extend this idea more via Factory pattern.根据事件,你可以在服务class中调用不同的方法。你也可以通过工厂模式更多地扩展这个想法。

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

You can call the other functions from the handler function, such as:您可以从处理程序 function 调用其他函数,例如:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM