I want to configure an Amazon CloudWatch subscription filter to invoke my AWS Lambda function.
With Amazon CloudWatch Logs, you can use a subscription filter that sends log data to your Lambda function. CloudWatch Logs subscription filters are base64 encoded and compressed with the GZIP format.
Before you create your Lambda function, calculate the volume of log data that will be generated. Be sure to create a function that can manage the volume amount. If the function doesn't have enough volume, then the log stream is throttled. For more information, see Lambda quotas.
Note: Streaming large amounts of CloudWatch Logs data might result in high usage charges. It's a best practice to use AWS Budgets to track spending and usage. For instructions, see How can I use AWS Budgets to track my spending and usage?
Create a CloudWatch Logs subscription filter that sends log data to your AWS Lambda function.
Note: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.
1. To provide CloudWatch Logs permission to invoke your Lambda function, run the AWS CLI command add-permission similar to the following:
aws lambda add-permission \
--function-name "helloworld" \
--statement-id "helloworld" \
--principal "logs.amazonaws.com" \
--action "lambda:InvokeFunction" \
--source-arn "arn:aws:logs:region:123456789123:log-group:YourLogGroup:*" \
Important: Replace "helloworld" with your Lambda function name, "YourLogGroup" with your log group, and the example account number with your account.
2. Create a subscription filter using the AWS CLI command put-subscription-filter to send log events that contains a keyword. In the following example, the keyword "ERROR" is used for the Lambda function:
Important: Replace "YourLogGroup" with your log group and the example account number with your account.
aws logs put-subscription-filter \
--log-group-name YourLogGroup \
--filter-name demo \
--filter-pattern "ERROR" \
The CloudWatch log group "YourLogGroup" invokes the Lambda function when it receives a log event that contains the keyword "ERROR" similar to the following:
Filter and pattern syntax