文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
CreateTrail
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 CreateTrail
。
- C++
-
- C++ 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 // 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(); }
-
如需 API 詳細資訊,請參閱 CreateTrail AWS SDK for C++ 參考中的 API。
-
- CLI
-
- AWS CLI
-
若要建立追蹤
下列
create-trail
命令會建立名為 的多區域追蹤,Trail1
並指定 S3 儲存貯體:aws cloudtrail create-trail --name
Trail1
--s3-bucket-namemy-bucket
--is-multi-region-trail輸出:
{ "IncludeGlobalServiceEvents": true, "Name": "Trail1", "TrailARN": "arn:aws:cloudtrail:us-west-2:123456789012:trail/Trail1", "LogFileValidationEnabled": false, "IsMultiRegionTrail": true, "S3BucketName": "my-bucket" }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 CreateTrail
。
-
- PowerShell
-
- for PowerShell 工具
-
範例 1:建立將使用儲存貯體 'mycloudtrailbucket' 進行日誌檔案儲存的追蹤。
New-CTTrail -Name "awscloudtrail-example" -S3BucketName "amzn-s3-demo-bucket"
範例 2:建立將使用儲存貯體 'mycloudtrailbucket' 進行日誌檔案儲存的追蹤。代表日誌的 S3 物件會有 'mylogs' 的常見金鑰字首。當新日誌交付至儲存貯體時,通知將傳送至 SNS 主題 'mlog-deliverytopic'。此範例使用 splatting 將參數值提供給 cmdlet。
$params = @{ Name="awscloudtrail-example" S3BucketName="amzn-s3-demo-bucket" S3KeyPrefix="mylogs" SnsTopicName="mlog-deliverytopic" } New-CTTrail @params
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考中的 CreateTrail。
-
- Ruby
-
- Ruby 的 SDK
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 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
-
如需 API 詳細資訊,請參閱 CreateTrail AWS SDK for Ruby 參考中的 API。
-
動作
DeleteTrail