There are more AWS SDK examples available in the AWS Doc SDK Examples
Use CreateTrail
with an AWS SDK or CLI
The following code examples show how to use CreateTrail
.
- 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 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.
-
- CLI
-
- AWS CLI
-
To create a trail
The following
create-trail
command creates a multi-region trail namedTrail1
and specifies an S3 bucket:aws cloudtrail create-trail --name
Trail1
--s3-bucket-namemy-bucket
--is-multi-region-trailOutput:
{ "IncludeGlobalServiceEvents": true, "Name": "Trail1", "TrailARN": "arn:aws:cloudtrail:us-west-2:123456789012:trail/Trail1", "LogFileValidationEnabled": false, "IsMultiRegionTrail": true, "S3BucketName": "my-bucket" }
-
For API details, see CreateTrail
in AWS CLI Command Reference.
-
- PowerShell
-
- Tools for PowerShell
-
Example 1: Creates a trail that will use the bucket 'mycloudtrailbucket' for log file storage.
New-CTTrail -Name "awscloudtrail-example" -S3BucketName "amzn-s3-demo-bucket"
Example 2: Creates a trail that will use the bucket 'mycloudtrailbucket' for log file storage. The S3 objects representing the logs will have a common key prefix of 'mylogs'. When new logs are delivered to the bucket a notification will be sent to the SNS topic 'mlog-deliverytopic'. This example using splatting to supply the parameter values to the cmdlet.
$params = @{ Name="awscloudtrail-example" S3BucketName="amzn-s3-demo-bucket" S3KeyPrefix="mylogs" SnsTopicName="mlog-deliverytopic" } New-CTTrail @params
-
For API details, see CreateTrail 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' require 'aws-sdk-s3' require 'aws-sdk-sts' def create_trail_example(s3_client, sts_client, cloudtrail_client, trail_name, bucket_name) resp = sts_client.get_caller_identity({}) account_id = resp.account # Attach policy to an Amazon Simple Storage Service (S3) bucket. s3_client.create_bucket(bucket: bucket_name) begin policy = { 'Version' => '2012-10-17', 'Statement' => [ { 'Sid' => 'AWSCloudTrailAclCheck20150319', 'Effect' => 'Allow', 'Principal' => { 'Service' => 'cloudtrail.amazonaws.com' }, 'Action' => 's3:GetBucketAcl', 'Resource' => "arn:aws:s3:::#{bucket_name}" }, { 'Sid' => 'AWSCloudTrailWrite20150319', 'Effect' => 'Allow', 'Principal' => { 'Service' => 'cloudtrail.amazonaws.com' }, 'Action' => 's3:PutObject', 'Resource' => "arn:aws:s3:::#{bucket_name}/AWSLogs/#{account_id}/*", 'Condition' => { 'StringEquals' => { 's3:x-amz-acl' => 'bucket-owner-full-control' } } } ] }.to_json s3_client.put_bucket_policy( bucket: bucket_name, policy: policy ) puts "Successfully added policy to bucket #{bucket_name}" end begin cloudtrail_client.create_trail({ name: trail_name, # required s3_bucket_name: bucket_name # required }) puts "Successfully created trail: #{trail_name}." rescue StandardError => e puts "Got error trying to create trail #{trail_name}:\n #{e}" puts e exit 1 end
-
For API details, see CreateTrail in AWS SDK for Ruby API Reference.
-