Skip to content

Custom CloudTrail events for my custom application

0

Hi everyone,

I understand that CloudTrail is mainly used to monitor activities within my AWS account, and I can ingest custom events into CloudTrail.

My plan is to ingest events from my custom application (desktop, web, mobile) into CloudTrail. For instance, I will ingest events such as user logins, booking deletions, and invoice creations, including details like who performed these actions and when. Later, I also want to retrieve these events to view them in my application, which will help me determine what actions users took if something goes wrong.

Is this approach feasible? Specifically, can CloudTrail be used to achieve this? If the answer is No, please recommend me alternative services that are suitable for this purpose?

Thank you so much in advance.

3 Answers
2
Accepted Answer

1. AWS CloudWatch Logs and CloudWatch Events:

Ingestion: You can ingest custom application events into CloudWatch Logs. This allows you to send log data from your application (e.g., user actions like logins, bookings, and invoice creations) to CloudWatch Logs.

Monitoring and Alerts: Once your data is in CloudWatch Logs, you can create CloudWatch Alarms to monitor specific patterns or thresholds. Retrieval and Analysis: You can use CloudWatch Logs Insights to query and analyze the log data. This would allow you to retrieve specific events or patterns of user behavior, which can be useful for debugging or monitoring purposes.

2. AWS EventBridge:

Event Ingestion: You can use Amazon EventBridge (formerly known as CloudWatch Events) to ingest custom application events. EventBridge allows you to define event buses that can capture events from your application, enabling you to filter, route, and process them as needed.

Event Processing: With EventBridge, you can route events to various AWS services (e.g., Lambda, SQS, SNS) for processing, storage, or further analysis.

Custom Event Bus: You can create a custom event bus for your application to handle and store custom events in a scalable and reliable way.

3. AWS S3 and Athena:

Log Storage: If you prefer to store logs in a centralized location, you can direct your application logs to Amazon S3.

Querying Logs: Use AWS Athena to run SQL queries on the log files stored in S3, allowing you to analyze user actions without having to move the data elsewhere.

4. AWS X-Ray:

Tracing: If your application involves distributed components, you can use AWS X-Ray to trace and analyze requests as they travel through your application. X-Ray can provide insights into user actions, latency issues, and errors, helping you diagnose problems.

Feasibility of Your Approach:

CloudTrail: Not suitable for custom application events; designed for AWS service API tracking.

Alternative: Use CloudWatch Logs, EventBridge, S3 with Athena, or X-Ray for the kind of event tracking and retrieval you described.

Suggested Approach:

CloudWatch Logs + CloudWatch Events/Alarms for real-time monitoring and alerting.

EventBridge for advanced event routing and processing.

S3 + Athena for cost-effective log storage and query capability.

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

EXPERT

reviewed 2 years ago

  • Thank you so much for your helpful answer. I will choose one from one of your recommended approaches.

  • Is there a Java SDK to get the CloudWatch Logs Insights query results? My plan is to let an admin user query the CloudWatch logs in my application. For example, the admin can get the logs for a specific user, or specific actions, or within a certain time period. Thanks again.

  • Hlo , please check below i posed another answer in below

1

Hi AKMin,

Please go through the below steps. I hope it will help solve your issue.

1. Amazon CloudWatch Logs

  • Use Case: You can use CloudWatch Logs to collect and monitor log data from your applications. You can create custom log groups and streams to capture events such as user logins and booking deletions.

  • Benefits: You can create metric filters to generate metrics based on log data, set up alarms, and even create dashboards to visualize the logs.

  • Integration: Your application can send logs directly to CloudWatch using the AWS SDK or API.

https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/WhatIsCloudWatchLogs.html

2. Amazon Kinesis Data Streams

  • Use Case: Kinesis Data Streams is useful for real-time processing of streaming data. You can stream custom application events to Kinesis and process them in real time.

  • Benefits: Provides real-time analytics and processing capabilities. You can also store the data in Amazon S3 or other data stores for later retrieval and analysis.

  • Integration: Your application can push events to Kinesis Data Streams, and you can use Lambda functions or Kinesis Data Firehose to process and store the data.

https://docs.aws.amazon.com/streams/latest/dev/introduction.html

3. Amazon EventBridge

  • Use Case: EventBridge (formerly CloudWatch Events) is a service for building event-driven architectures. You can publish custom events to EventBridge and create rules to route these events to various targets like Lambda functions, SQS queues, or SNS topics.

  • Benefits: It’s suitable for building loosely-coupled and distributed systems where you want to decouple event producers and consumers.

  • Integration: You can define custom events and rules to handle different types of application events.

https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-what-is.html

4. Amazon DynamoDB Streams

  • Use Case: If you use DynamoDB to store your application data, DynamoDB Streams can capture changes to your DynamoDB tables and trigger Lambda functions to process these changes.

  • Benefits: Provides a way to react to data modifications in real time and integrate with other AWS services.

https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_StreamSpecification.html

5. AWS S3 with Lambda

  • Use Case: You can use S3 to store log files or event data, and trigger Lambda functions for processing these logs.

  • Benefits: Flexible storage with S3 combined with serverless compute using Lambda

https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html

https://docs.aws.amazon.com/lambda/latest/dg/welcome.html

EXPERT

answered 2 years ago

  • Thanks a lot for your comprehensive answer. I will just pick one from your recommended approaches. I really appreciate it.

  • Hi there. Is there a Java SDK to get the CloudWatch Logs Insights query results? My plan is to let an admin user query the CloudWatch logs in my application. For example, the admin can get the logs for a specific user, or specific actions, or within a certain time period. Thanks again.

0

Yes, the AWS SDK for Java provides support for interacting with CloudWatch Logs, including running Insights queries and retrieving their results. Below is a general overview of how you can use the Java SDK to achieve your goal:

1. Set Up AWS SDK for Java

Ensure that you have the AWS SDK for Java set up in your project. If you're using Maven, include the following dependency in your pom.xml:

<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>cloudwatchlogs</artifactId>
    <version>2.20.0</version> <!-- Use the latest version -->
</dependency>

2. Running a CloudWatch Logs Insights Query

To run a CloudWatch Logs Insights query, you'll use the StartQueryRequest to initiate the query and GetQueryResultsRequest to retrieve the results.

Here's an example in Java:

import software.amazon.awssdk.services.cloudwatchlogs.CloudWatchLogsClient;
import software.amazon.awssdk.services.cloudwatchlogs.model.StartQueryRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.StartQueryResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.GetQueryResultsRequest;
import software.amazon.awssdk.services.cloudwatchlogs.model.GetQueryResultsResponse;
import software.amazon.awssdk.services.cloudwatchlogs.model.QueryStatus;

public class CloudWatchLogsInsightsExample {
    public static void main(String[] args) {
        // Initialize the CloudWatch Logs client
        CloudWatchLogsClient logsClient = CloudWatchLogsClient.builder().build();

        // Define the log group(s) to query
        String logGroupName = "/aws/lambda/your-log-group";
        
        // Define the query string (e.g., filter logs for a specific user)
        String queryString = "fields @timestamp, @message | filter @message like /your-search-term/ | sort @timestamp desc";

        // Specify the start and end time for the query
        long startTime = System.currentTimeMillis() - 3600 * 1000; // 1 hour ago
        long endTime = System.currentTimeMillis();

        // Create the StartQueryRequest
        StartQueryRequest startQueryRequest = StartQueryRequest.builder()
                .logGroupName(logGroupName)
                .startTime(startTime)
                .endTime(endTime)
                .queryString(queryString)
                .build();

        // Start the query
        StartQueryResponse startQueryResponse = logsClient.startQuery(startQueryRequest);
        String queryId = startQueryResponse.queryId();
        System.out.println("Started query with ID: " + queryId);

        // Poll for the query results
        GetQueryResultsResponse queryResultsResponse;
        do {
            try {
                // Wait for a short period before checking the status again
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            GetQueryResultsRequest getQueryResultsRequest = GetQueryResultsRequest.builder()
                    .queryId(queryId)
                    .build();

            queryResultsResponse = logsClient.getQueryResults(getQueryResultsRequest);

        } while (queryResultsResponse.status() == QueryStatus.RUNNING);

        // Process and display the query results
        queryResultsResponse.results().forEach(result -> {
            result.forEach(field -> {
                System.out.printf("%s: %s\n", field.field(), field.value());
            });
            System.out.println("----");
        });

        logsClient.close();
    }
}

3. Explanation of the Code:

CloudWatchLogsClient: This is the client that interacts with AWS CloudWatch Logs.

StartQueryRequest: This request starts a CloudWatch Logs Insights query. You specify the log group, query string, and the time range for the query.

GetQueryResultsRequest: After starting the query, you poll the status of the query using this request. When the status is no longer "RUNNING," you can retrieve the results.

Query Results Processing: The results are displayed by iterating through the fields returned by the query.

4. Usage in Your Application:

Admin Interface: In your application, you can provide an admin interface where the admin user can input the search criteria (user ID, action type, time period).

Execute Queries: Use the above logic to execute the corresponding CloudWatch Logs Insights queries.

Display Results: Retrieve and display the query results to the admin user in a user-friendly format.

EXPERT

answered 2 years ago

  • Thank you so much for your detailed answer. It is very helpful.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.