There are more AWS SDK examples available in the AWS Doc SDK Examples
CloudTrail examples using SDK for Ruby
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Ruby 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 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.
-
The following code example shows how to use DeleteTrail
.
- 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
. client.delete_trail({ name: trail_name # required }) puts "Successfully deleted trail: #{trail_name}" rescue StandardError => e puts "Got error trying to delete trail: #{trail_name}:" puts e exit 1 end
-
For API details, see DeleteTrail in AWS SDK for Ruby API Reference.
-
The following code example shows how to use ListTrails
.
- 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' def describe_trails_example(client) resp = client.describe_trails({}) puts "Found #{resp.trail_list.count} trail(s)." resp.trail_list.each do |trail| puts "Name: #{trail.name}" puts "S3 bucket name: #{trail.s3_bucket_name}" puts end
-
For API details, see ListTrails in AWS SDK for Ruby API Reference.
-
The following code example shows how to use LookupEvents
.
- 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.
-