Sub topic 14
Logging sessions to CloudWatch Logs, S3, and CloudTrail
This chapter assumes you can already start a Session Manager session, that your instance has an instance profile, and that you know basic IAM policy syntax. It covers how to record who connected and what they did.
Two kinds of records
Session Manager produces two separate records. They answer different questions.
| Record | Answers | Where it goes | Setup |
|---|---|---|---|
| API events | Who started a session, on which instance, and when | CloudTrail | On by default |
| Session data | Which commands were typed and what output appeared | CloudWatch Logs or S3 | You enable it in Session Manager preferences |
CloudTrail never contains command text. If you need to know what a user ran, you must enable session data logging.
Finding who started a session
CloudTrail records StartSession and TerminateSession as management events. Query recent ones from the CLI:
aws cloudtrail lookup-events \
--lookup-attributes AttributeKey=EventName,AttributeValue=StartSession \
--max-results 5Each event shows the IAM identity, source IP, and the target instance ID. For longer retention or searching, create a trail that delivers to S3.
Enabling session data logging
Open Systems Manager, then Session Manager, then the Preferences tab. Choose Edit and configure one or both destinations:
- CloudWatch Logs: pick an existing log group, such as
/ssm/sessions. Output is streamed while the session runs, so you can watch or alert on it. - S3: pick an existing bucket and an optional prefix. The log is uploaded when the session ends, so a session that is still open has no object yet.
Create the log group and bucket first. Session Manager does not create them for you.
The settings are saved in the SSM-SessionManagerRunShell document. Sessions started with a different document, such as AWS-StartPortForwardingSession, do not use these preferences.
Letting the instance write the logs
The SSM Agent on the instance writes the logs, not the user. Add permissions to the instance profile role, next to AmazonSSMManagedInstanceCore:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "logs:DescribeLogGroups",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:PutLogEvents",
"logs:DescribeLogStreams"
],
"Resource": "arn:aws:logs:us-east-1:111122223333:log-group:/ssm/sessions:*"
},
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::example-session-logs/ssm/*"
},
{
"Effect": "Allow",
"Action": "s3:GetEncryptionConfiguration",
"Resource": "arn:aws:s3:::example-session-logs"
}
]
}Replace the account ID, log group, and bucket name with your own. If the role lacks these permissions, sessions can fail to start or end without logs, depending on your preferences. Check the role first when logs are missing.
Read the streamed output with the CLI:
aws logs tail /ssm/sessions --followLimits to know
- Port forwarding and SSH-over-SSM sessions are not logged as session data. SSH encrypts the payload inside the tunnel, so the agent cannot read it. CloudTrail still records that the session started.
- Anyone who can edit the preferences can turn logging off. Limit
ssm:UpdateDocumentand related permissions to administrators. - Logged output can contain secrets typed or printed in the shell. Encrypt the log group with a KMS key, enable default encryption on the bucket, and limit read access.
Exercise
A user ran aws ssm start-session --target i-0123456789abcdef0 yesterday. CloudTrail shows the StartSession event, but your CloudWatch log group has no stream for it. Name two likely causes.
Show answer
Session data logging may not be enabled in the Session Manager preferences, or the instance profile role may lack permissions such as logs:CreateLogStream and logs:PutLogEvents on the log group. The log group may also not exist, since Session Manager does not create it. CloudTrail records the API call regardless of these settings.
Exercise
Why will a session started with AWS-StartSSHSession never show its commands in CloudWatch Logs?
Show answer
SSH encrypts the data inside the tunnel, so the SSM Agent cannot read the commands. Only the CloudTrail event for the session is available.