Stream Amazon CloudWatch Logs to Splunk Using AWS Lambda

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

Deploy Serverless Application

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.

Deploying the Solution

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.

Best Practices

  1. For Lambda scaling, use Reserved or Provisioned concurrency settings for Lambda.
  2. If the Splunk indexers are hosted privately in a VPC or in an on-prem data center with network connectivity to Amazon VPC like Direct Connect or VPN, you can configure your lambda function for VPC Access for ingesting the CloudWatch Logs data. The Lambda function will require to have appropriate network access to the Splunk indexers using route table entries, security group rules, NACL rules etc.
  3. Deploy the Lambda function for single source type and ensure your Log Groups contains data for that source type. For example: If you deployed the function for cloudtrail log data and configured for the Log Group for CloudTrail data, do not use the same Lambda function for VPC Flow Logs. Deploy another function for VPC Flow Log Groups.
  4. Use DEBUG_DATA Lambda environment variable for debugging and CloudWatch Insights for troubleshooting. Help for troubleshooting with CloudWatch Insights are given below.

Troubleshooting & Monitoring Using Cloudwatch Log Insights

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:

Check Error Messages

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)

Check for Connection Errors

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.

Check for Acknowledgment Failures

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.

Clean Up

To avoid incurring future charges, delete the resources you created in the following order:

  1. Delete the CloudWatch Logs Subscription Filter
  2. Delete the CloudFormation Stack for the serverless application

Conclusion

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.

Related Articles

Announcing the General Availability of Splunk POD: Unlock the Power of Your Data with Ease
Platform
2 Minute Read

Announcing the General Availability of Splunk POD: Unlock the Power of Your Data with Ease

Splunk POD is designed to simplify your on-premises data analytics, so you can focus on what really matters: making smarter, faster decisions that drive your business forward.
Introducing the New Workload Dashboard: Enhanced Visibility, Faster Troubleshooting, and Deeper Insights
Platform
3 Minute Read

Introducing the New Workload Dashboard: Enhanced Visibility, Faster Troubleshooting, and Deeper Insights

Announcing the general availability of the new workload dashboard – a modern and intuitive dashboard experience in the Cloud Monitoring Console app.
Leading the Agentic AI Era: The Splunk Platform at Cisco Live APJ
Platform
5 Minute Read

Leading the Agentic AI Era: The Splunk Platform at Cisco Live APJ

The heart of our momentum at Cisco Live APJ is our deeper integration with Cisco, culminating in the Splunk POD and new integrations, delivering unified, next-generation data operations for every organization.
Dashboard Studio: Token Eval and Conditional Panel Visibility
Platform
4 Minute Read

Dashboard Studio: Token Eval and Conditional Panel Visibility

Dashboard Studio in Splunk Cloud Platform can address more complex use cases with conditional panel visibility, token eval, and custom visualizations support.
Introducing Resource Metrics: Elevate Your Insights with the New Workload Dashboard
Platform
4 Minute Read

Introducing Resource Metrics: Elevate Your Insights with the New Workload Dashboard

Introducing Resource Metrics in Workload Dashboard (WLD) – a modern and intuitive monitoring experience in the Cloud Monitoring Console (CMC) app.
Powering AI Innovation with Splunk: Meet the Cisco Data Fabric
Platform
3 Minute Read

Powering AI Innovation with Splunk: Meet the Cisco Data Fabric

The Cisco Data Fabric brings AI-centric advancements to the Splunk Platform, seamlessly connecting knowledge, business, and machine data.
Remote Upgrader for Windows Is Here: Simplifying Fleet-Wide Forwarder Upgrades
Platform
3 Minute Read

Remote Upgrader for Windows Is Here: Simplifying Fleet-Wide Forwarder Upgrades

Simplify fleet-wide upgrades of Windows Universal Forwarders with Splunk Remote Upgrader—centralized, signed, secure updates with rollback, config preservation, and audit logs.
Dashboard Studio: Spec-TAB-ular Updates
Platform
3 Minute Read

Dashboard Studio: Spec-TAB-ular Updates

Splunk Cloud Platform 10.0.2503 includes a number of enhancements related to tabbed dashboards, trellis for more charts, and more!
Introducing Edge Processor for Splunk Enterprise: Data Management on Your Premises
Platform
2 Minute Read

Introducing Edge Processor for Splunk Enterprise: Data Management on Your Premises

Announcing the introduction of Edge Processor for Splunk Enterprise 10.0, designed to help customers achieve greater efficiencies in data transformation and improved visibility into data in motion.