Logging Amazon Athena API calls with AWS CloudTrail - Amazon Athena

Logging Amazon Athena API calls with AWS CloudTrail

Athena is integrated with AWS CloudTrail, a service that provides a record of actions taken by a user, role, or an AWS service in Athena.

CloudTrail captures all API calls for Athena as events. The calls captured include calls from the Athena console and code calls to the Athena API operations. If you create a trail, you can enable continuous delivery of CloudTrail events to an Amazon S3 bucket, including events for Athena. If you don't configure a trail, you can still view the most recent events in the CloudTrail console in Event history.

Using the information collected by CloudTrail, you can determine the request that was made to Athena, the IP address from which the request was made, who made the request, when it was made, and additional details.

To learn more about CloudTrail, see the AWS CloudTrail User Guide.

You can use Athena to query CloudTrail log files from Athena itself and from other AWS services. For more information, see Querying AWS CloudTrail logs, Hive JSON SerDe, and the AWS Big Data Blog post Use CTAS statements with Amazon Athena to reduce cost and improve performance, which uses CloudTrail to provide insight into Athena usage.

Athena information in CloudTrail

CloudTrail is enabled on your Amazon Web Services account when you create the account. When activity occurs in Athena, that activity is recorded in a CloudTrail event along with other AWS service events in Event history. You can view, search, and download recent events in your Amazon Web Services account. For more information, see Viewing events with CloudTrail event history.

For an ongoing record of events in your Amazon Web Services account, including events for Athena, create a trail. A trail enables CloudTrail to deliver log files to an Amazon S3 bucket. By default, when you create a trail in the console, the trail applies to all AWS Regions. The trail logs events from all Regions in the AWS partition and delivers the log files to the Amazon S3 bucket that you specify. Additionally, you can configure other AWS services to further analyze and act upon the event data collected in CloudTrail logs. For more information, see the following:

All Athena actions are logged by CloudTrail and are documented in the Amazon Athena API Reference. For example, calls to the StartQueryExecution and GetQueryResults actions generate entries in the CloudTrail log files.

Every event or log entry contains information about who generated the request. The identity information helps you determine the following:

  • Whether the request was made with root or AWS Identity and Access Management (IAM) user credentials.

  • Whether the request was made with temporary security credentials for a role or federated user.

  • Whether the request was made by another AWS service.

For more information, see the CloudTrail userIdentity element.

Understanding Athena log file entries

A trail is a configuration that enables delivery of events as log files to an Amazon S3 bucket that you specify. CloudTrail log files contain one or more log entries. An event represents a single request from any source and includes information about the requested action, the date and time of the action, request parameters, and so on. CloudTrail log files aren't an ordered stack trace of the public API calls, so they don't appear in any specific order.


To prevent unintended disclosure of sensitive information, the queryString entry in both the StartQueryExecution and CreateNamedQuery logs has a value of ***OMITTED***. This is by design. To access the actual query string, you can use the Athena GetQueryExecution API and pass in the value of responseElements.queryExecutionId from the CloudTrail log.

The following examples demonstrate CloudTrail log entries for:

StartQueryExecution (successful)

{ "eventVersion":"1.05", "userIdentity":{ "type":"IAMUser", "principalId":"EXAMPLE_PRINCIPAL_ID", "arn":"arn:aws:iam::123456789012:user/johndoe", "accountId":"123456789012", "accessKeyId":"EXAMPLE_KEY_ID", "userName":"johndoe" }, "eventTime":"2017-05-04T00:23:55Z", "eventSource":"athena.amazonaws.com", "eventName":"StartQueryExecution", "awsRegion":"us-east-1", "sourceIPAddress":"77.88.999.69", "userAgent":"aws-internal/3", "requestParameters":{ "clientRequestToken":"16bc6e70-f972-4260-b18a-db1b623cb35c", "resultConfiguration":{ "outputLocation":"s3://DOC-EXAMPLE-BUCKET/test/" }, "queryString":"***OMITTED***" }, "responseElements":{ "queryExecutionId":"b621c254-74e0-48e3-9630-78ed857782f9" }, "requestID":"f5039b01-305f-11e7-b146-c3fc56a7dc7a", "eventID":"c97cf8c8-6112-467a-8777-53bb38f83fd5", "eventType":"AwsApiCall", "recipientAccountId":"123456789012" }

StartQueryExecution (failed)

{ "eventVersion":"1.05", "userIdentity":{ "type":"IAMUser", "principalId":"EXAMPLE_PRINCIPAL_ID", "arn":"arn:aws:iam::123456789012:user/johndoe", "accountId":"123456789012", "accessKeyId":"EXAMPLE_KEY_ID", "userName":"johndoe" }, "eventTime":"2017-05-04T00:21:57Z", "eventSource":"athena.amazonaws.com", "eventName":"StartQueryExecution", "awsRegion":"us-east-1", "sourceIPAddress":"77.88.999.69", "userAgent":"aws-internal/3", "errorCode":"InvalidRequestException", "errorMessage":"Invalid result configuration. Should specify either output location or result configuration", "requestParameters":{ "clientRequestToken":"ca0e965f-d6d8-4277-8257-814a57f57446", "queryString":"***OMITTED***" }, "responseElements":null, "requestID":"aefbc057-305f-11e7-9f39-bbc56d5d161e", "eventID":"6e1fc69b-d076-477e-8dec-024ee51488c4", "eventType":"AwsApiCall", "recipientAccountId":"123456789012" }


{ "eventVersion":"1.05", "userIdentity":{ "type":"IAMUser", "principalId":"EXAMPLE_PRINCIPAL_ID", "arn":"arn:aws:iam::123456789012:user/johndoe", "accountId":"123456789012", "accessKeyId":"EXAMPLE_KEY_ID", "userName":"johndoe" }, "eventTime":"2017-05-16T22:00:58Z", "eventSource":"athena.amazonaws.com", "eventName":"CreateNamedQuery", "awsRegion":"us-west-2", "sourceIPAddress":"77.88.999.69", "userAgent":"aws-cli/1.11.85 Python/2.7.10 Darwin/16.6.0 botocore/1.5.48", "requestParameters":{ "name":"johndoetest", "queryString":"***OMITTED***", "database":"default", "clientRequestToken":"fc1ad880-69ee-4df0-bb0f-1770d9a539b1" }, "responseElements":{ "namedQueryId":"cdd0fe29-4787-4263-9188-a9c8db29f2d6" }, "requestID":"2487dd96-3a83-11e7-8f67-c9de5ac76512", "eventID":"15e3d3b5-6c3b-4c7c-bc0b-36a8dd95227b", "eventType":"AwsApiCall", "recipientAccountId":"123456789012" },