0

I have a requirement to invoke 2 methods x and y specified in the lambda function at different times which is triggered by AWS Cloudwatch event. Is it possible to invoke the handler for different time units by passing in different values to the event parameter ? Or else should I create another lambda handler and invoke it separately?

def lambda_handler(event , context):
    
"""Run main logic of the script.
    This function is called by AWS to handle the request
    or by __main__ when run from the command-line.

    Args:
        context: Lambda context instance.

    Returns:
        A string saying "Successful invocation" on success.

    Raises:
        Exception: If a problem occurs.
    """
    # Set up logging
    app_config = config.Config(context.aws_request_id)

    x.run(app_config)

    y.run(app_config)

    return "Successful invocation."

Template for AWS Cloudwatch event:

Trigger:
    Type: AWS::Events::Rule
    Properties:
      Description: !Sub "Trigger the exp-${Environment} Lambda every ${FunctionInvocationRateValue} ${FunctionInvocationRateUnit}"
      Name: !Sub "exp-${Environment}"
      ScheduleExpression: !Sub "rate(${FunctionInvocationRateValue} ${FunctionInvocationRateUnit})"
      Targets:
        - Id: !Sub "exp-${Environment}"
          Arn: !GetAtt exp.Arn
          Input: '{}' # No input necessary for the function to run.
  • " Can I pass in different inputs in the event handler and make it invoke for different intervals?" - what does it mean? Can you clarify better what you want to do and what is wrong with your current code? – Marcin Sep 07 '21 at 10:14
  • @Marcin. Is it better now? – user2123498 Sep 07 '21 at 11:10

1 Answers1

0

looking long-term, you would be better off with two lambdas to handle X and Y process (e.g easy to debug, to improve code etc etc)

if you want to create a worklow based in a single entry, for example: cloudwatch triggers a single lambda and from that lambda you invoke lambda x or lambda y, you can use aws step functions to manage your workflow

https://aws.amazon.com/blogs/aws/new-aws-step-functions-express-workflows-high-performance-low-cost/

enter image description here

oieduardorabelo
  • 2,687
  • 18
  • 21