【问题标题】:AWS SAM Schedule Lambda is not triggering as per ScheduleAWS SAM 计划 Lambda 未按计划触发
【发布时间】:2021-08-15 07:52:33
【问题描述】:

我有一个用例,我需要每 2 分钟调用一次 API 来检查更新并将结果存储到数据库中。同样,我正在尝试使用 AWS SAM CLI 使用 Python 的 AWS Schedule lambda 函数,但我的 Lambda 函数没有被触发。以下是我的代码:

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......")

模板.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"

谁能告诉我,我的代码有什么问题或者我的代码缺少什么?

【问题讨论】:

    标签: python-3.x aws-sam aws-sam-cli


    【解决方案1】:

    我犯了错误。错误出现在“权限”部分。我在这里发布了正确的 yaml 配置,以便对 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......")
    

    模板.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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-28
      • 2023-01-05
      相关资源
      最近更新 更多