简体   繁体   中英

Using two python functions boto3 in a single AWS Lambda

I want to write two functions in a single AWS Lambda function. currently, Lambda triggers the function using the handler in case I have two functions in my code then how to change it so that the Lambda handler can execute both the functions.

I have found this. However, it is using if statement. In my scenario I will have to run both the functions one after the other and also pass the output of 1st function to the 2nd function. Thanks How to have more than one handler in AWS Lambda Function?

Here is the sample code:

import boto3' import json' from datetime 
import datetime REGION = 'us-east-1' 
emrclient = boto3.client('emr', region_name=REGION) 

def lambda_handler(event, context): 
EMRS = emrclient.list_clusters( ClusterStates = ['STARTING', 'RUNNING', 'WAITING', 'TERMINATING'] ) 
clusters = EMRS["Clusters"] 
    for cluster in clusters : 
    ID = cluster.get("Id") 
    EMRid = emrclient.list_instance_groups( ClusterId = str("ID") ) 
    print(EMRid)

It's hard to say what the best solution is without more information on your use case, but AWS Step Functions are designed to handle running multiple lambdas with data passed between them in a robust way (retries, parallelization, etc).

This blog post provides a good overview, though the code in the example is JavaScript rather than Python.

You don't need an additional function to achieve what you have listed as an example. That said, assuming that you need to add additional processing/functionality on the instance_groups dict, you can start here. This should work, if you have 50 clusters or less:

import boto3
import json
import datetime

emrclient = boto3.client('emr', region_name='us-east-1') 

def lambda_handler(event, context):
    EMRS = emrclient.list_clusters(ClusterStates=['STARTING', 'RUNNING', 'WAITING', 'TERMINATING'])
    for cluster in EMRS['Clusters']:
        emr_inst_grp_work(cluster['Id'])

def emr_inst_grp_work(cluster_id):
    """Get instance group details dict for given cluster id"""
    inst_dict = emrclient.list_instance_groups(ClusterId = cluster_id)
    print(inst_dict)
    # ADD code for additional dict or cluster work/processing here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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