CloudTrail examples using SDK for C++ - AWS SDK Code Examples

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

CloudTrail examples using SDK for C++

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for C++ with CloudTrail.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateTrail.

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 creates an AWS CloudTrail trail. /*! \param trailName: The name of the CloudTrail trail. \param bucketName: The Amazon S3 bucket designate for publishing logs. \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::CloudTrail::createTrail(const Aws::String trailName, const Aws::String bucketName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudTrail::CloudTrailClient trailClient(clientConfig); Aws::CloudTrail::Model::CreateTrailRequest request; request.SetName(trailName); request.SetS3BucketName(bucketName); Aws::CloudTrail::Model::CreateTrailOutcome outcome = trailClient.CreateTrail( request); if (outcome.IsSuccess()) { std::cout << "Successfully created trail " << trailName << std::endl; } else { std::cerr << "Failed to create trail " << trailName << ": " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see CreateTrail in AWS SDK for C++ API Reference.

The following code example shows how to use DeleteTrail.

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 deletes an AWS CloudTrail trail. /*! \param trailName: The name of the CloudTrail trail. \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::CloudTrail::deleteTrail(const Aws::String trailName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudTrail::CloudTrailClient trailClient(clientConfig); Aws::CloudTrail::Model::DeleteTrailRequest request; request.SetName(trailName); auto outcome = trailClient.DeleteTrail(request); if (outcome.IsSuccess()) { std::cout << "Successfully deleted trail " << trailName << std::endl; } else { std::cerr << "Error deleting trail " << trailName << " " << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see DeleteTrail in AWS SDK for C++ API Reference.

The following code example shows how to use DescribeTrail.

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 describes the AWS CloudTrail trails in an account. /*! \param clientConfig: Aws client configuration. \return bool: Function succeeded. */ bool AwsDoc::CloudTrail::describeTrails( const Aws::Client::ClientConfiguration &clientConfig) { Aws::CloudTrail::CloudTrailClient cloudTrailClient(clientConfig); Aws::CloudTrail::Model::DescribeTrailsRequest request; auto outcome = cloudTrailClient.DescribeTrails(request); if (outcome.IsSuccess()) { const Aws::Vector<Aws::CloudTrail::Model::Trail> &trails = outcome.GetResult().GetTrailList(); std::cout << trails.size() << " trail(s) found." << std::endl; for (const Aws::CloudTrail::Model::Trail &trail: trails) { std::cout << trail.GetName() << std::endl; } } else { std::cerr << "Failed to describe trails." << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see DescribeTrail in AWS SDK for C++ API Reference.

The following code example shows how to use LookupEvents.

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.