Use LookupEvents with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use LookupEvents with an AWS SDK or CLI

The following code examples show how to use LookupEvents.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// Routine which looks up events captured by AWS CloudTrail. /*! \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::CloudTrail::lookupEvents( const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudTrail::CloudTrailClient cloudtrail(clientConfig); Aws::String nextToken; // Used for pagination. Aws::Vector<Aws::CloudTrail::Model::Event> allEvents; Aws::CloudTrail::Model::LookupEventsRequest request; size_t count = 0; do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::CloudTrail::Model::LookupEventsOutcome outcome = cloudtrail.LookupEvents( request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::CloudTrail::Model::Event> &events = outcome.GetResult().GetEvents(); count += events.size(); allEvents.insert(allEvents.end(), events.begin(), events.end()); nextToken = outcome.GetResult().GetNextToken(); } else { std::cerr << "Error: " << outcome.GetError().GetMessage() << std::endl; return false; } } while (!nextToken.empty() && count <= 50); // Limit to 50 events. std::cout << "Found " << allEvents.size() << " event(s)." << std::endl; for (auto &event: allEvents) { std::cout << "Event name: " << event.GetEventName() << std::endl; std::cout << "Event source: " << event.GetEventSource() << std::endl; std::cout << "Event id: " << event.GetEventId() << std::endl; std::cout << "Resources: " << std::endl; for (auto &resource: event.GetResources()) { std::cout << " " << resource.GetResourceName() << std::endl; } } return true; }
  • For API details, see LookupEvents in AWS SDK for C++ API Reference.

CLI
AWS CLI

To look up events for a trail

The following lookup-events command looks up API activity events by the attribute EventName:

aws cloudtrail lookup-events --lookup-attributes AttributeKey=EventName,AttributeValue=ConsoleLogin

Output:

{ "Events": [ { "EventId": "654ccbc0-ba0d-486a-9076-dbf7274677a7", "Username": "my-session-name", "EventTime": "2021-11-18T09:41:02-08:00", "CloudTrailEvent": "{\"eventVersion\":\"1.02\",\"userIdentity\":{\"type\":\"AssumedRole\",\"principalId\":\"AROAJIKPFTA72SWU4L7T4:my-session-name\",\"arn\":\"arn:aws:sts::123456789012:assumed-role/my-role/my-session-name\",\"accountId\":\"123456789012\",\"sessionContext\":{\"attributes\":{\"mfaAuthenticated\":\"false\",\"creationDate\":\"2016-01-26T21:42:12Z\"},\"sessionIssuer\":{\"type\":\"Role\",\"principalId\":\"AROAJIKPFTA72SWU4L7T4\",\"arn\":\"arn:aws:iam::123456789012:role/my-role\",\"accountId\":\"123456789012\",\"userName\":\"my-role\"}}},\"eventTime\":\"2016-01-26T21:42:12Z\",\"eventSource\":\"signin.amazonaws.com\",\"eventName\":\"ConsoleLogin\",\"awsRegion\":\"us-east-1\",\"sourceIPAddress\":\"72.21.198.70\",\"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36\",\"requestParameters\":null,\"responseElements\":{\"ConsoleLogin\":\"Success\"},\"additionalEventData\":{\"MobileVersion\":\"No\",\"MFAUsed\":\"No\"},\"eventID\":\"654ccbc0-ba0d-486a-9076-dbf7274677a7\",\"eventType\":\"AwsConsoleSignIn\",\"recipientAccountId\":\"123456789012\"}", "EventName": "ConsoleLogin", "Resources": [] } ] }
  • For API details, see LookupEvents in AWS CLI Command Reference.

PowerShell
Tools for PowerShell

Example 1: Returns all events that have occurred over the last seven days. The cmdlet by default automatically makes multiple calls to deliver all events, exiting when the service indicates no further data is available.

Find-CTEvent

Example 2: Returns all events that have occurred over the last seven days specifying a region that is not the current shell default.

Find-CTEvent -Region eu-central-1

Example 3: Returns all events that are associated with the RunInstances API call.

Find-CTEvent -LookupAttribute @{ AttributeKey="EventName"; AttributeValue="RunInstances" }

Example 4: Returns the first 5 available events.

Find-CTEvent -MaxResult 5
  • For API details, see LookupEvents in AWS Tools for PowerShell Cmdlet Reference.

Ruby
SDK for Ruby
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

require 'aws-sdk-cloudtrail' # v2: require 'aws-sdk' # @param [Object] client def lookup_events_example(client) resp = client.lookup_events puts "Found #{resp.events.count} events:" resp.events.each do |e| puts "Event name: #{e.event_name}" puts "Event ID: #{e.event_id}" puts "Event time: #{e.event_time}" puts 'Resources:' e.resources.each do |r| puts " Name: #{r.resource_name}" puts " Type: #{r.resource_type}" puts '' end end end
  • For API details, see LookupEvents in AWS SDK for Ruby API Reference.