3

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)
1
  • 2
    Please provide the codes for others to have a better picture of your question. See How to ask to improve the quality of the question. Commented Jan 30, 2019 at 3:20

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bwest really appreciate your help. I have a sample code here and I understood how to use it in the same function. But need help in sending the ID output to the list_instance_groups.
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)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.