简体   繁体   English

AWS SAM 计划 Lambda 未按计划触发

[英]AWS SAM Schedule Lambda is not triggering as per Schedule

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.我有一个用例,我需要每 2 分钟调用一次 API 来检查更新并将结果存储到数据库中。 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. 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应用程序.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模板.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?谁能告诉我,我的代码有什么问题或我的代码中缺少什么?

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.我在这里发布了正确的 yaml 配置,因此它可以帮助刚接触 AWS SAM CLI 的人。

app.py应用程序.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模板.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"

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

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