serverless-plugin-existing-cloudwatch-rule v1.0.1
serverless-plugin-existing-cloudwatch-rule
Allows an AWS Lambda function to be triggered by pre-configured CloudWatch event rules.
In contrast to the traditional schedule event that creates a new CloudWatch rule, the plugin assumes that an existing rule is already in place, and the Lambda function is specified as one of its targets. The plugin just adds necessary permissions to the Lambda function itself to complete the "link" between CloudWatch and Lambda.
Useful for projects that setup and scale infrastructure separately from code, e.g., deploy a Lambda function triggered by multiple timers or s3 events that are configured by scripts like Terraform, a separate CloudFormation template, or even a manually created infrastructure.
Also, can be useful for subscribing to an external (cross-account) rule.
Example serverless.yml:
plugins:
- serverless-plugin-existing-cloudwatch-rule
functions:
key-rotation-lambda: #1
handler: src/key-rotation-lambda.handler
events:
- cloudWatchRule: key-rotation-timer
counter-lambda: #2
handler: src/counter-lambda.handler
events:
- cloudWatchRuleArn: 'arn:aws:events:us-east-1:160879880353:rule/my-project-MidnightSchedule-42UGHOTBBVIET'
scalable-lambda: #3
handler: src/scalable-lambda.handler
events:
- cloudWatchRule: ANYIn the example,
key-rotation-timeris assumed to be setup and pointing tokey-rotation-lambda. The functionkey-rotation-lambdawill be deployed onserverless deployand, due to the plugin, with the necessary permissions to be invoked bykey-rotation-timer. The timer will be shown on the AWS Lambda -> Triggers page as if it was setup manually or using the traditionalscheduleevent. The prefixrule/for the name is supported but optional for the plugin.To attach a rule defined by its full ARN, use the
cloudWatchRuleArnproperty, such as in thecounter-lambdaexample.The
ANYkeyword allow for any trigger under the current AWS account, soserverlessdoes not need to run on every change to the project's infrastructure. In the example, a new timer can be added that triggersscalable-lambdawith a different set of arguments, but developers do not need to runserverless deployor use CLI to change the permissions on the function.