0

I have a use case where I need to call one API every 2 minutes to check the updates and store the result into the database. For the same, I am trying the AWS Schedule lambda function using AWS SAM CLI using Python but my Lambda function is not getting triggered. Below is my code:

app.py

def lambda_schedule(event, context):
    print("Lambda Schedule event started Successfully......")
    print("Lambda function ARN:", context.invoked_function_arn)
    print("CloudWatch log stream name:", context.log_stream_name)
    print("CloudWatch log group name:", context.log_group_name)
    print("Lambda Request ID:", context.aws_request_id)
    print("Lambda Schedule event ended Successfully......")

template.yaml

CronLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_schedule
      Runtime: python3.8
    Events:
      PullBalanceScheduleRule:
        Type: AWS::Events::Rule
        Properties:
          EventPattern:
            source:
              - "aws.events"

PullBalanceScheduleRule:
    Type: AWS::Events::Rule
    Properties:
      Description: "PullBalanceScheduleRule"
      ScheduleExpression: "rate(2 minutes)"
      State: "ENABLED"
      Targets:
        -
          Arn: !GetAtt CronLambdaFunction.Arn
          Id: "CronLambdaFunction"
  PermissionForEventsToInvokeLambda:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref "PullBalanceScheduleRule"
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn:
        -
          Arn: !GetAtt PullBalanceScheduleRule.Arn
          Id: "PullBalanceScheduleRule"

can anyone tell me, what is wrong in my code OR what is missing in my code?

Mihir Shah
  • 948
  • 10
  • 17

1 Answers1

0

I got my mistakes. Mistakes were in the Permission sections. I am posting here correct yaml configurations so it could help to someone who are new to AWS SAM CLI.

app.py

def lambda_schedule(event, context):
    print("Lambda Schedule event started Successfully......")
    print("Lambda function ARN:", context.invoked_function_arn)
    print("CloudWatch log stream name:", context.log_stream_name)
    print("CloudWatch log group name:", context.log_group_name)
    print("Lambda Request ID:", context.aws_request_id)
    print("Lambda Schedule event ended Successfully......")

template.yaml

CronLambdaFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_schedule
      Runtime: python3.8
    Events:
      PullBalanceScheduleRule:
        Type: AWS::Events::Rule
        Properties:
          EventPattern:
            source:
              - "aws.events"

PullBalanceScheduleRule:
    Type: AWS::Events::Rule
    Properties:
      Description: "PullBalanceScheduleRule"
      ScheduleExpression: "rate(2 minutes)"
      State: "ENABLED"
      Targets:
        -
          Arn: !GetAtt CronLambdaFunction.Arn
          Id: "CronLambdaFunction"
PermissionForEventsToInvokeLambda:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref "CronLambdaFunction"
      Action: "lambda:InvokeFunction"
      Principal: "events.amazonaws.com"
      SourceArn:
        Fn::GetAtt:
          - "PullBalanceScheduleRule"
          - "Arn"
Mihir Shah
  • 948
  • 10
  • 17