AWS SDK for .NET
Developer Guide

Using Subscription Filters in Amazon CloudWatch Logs

This .NET example shows you how to:

  • List a subscription filter in CloudWatch Logs

  • Create or delete a subscription filter in CloudWatch Logs

The Scenario

Subscriptions provide access to a real-time feed of log events from CloudWatch Logs and deliver that feed to other services such as an Amazon Kinesis Data Streams or AWS Lambda for custom processing, analysis, or loading to other systems. A subscription filter defines the pattern to use for filtering which log events are delivered to your AWS resource. This example shows how to list, create, and delete a subscription filter in CloudWatch Logs. The destination for the log events is a Lambda function.

This example uses the AWS SDK for .NET to manage subscription filters using these methods of the AmazonCloudWatchLogsClient class:

For more information about CloudWatch Logs subscriptions, see Real-time Processing of Log Data with Subscriptions in the Amazon CloudWatch Logs User Guide.

Prerequisite Tasks

To set up and run this example, you must first:

Describe Existing Subscription Filters

Create an AmazonCloudWatchLogsClient object. Create a DescribeSubscriptionFiltersRequest object containing the parameters needed to describe your existing filters. Include the name of the log group and the maximum number of filters you want described. Call the DescribeSubscriptionFilters method.

public static void DescribeSubscriptionFilters() { var client = new AmazonCloudWatchLogsClient(); var request = new Amazon.CloudWatchLogs.Model.DescribeSubscriptionFiltersRequest() { LogGroupName = "GROUP_NAME", Limit = 5 }; try { var response = client.DescribeSubscriptionFilters(request); } catch (Amazon.CloudWatchLogs.Model.ResourceNotFoundException e) { Console.WriteLine(e.Message); } }

Create a Subscription Filter

Create an AmazonCloudWatchLogsClient object. Create a PutSubscriptionFilterRequest object containing the parameters needed to create a filter, including the ARN of the destination Lambda function, the name of the filter, the string pattern for filtering, and the name of the log group. Call the PutSubscriptionFilter method.

public static void PutSubscriptionFilters() { var client = new AmazonCloudWatchLogsClient(); var request = new Amazon.CloudWatchLogs.Model.PutSubscriptionFilterRequest() { DestinationArn = "LAMBDA_FUNCTION_ARN", FilterName = "FILTER_NAME", FilterPattern = "ERROR", LogGroupName = "Log_Group" }; try { var response = client.PutSubscriptionFilter(request); } catch (InvalidParameterException e) { Console.WriteLine(e.Message); } }

Delete a Subscription Filter

Create an AmazonCloudWatchLogsClient object. Create a DeleteSubscriptionFilterRequest object containing the parameters needed to delete a filter, including the names of the filter and the log group. Call the DeleteSubscriptionFilter method.

public static void DeleteSubscriptionFilter() { var client = new AmazonCloudWatchLogsClient(); var request = new Amazon.CloudWatchLogs.Model.DeleteSubscriptionFilterRequest() { LogGroupName = "GROUP_NAME", FilterName = "FILTER" }; try { var response = client.DeleteSubscriptionFilter(request); } catch (Amazon.CloudWatchLogs.Model.ResourceNotFoundException e) { Console.WriteLine(e.Message); } }