如何为来自 Serverless.yaml 的预定事件规则设置 TargetId(可能是函数名称)

How to set TargetId (possibly function name) for a scheduled event rule from Serverless.yaml

我需要为规则设置目标 ID。到目前为止,targetId 是 testLambdaSchedule,但我想将其设置为自定义 ID。

如果有办法做到这一点,请告诉我。

functions:
  testLambda:
    handler: handler.testingingLambda
    timeout: 300
    events:
      - schedule:
          name: newestTestRule
          rate: cron(0 12 5 4 ? 2005) 

然后您需要在 serverless.yaml 文件中将您的计划规则描述为单独的资源。

例如:

ScheduledRule: 
  Type: AWS::Events::Rule
  Properties: 
    Description: "ScheduledRule"
    ScheduleExpression: "rate(10 minutes)"
    State: "ENABLED"
    Targets: 
      - 
        Arn: 
          Fn::GetAtt: 
            - "LambdaFunction"
            - "Arn"
        Id: "TargetFunctionV1"

AWS::Events::Rule

functions:
  testLambdaModified:
    handler: handler.testingingLambda
    timeout: 300
    events:
      - schedule:
          name: newestTestRule
          rate: cron(0 12 5 4 ? 2005)
resources:
  Resources:
    TestLambdaModifiedEventsRuleSchedule1: # it is  {normalizedFunctionName}EventsRuleSchedule{SequentialID}
      Type: AWS::Events::Rule
      Properties:
        Targets:
          - Id: "anyId"

这是修改 Cloudformation 资源的指南。 https://serverless.com/framework/docs/providers/aws/guide/resources#override-aws-cloudformation-resource