Skip to main content

Audit log on Amazon EKS

On Amazon EKS, the Kubernetes API server sends its audit log to CloudWatch Logs. This page shows how to turn the audit log on, where to find it, and how to count the users of your platform, in CloudWatch Logs Insights or in a Grafana dashboard.

Each create, update and delete of a resource request carries the audit annotation attribution.platform.syntasso.io/last-changed-by, set to the user who made the change.

Turning on the audit log​

EKS does not send the audit log anywhere until you turn on the audit log type for your platform cluster. See Send control plane logs to CloudWatch Logs in the EKS documentation. For example, with the AWS CLI:

aws eks update-cluster-config \
--name <CLUSTER_NAME> \
--logging '{"clusterLogging":[{"types":["audit"],"enabled":true}]}'

The EKS audit policy already records writes to resource requests, so there is nothing else to set up.

In the EKS console, the same setting is under the cluster's Control plane logs. Select Manage to see that Audit is selected:

The Manage logging page of an EKS cluster, with the Audit log type selectedThe Manage logging page of an EKS cluster, with the Audit log type selected
The audit log turned on in the EKS console

Finding the logs​

EKS writes the audit log to the log group /aws/eks/<CLUSTER_NAME>/cluster, in log streams that start with kube-apiserver-audit-. Each line is one audit event in JSON, and the user is in annotations.attribution.platform.syntasso.io/last-changed-by.

One audit event in CloudWatch Logs Insights, with the user in the last-changed-by annotationOne audit event in CloudWatch Logs Insights, with the user in the last-changed-by annotation
The user in the annotations of one audit event

Setting log retention​

You can only see activity and count your platform users if their events are still in the log group. To count the users who used your platform over the last year, you need to keep a year of logs.

For that, check the log group's retention, and change it if you need to:

aws logs put-retention-policy \
--log-group-name /aws/eks/<CLUSTER_NAME>/cluster \
--retention-in-days 365

CloudWatch charges for the logs it stores, so a longer retention will cost more. Check the AWS pricing page to find more information about pricing.

Counting platform users​

With the audit log turned on, you can count platform users in CloudWatch Logs Insights or in Grafana.

Using CloudWatch​

  1. Open CloudWatch Logs Insights.
  2. Select the log group /aws/eks/<CLUSTER_NAME>/cluster.
  3. Choose the time range to count over.
  4. Run this query:
filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`)
and stage = "ResponseComplete"
and responseStatus.code < 400
and requestURI not like /dryRun/
| stats count_distinct(`annotations.attribution.platform.syntasso.io/last-changed-by`) as users

This filter keeps the changes to resource requests that succeeded. It drops the changes Kubernetes rejected and any dry runs, such as the ones kubectl diff sends, which also carry the audit annotation.

The unique users query in CloudWatch Logs Insights, returning 3 usersThe unique users query in CloudWatch Logs Insights, returning 3 users
Counting platform users in CloudWatch Logs Insights

Other useful CloudWatch queries​

How many changes each user made:

filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`)
and stage = "ResponseComplete"
and responseStatus.code < 400
and requestURI not like /dryRun/
| stats count(*) as changes by `annotations.attribution.platform.syntasso.io/last-changed-by` as user
| sort changes desc
The changes per user query in CloudWatch Logs Insights, returning one row for each userThe changes per user query in CloudWatch Logs Insights, returning one row for each user
Changes per user in CloudWatch Logs Insights

Who created, updated and deleted a request:

filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`)
and stage = "ResponseComplete"
and responseStatus.code < 400
and requestURI not like /dryRun/
and objectRef.namespace = "<NAMESPACE>"
and objectRef.name = "<REQUEST_NAME>"
| fields @timestamp, `annotations.attribution.platform.syntasso.io/last-changed-by` as user, verb, objectRef.resource as type
| sort @timestamp desc

verb is the Kubernetes API verb, which depends on how the tool sends the change. Most tools, such as kubectl apply, kubectl edit, Flux and Argo CD, send changes to an existing request as patch. Tools that send the whole request, such as kubectl replace, show as update.

The history query in CloudWatch Logs Insights, showing who created, changed and deleted my-cacheThe history query in CloudWatch Logs Insights, showing who created, changed and deleted my-cache
Who created, changed and deleted a request in CloudWatch Logs Insights

Grafana​

Grafana can run the same queries through its CloudWatch data source. The dashboard below shows the number of unique users, the number of changes, the changes each user made, and the most recent changes. To set it up:

  1. Give Grafana access to the log group. See Configure the CloudWatch data source in the Grafana documentation. Grafana needs to run Logs Insights queries on /aws/eks/<CLUSTER_NAME>/cluster.
  2. Add the CloudWatch data source in Grafana.
  3. Copy the dashboard below, replace <REGION>, <ACCOUNT_ID> and <CLUSTER_NAME>, then import it and choose your CloudWatch data source.
The SKE platform audit dashboard, showing unique users, changes, changes per user, and recent changesThe SKE platform audit dashboard, showing unique users, changes, changes per user, and recent changes
The SKE platform audit dashboard on EKS
Dashboard JSON
{
"__inputs": [
{
"name": "DS_CLOUDWATCH",
"label": "CloudWatch",
"type": "datasource",
"pluginId": "cloudwatch",
"pluginName": "CloudWatch"
}
],
"title": "SKE platform audit",
"uid": "ske-platform-audit",
"tags": [
"ske",
"audit"
],
"time": {
"from": "now-30d",
"to": "now"
},
"schemaVersion": 39,
"panels": [
{
"id": 1,
"title": "Unique users",
"type": "stat",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"gridPos": {
"x": 0,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"queryMode": "Logs",
"region": "<REGION>",
"logGroups": [
{
"arn": "arn:aws:logs:<REGION>:<ACCOUNT_ID>:log-group:/aws/eks/<CLUSTER_NAME>/cluster",
"name": "/aws/eks/<CLUSTER_NAME>/cluster"
}
],
"expression": "filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`) and stage = \"ResponseComplete\" and responseStatus.code < 400 and requestURI not like /dryRun/\n| stats count_distinct(`annotations.attribution.platform.syntasso.io/last-changed-by`) as users",
"statsGroups": [],
"id": ""
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 2,
"title": "Changes to resource requests",
"type": "stat",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"gridPos": {
"x": 8,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"queryMode": "Logs",
"region": "<REGION>",
"logGroups": [
{
"arn": "arn:aws:logs:<REGION>:<ACCOUNT_ID>:log-group:/aws/eks/<CLUSTER_NAME>/cluster",
"name": "/aws/eks/<CLUSTER_NAME>/cluster"
}
],
"expression": "filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`) and stage = \"ResponseComplete\" and responseStatus.code < 400 and requestURI not like /dryRun/\n| stats count(*) as changes",
"statsGroups": [],
"id": ""
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 3,
"title": "Average changes per user",
"type": "stat",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"gridPos": {
"x": 16,
"y": 0,
"w": 8,
"h": 5
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"queryMode": "Logs",
"region": "<REGION>",
"logGroups": [
{
"arn": "arn:aws:logs:<REGION>:<ACCOUNT_ID>:log-group:/aws/eks/<CLUSTER_NAME>/cluster",
"name": "/aws/eks/<CLUSTER_NAME>/cluster"
}
],
"expression": "filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`) and stage = \"ResponseComplete\" and responseStatus.code < 400 and requestURI not like /dryRun/\n| stats count(*) as changes by `annotations.attribution.platform.syntasso.io/last-changed-by` as user\n| stats avg(changes) as average",
"statsGroups": [],
"id": ""
}
],
"options": {
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"colorMode": "none",
"graphMode": "none"
}
},
{
"id": 4,
"title": "Changes per user",
"type": "table",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"gridPos": {
"x": 0,
"y": 5,
"w": 12,
"h": 10
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"queryMode": "Logs",
"region": "<REGION>",
"logGroups": [
{
"arn": "arn:aws:logs:<REGION>:<ACCOUNT_ID>:log-group:/aws/eks/<CLUSTER_NAME>/cluster",
"name": "/aws/eks/<CLUSTER_NAME>/cluster"
}
],
"expression": "filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`) and stage = \"ResponseComplete\" and responseStatus.code < 400 and requestURI not like /dryRun/\n| stats count(*) as changes by `annotations.attribution.platform.syntasso.io/last-changed-by` as user\n| sort changes desc",
"statsGroups": [],
"id": ""
}
]
},
{
"id": 5,
"title": "Recent changes",
"type": "table",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"gridPos": {
"x": 12,
"y": 5,
"w": 12,
"h": 10
},
"targets": [
{
"refId": "A",
"datasource": {
"type": "cloudwatch",
"uid": "${DS_CLOUDWATCH}"
},
"queryMode": "Logs",
"region": "<REGION>",
"logGroups": [
{
"arn": "arn:aws:logs:<REGION>:<ACCOUNT_ID>:log-group:/aws/eks/<CLUSTER_NAME>/cluster",
"name": "/aws/eks/<CLUSTER_NAME>/cluster"
}
],
"expression": "filter ispresent(`annotations.attribution.platform.syntasso.io/last-changed-by`) and stage = \"ResponseComplete\" and responseStatus.code < 400 and requestURI not like /dryRun/\n| fields @timestamp, `annotations.attribution.platform.syntasso.io/last-changed-by` as user, verb, objectRef.resource as type, objectRef.namespace as namespace, objectRef.name as name\n| sort @timestamp desc\n| limit 100",
"statsGroups": [],
"id": ""
}
],
"options": {
"sortBy": [
{
"displayName": "Time",
"desc": true
}
]
}
}
]
}

Known limitations​

  • Counts only go back as far as your retention.
  • The recorded user is only as specific as the way users log in. If many users share one IAM role, the audit log records the role's username for all of them, unless you set userInfoField to a field that is different for each user. Run kubectl auth whoami -o yaml to see which fields your cluster gives you. With IAM Identity Center, for example, extra.sessionName is often the user's username.
  • Some changes have no user. For example, deletes through GitOps. See What is not recorded.