This blog was co-authored by Ranjit Kalidasan, Senior Solutions Architect at AWS.
Amazon CloudWatch Logs enables you to centralize the logs from different AWS services, logs from your applications running in AWS and on-prem servers, using a single highly scalable service. You can then easily view these logs data, search them for specific error codes or patterns, filter them based on specific fields, or archive them securely for future analysis. You can ingest these CloudWatch Logs into Splunk for use cases such as security analysis, application troubleshooting and audit compliance requirements.
You can use the subscription filters feature in CloudWatch Logs to get access to a real-time feed of log events and have it delivered to other services, such as an Amazon Kinesis stream, an Amazon Data Firehose stream, or AWS Lambda for custom processing, analysis, or loading to other systems. When log events are sent to the receiving service, they are base64 encoded and compressed with the gzip format.
In this blog, we will explain how to set up a subscription filter with AWS Lambda to ingest CloudWatch Logs data into different Splunk destinations like Splunk Cloud Platform, customer-managed Splunk Enterprise clusters running on AWS or Splunk Enterprise hosted in on-prem data centers.
Figure 1: Example Architecture for CloudWatch Logs, Lambda & Splunk
The solution used in this blog, deploys a Lambda function to process the CloudWatch Logs and ingest into Splunk over HEC endpoint. This Lambda function has options to customize the log ingestion with features like including Splunk source types, using indexer acknowledgement feature to check the durability of ingested data and debugging. This lambda application is deployed as a serverless application. The source code and deployment instructions can be found in this aws-samples repository. You will require AWS SAM cli with AWS credentials and a desktop of IDE with python 3.9 installation.
Clone this repo locally and follow the deployment steps.
Use the following reference to fill in the application parameters during deployment.
These input parameters for serverless application are mapped to Lambda Environment Variables as follows.
Serverless Application Parameter | Lambda Environment Variable |
---|---|
SplunkHttpEventCollectorURL | HEC_HOST |
SplunkHttpEventCollectorToken | HEC_TOKEN |
SplunkSourceType | SOURCE_TYPE |
SplunkHttpEventCollectorType | HEC_ENDPOINT_TYPE |
SplunkAcknowledgementRequired | ACK_REQUIRED |
SplunkAcknowledgementRetries | ACK_RETRIES |
SplunkAcknowledgementWaitSeconds | ACK_WAIT_SECS |
ELBCookieName | ELB_COOKIE_NAME |
HTTPRequestTimeout | REQUEST_TIMEOUT |
HTTPVerifySSL | VERIFY_SSL |
DebugData | VERIFY_SSL |
The deployment steps will create and submit a CloudFormation template in the AWS account and AWS region. Once the CloudFormation stack is completed, a Lambda function will be created. Note the physical id of the Lambda function deployed. We will need this physical id in the next section, when we create the CloudWatch Logs subscription filter.
To create the subscription filter, go to CloudWatch Logs console and select the Log group. Go to Subscription filters tab and create the subscription filter for Lambda.
Figure 2: Subscription Filter
Select the Lambda function you created in the preceding step using the serverless App, provide a name for the subscription filter and select Start Streaming to create the subscription filter.
Now you can view your data ingested in Splunk.
To troubleshoot and monitor the Lambda function execution, you can use CloudWatch Logs Insights. Here are some of the sample queries you can use for various troubleshooting scenarios:
fields @message | parse @message "[*] *" as loggingType, loggingMessage | filter loggingType = "ERROR" | display loggingMessage
To get the count of error messages by 5 mins interval:
fields @message | parse @message "[*] *" as loggingType, loggingMessage | filter loggingType = "ERROR" | stats count() by bin(5m)
fields @timestamp, @message | filter @message like /Connection Error/
To get the count of connection errors by 5 mins interval:
fields @timestamp, @message | filter @message like /Connection Error/ | stats count() by bin(5m)
Check the network connectivity for any connection errors. If the Lambda function is a public function then ensure the Splunk endpoint is a public endpoint reachable over internet. If the access to Splunk endpoint is firewall protected and need to be enabled for Lambda access, then refer this URL for AWS services public endpoints by AWS regions. If you configured your Lambda for VPC Access, ensure you have network connectivity to Splunk endpoints from your VPC where Lambda is configured.
fields @timestamp, @message, @logStream, @log | filter @message Like /Acknowledgement Failed/
To get the count of failures by 5 mins interval:
fields @timestamp, @message, @logStream, @log | filter @message Like /Acknowledgement Failed/ | stats count() by bin(5m)
The Lambda function checks for ingestion acknowledgement if serverless parameter SplunkAcknowledgementRequired or Lambda environment variable ACK_REQUIRED is set to true. For any acknowledgement failures, try changing the Lambda environment variables for Acknowledgement (ACK_RETRIES & ACK_WAIT_SECS) to a higher values.
To avoid incurring future charges, delete the resources you created in the following order:
This blog explains how to use Lambda as a solution to ingest CloudWatch Logs into Splunk destinations. The serverless application is quite extensible to ingest any type of AWS and 3rd party logs from CloudWatch into Splunk destinations running anywhere. This will be an efficient and cost optimized solution for customers looking to ingest volume log data from CloudWatch into Splunk using Lambda as ingestion mechanism.
The Splunk platform removes the barriers between data and action, empowering observability, IT and security teams to ensure their organizations are secure, resilient and innovative.
Founded in 2003, Splunk is a global company — with over 7,500 employees, Splunkers have received over 1,020 patents to date and availability in 21 regions around the world — and offers an open, extensible data platform that supports shared data across any environment so that all teams in an organization can get end-to-end visibility, with context, for every interaction and business process. Build a strong data foundation with Splunk.