Log Amazon EC2 API calls using AWS CloudTrail - Amazon Elastic Compute Cloud

Log Amazon EC2 API calls using AWS CloudTrail

The Amazon EC2 API is integrated with AWS CloudTrail, a service that provides a record of actions taken by a user, role, or an AWS service. CloudTrail captures all API calls for Amazon EC2 as events, including calls from the console and from code calls to the API operations. Using the information collected by CloudTrail, you can determine the request that was made to the Amazon EC2 API, the IP address from which the request was made, when it was made, and so on.

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

Amazon EC2 API information in CloudTrail

CloudTrail is enabled on your AWS 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 AWS service events in Event history. You can view, search, and download recent events in your AWS account. For more information, see Viewing events with CloudTrail Event history.

For an ongoing record of events in your AWS 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 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:

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 AWS service.

For more information, see the CloudTrail userIdentity element.

Understand Amazon EC2 API 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 AWS CloudTrail to audit connections made using EC2 Instance Connect

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

To audit SSH activity via EC2 Instance Connect using the AWS CloudTrail console
  1. Open the 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 AWS 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 AWS 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 AWS CloudTrail User Guide.