Log Amazon EC2 and Amazon EBS API calls with Amazon CloudTrail - Amazon Elastic Compute Cloud
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Log Amazon EC2 and Amazon EBS API calls with Amazon CloudTrail

Amazon EC2 and Amazon EBS are integrated with Amazon CloudTrail, a service that provides a record of actions taken by a user, role, or an Amazon service in Amazon EC2 and Amazon EBS. CloudTrail captures all API calls for Amazon EC2 and Amazon EBS as events, including calls from the console and from code calls to the APIs. If you create a trail, you can enable continuous delivery of CloudTrail events to an Amazon S3 bucket, including events for Amazon EC2 and Amazon EBS. 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 Amazon EC2 and Amazon EBS, 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 Amazon CloudTrail User Guide.

Amazon EC2 and Amazon EBS information in CloudTrail

CloudTrail is enabled on your Amazon Web Services account when you create the account. When activity occurs in Amazon EC2 and Amazon EBS, that activity is recorded in a CloudTrail event along with other Amazon Web 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 Amazon EC2 and Amazon EBS, 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 Amazon Web Services Regions. The trail logs events from all Regions in the Amazon partition and delivers the log files to the Amazon S3 bucket that you specify. Additionally, you can configure other Amazon services to further analyze and act upon the event data collected in CloudTrail logs. For more information, see:

All Amazon EC2 actions, and Amazon EBS management actions, are logged by CloudTrail and are documented in the Amazon EC2 API Reference. For example, calls to the RunInstances, DescribeInstances, or CreateImage 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 user or 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 Amazon Web Service.

For more information, see the CloudTrail userIdentity element.

Understand Amazon EC2 and Amazon EBS 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 are not an ordered stack trace of the public API calls, so they do not appear in any specific order.

The following log file record shows that a user terminated an instance.

{ "Records":[ { "eventVersion":"1.03", "userIdentity":{ "type":"Root", "principalId":"123456789012", "arn":"arn:aws:iam::123456789012:root", "accountId":"123456789012", "accessKeyId":"AKIAIOSFODNN7EXAMPLE", "userName":"user" }, "eventTime":"2016-05-20T08:27:45Z", "eventSource":"ec2.amazonaws.com", "eventName":"TerminateInstances", "awsRegion":"us-west-2", "sourceIPAddress":"198.51.100.1", "userAgent":"aws-cli/1.10.10 Python/2.7.9 Windows/7botocore/1.4.1", "requestParameters":{ "instancesSet":{ "items":[{ "instanceId":"i-1a2b3c4d" }] } }, "responseElements":{ "instancesSet":{ "items":[{ "instanceId":"i-1a2b3c4d", "currentState":{ "code":32, "name":"shutting-down" }, "previousState":{ "code":16, "name":"running" } }] } }, "requestID":"be112233-1ba5-4ae0-8e2b-1c302EXAMPLE", "eventID":"6e12345-2a4e-417c-aa78-7594fEXAMPLE", "eventType":"AwsApiCall", "recipientAccountId":"123456789012" } ] }

Use Amazon CloudTrail to audit users that connect via EC2 Instance Connect

Use Amazon CloudTrail to audit the users that connect to your instances via EC2 Instance Connect.

To audit SSH activity via EC2 Instance Connect using the Amazon CloudTrail console
  1. Open the Amazon CloudTrail console at https://console.aws.amazon.com/cloudtrail/.

  2. Verify that you are in the correct Region.

  3. In the navigation pane, choose Event history.

  4. For Filter, choose Event source, ec2-instance-connect.amazonaws.com.

  5. (Optional) For Time range, select a time range.

  6. Choose the Refresh events icon.

  7. The page displays the events that correspond to the SendSSHPublicKey API calls. Expand an event using the arrow to view additional details, such as the user name and Amazon access key that was used to make the SSH connection, and the source IP address.

  8. To display the full event information in JSON format, choose View event. The requestParameters field contains the destination instance ID, OS user name, and public key that were used to make the SSH connection.

    { "eventVersion": "1.05", "userIdentity": { "type": "IAMUser", "principalId": "ABCDEFGONGNOMOOCB6XYTQEXAMPLE", "arn": "arn:aws:iam::1234567890120:user/IAM-friendly-name", "accountId": "123456789012", "accessKeyId": "ABCDEFGUKZHNAW4OSN2AEXAMPLE", "userName": "IAM-friendly-name", "sessionContext": { "attributes": { "mfaAuthenticated": "false", "creationDate": "2018-09-21T21:37:58Z"} } }, "eventTime": "2018-09-21T21:38:00Z", "eventSource": "ec2-instance-connect.amazonaws.com", "eventName": "SendSSHPublicKey ", "awsRegion": "us-west-2", "sourceIPAddress": "123.456.789.012", "userAgent": "aws-cli/1.15.61 Python/2.7.10 Darwin/16.7.0 botocore/1.10.60", "requestParameters": { "instanceId": "i-0123456789EXAMPLE", "osUser": "ec2-user", "SSHKey": { "publicKey": "ssh-rsa ABCDEFGHIJKLMNO01234567890EXAMPLE" } }, "responseElements": null, "requestID": "1a2s3d4f-bde6-11e8-a892-f7ec64543add", "eventID": "1a2w3d4r5-a88f-4e28-b3bf-30161f75be34", "eventType": "AwsApiCall", "recipientAccountId": "0987654321" }

    If you have configured your Amazon account to collect CloudTrail events in an S3 bucket, you can download and audit the information programmatically. For more information, see Getting and viewing your CloudTrail log files in the Amazon CloudTrail User Guide.