Export log data to Amazon S3 using the AWS CLI - Amazon CloudWatch Logs

Export log data to Amazon S3 using the AWS CLI

In the following example, you use an export task to export all data from a CloudWatch Logs log group named my-log-group to an Amazon S3 bucket named my-exported-logs. This example assumes that you have already created a log group called my-log-group.

Exporting log data to S3 buckets that are encrypted by AWS KMS is supported.

Step 1: Create an S3 bucket

We recommend that you use a bucket that was created specifically for CloudWatch Logs. However, if you want to use an existing bucket, you can skip to step 2.

Note

The S3 bucket must reside in the same Region as the log data to export. CloudWatch Logs doesn't support exporting data to S3 buckets in a different Region.

To create an S3 bucket using the AWS CLI

At a command prompt, run the following create-bucket command, where LocationConstraint is the Region where you are exporting log data.

aws s3api create-bucket --bucket my-exported-logs --create-bucket-configuration LocationConstraint=us-east-2

The following is example output.

{ "Location": "/my-exported-logs" }

Step 2: Create an IAM user with full access to Amazon S3 and CloudWatch Logs

In the following steps, you create the IAM user with necessary permissions.

To create the user and assign permissions

  1. Create the IAM user by entering the following command.

    aws iam create-user --user-name CWLExportUser
  2. Attach the IAM managed policies to the IAM user that you just created.

    export S3POLICYARN=$(aws iam list-policies --query 'Policies[?PolicyName==`AmazonS3FullAccess`].{ARN:Arn}' --output text)
    export CWLPOLICYARN=$( aws iam list-policies --query 'Policies[?PolicyName==`CloudWatchLogsFullAccess`].{ARN:Arn}' --output text)
    aws iam attach-user-policy --user-name CWLExportUser --policy-arn $S3POLICYARN
    aws iam attach-user-policy --user-name CWLExportUser --policy-arn $CWLPOLICYARN
  3. Confirm that the two managed policies are attached.

    aws iam list-attached-user-policies --user-name CWLExportUser
  4. Configure your AWS CLI to include the IAM credentials of the CWLExportUser IAM user. For more information, see Configuring the AWS CLI.

Step 3: Set permissions on an S3 bucket

By default, all S3 buckets and objects are private. Only the resource owner, the account that created the bucket, can access the bucket and any objects that it contains. However, the resource owner can choose to grant access permissions to other resources and users by writing an access policy.

Important

To make exports to S3 buckets more secure, we now require you to specify the list of source accounts that are allowed to export log data to your S3 bucket. In the example below, the list of these account IDs is specified in the aws:SourceAccount key.

We recommend that you also include the account ID of the account where the S3 bucket is created, to allow export within the same account.

To set permissions on an S3 bucket

  1. Create a file named policy.json and add the following access policy, changing Resource to the name of your S3 bucket and Principal to the endpoint of the Region where you are exporting log data. Use a text editor to create this policy file. Don't use the IAM console.

    { "Version": "2012-10-17", "Statement": [ { "Action": "s3:GetBucketAcl", "Effect": "Allow", "Resource": "arn:aws:s3:::my-exported-logs", "Principal": { "Service": "logs.us-east-2.amazonaws.com" } }, { "Action": "s3:PutObject" , "Effect": "Allow", "Resource": "arn:aws:s3:::my-exported-logs/*", "Condition": { "StringEquals": { "s3:x-amz-acl": "bucket-owner-full-control", "aws:SourceAccount": [ "AccountId1", "AccountId2", ... ] } }, "Principal": { "Service": "logs.us-east-2.amazonaws.com" } } ] }
  2. Set the policy that you just added as the access policy on your bucket by using the put-bucket-policy command. This policy enables CloudWatch Logs to export log data to your S3 bucket. The bucket owner will have full permissions on all of the exported objects.

    aws s3api put-bucket-policy --bucket my-exported-logs --policy file://policy.json
    Warning

    If the existing bucket already has one or more policies attached to it, add the statements for CloudWatch Logs access to that policy or policies. We recommend that you evaluate the resulting set of permissions to be sure that they're appropriate for the users who will access the bucket.

(Optional) Step 4: Exporting to a bucket encrypted with SSE-KMS

This step is necessary only if you are exporting to an S3 bucket that uses server-side encryption with AWS KMS keys. This encryption is known as SSE-KMS.

To export to a bucket encrypted with SSE-KMS

  1. Use a text editor to create a file named key_policy.json and add the following access policy. When you add the policy, make the following changes:

    • Replace Region with the Region of your logs.

    • Replace account-ARN with the ARN of the account that owns the KMS key.

    { "Version": "2012-10-17", "Statement": [ { "Sid": "Allow CWL Service Principal usage", "Effect": "Allow", "Principal": { "Service": "logs.Region.amazonaws.com" }, "Action": [ "kms:GenerateDataKey", "kms:Decrypt" ], "Resource": "*" }, { "Sid": "Enable IAM User Permissions", "Effect": "Allow", "Principal": { "AWS": "account-ARN" }, "Action": [ "kms:GetKeyPolicy*", "kms:PutKeyPolicy*", "kms:DescribeKey*", "kms:CreateAlias*", "kms:ScheduleKeyDeletion*", "kms:Decrypt" ], "Resource": "*" } ] }
  2. Enter the following command:

    aws kms create-key --policy file://key_policy.json

    The following is example output from this command:

    { "KeyMetadata": { "AWSAccountId": "account_id", "KeyId": "key_id", "Arn": "arn:aws:kms:us-east-2:account_id:key/key_id", "CreationDate": "time", "Enabled": true, "Description": "", "KeyUsage": "ENCRYPT_DECRYPT", "KeyState": "Enabled", "Origin": "AWS_KMS", "KeyManager": "CUSTOMER", "CustomerMasterKeySpec": "SYMMETRIC_DEFAULT", "KeySpec": "SYMMETRIC_DEFAULT", "EncryptionAlgorithms": [ "SYMMETRIC_DEFAULT" ], "MultiRegion": false }
  3. Use a text editor to create a file called bucketencryption.json with the following contents.

    { "Rules": [ { "ApplyServerSideEncryptionByDefault": { "SSEAlgorithm": "aws:kms", "KMSMasterKeyID": "{KMS Key ARN}" }, "BucketKeyEnabled": true } ] }
  4. Enter the following command, replacing bucket-name with the name of the bucket that you are exporting logs to.

    aws s3api put-bucket-encryption --bucket bucket-name --server-side-encryption-configuration file://bucketencryption.json

    If the command doesn't return an error, the process is successful.

Step 5: Create an export task

After you create the export task for exporting logs from a log group, the export task might take anywhere from a few seconds to a few hours, depending on the size of the data to export.

To create an export task using the AWS CLI

At a command prompt, use the following create-export-task command to create the export task.

aws logs create-export-task --profile CWLExportUser --task-name "my-log-group-09-10-2015" --log-group-name "my-log-group" --from 1441490400000 --to 1441494000000 --destination "my-exported-logs" --destination-prefix "export-task-output"

The following is example output.

{ "taskId": "cda45419-90ea-4db5-9833-aade86253e66" }

Step 6: Describe export tasks

After you create an export task, you can get the current status of the task.

To describe export tasks using the AWS CLI

At a command prompt, use the following describe-export-tasks command.

aws logs --profile CWLExportUser describe-export-tasks --task-id "cda45419-90ea-4db5-9833-aade86253e66"

The following is example output.

{ "exportTasks": [ { "destination": "my-exported-logs", "destinationPrefix": "export-task-output", "executionInfo": { "creationTime": 1441495400000 }, "from": 1441490400000, "logGroupName": "my-log-group", "status": { "code": "RUNNING", "message": "Started Successfully" }, "taskId": "cda45419-90ea-4db5-9833-aade86253e66", "taskName": "my-log-group-09-10-2015", "tTo": 1441494000000 }] }

You can use the describe-export-tasks command in three different ways:

  • Without any filters – Lists all of your export tasks, in reverse order of creation.

  • Filter on task ID – Lists the export task, if one exists, with the specified ID.

  • Filter on task status – Lists the export tasks with the specified status.

For example, use the following command to filter on the FAILED status.

aws logs --profile CWLExportUser describe-export-tasks --status-code "FAILED"

The following is example output.

{ "exportTasks": [ { "destination": "my-exported-logs", "destinationPrefix": "export-task-output", "executionInfo": { "completionTime": 1441498600000 "creationTime": 1441495400000 }, "from": 1441490400000, "logGroupName": "my-log-group", "status": { "code": "FAILED", "message": "FAILED" }, "taskId": "cda45419-90ea-4db5-9833-aade86253e66", "taskName": "my-log-group-09-10-2015", "to": 1441494000000 }] }

Step 7: Cancel an export task

You can cancel an export task if it's in a PENDING or RUNNING state.

To cancel an export task using the AWS CLI

At a command prompt, use the following cancel-export-task command:

aws logs --profile CWLExportUser cancel-export-task --task-id "cda45419-90ea-4db5-9833-aade86253e66"

You can use the describe-export-tasks command to verify that the task was canceled successfully.