Home/Templates/A record of every action against the cluster API
A record of every action against the cluster API
Cluster audit logs sent where responders can search them, so exec into a pod or a new role binding is never invisible.
Terraform
Why bother
Most Kubernetes attacks are API calls: an exec into a running pod, a secret read, a new cluster role binding, a pod created with host access. The cluster audit log records each one with who asked. Managed clusters keep it off, or keep it for a short time, until you say otherwise.
Run kubectl exec into a test pod. An alert fires within minutes, and the audit entry behind it names the user who made the call.
How it works
Set it up
Pick your platform, then open the level you want. Each level is complete on its own. Read the comments in the files as you go. Anything with a real consequence is explained on the line where it happens.
Terraform. EKS control plane audit logs, kept and turned into alarms
What you need first
- An EKS cluster whose logging settings you can change.
- An email address for the alarms, or for Standard an existing SNS topic.
- For the Standard SIEM feed, a Firehose stream such as the SIEM ingest template builds.
What it creates
- The cluster's audit log group, kept a year in the quick start, or for your own period under its own key (Standard)
- Metric filters and alarms for exec or attach into a pod, new cluster role bindings, and anonymous requests
- Two more: secrets read by people, and bursts of refused requests (Standard)
- A subscription that sends every audit event to your SIEM's Firehose stream (Standard, optional)
See the five alerts this creates
The code
Quick startReady
cluster_name = "your-cluster"main.tf, line 17alert_email = "you@example.com"main.tf, line 18
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: four checks passed, and two rules flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
cluster_name. The EKS cluster whose audit log this keeps and watches.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars cluster_name = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 39 checks passed, and three rules flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
cluster_name. The EKS cluster whose audit log this keeps and watches.
aws sts get-caller-identity
Shows the account and role you are signed in as. Check it before every apply.# terraform.tfvars cluster_name = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 36 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Switch logging on with the command terraform output switch_on prints. Run kubectl exec into a test pod, and the pod-exec alarm fires within about ten minutes.
EKS writes nothing until audit logging is switched on for the cluster. This template prepares the log group and the detections, and prints the command that switches logging on.
If logging was already on, EKS already created the log group, and apply fails because it exists. Import it first with terraform import.
Newer kubectl clients open exec as a get over a WebSocket, so a rule that matches only create misses them. These match the exec subresource whatever the verb.
Audit events can include request bodies. Standard encrypts the log under its own key, and access to the log group deserves the same care as the cluster itself.
CloudWatch alarms cannot publish to an SNS topic encrypted with the AWS managed key, and nothing reports the failure. Standard creates its topic with a key whose policy allows them. A topic you pass in needs the same.
How to undo it
terraform destroy removes the detections and the log group, and the log with it. To stop logging too, run the switch_on command with enabled set to false.
What it costs
CloudWatch charges per gigabyte ingested and stored, and the volume grows with cluster activity. Check the first week's ingestion.
Terraform. AKS audit logs in Log Analytics, turned into alerts
What you need first
- An AKS cluster you can add diagnostic settings to, with Microsoft Entra ID integration so that user names in the log are people.
- Rights to create a Log Analytics workspace, an action group, and alert rules in the cluster's resource group, and role assignments on the workspace. Standard also locks them, which needs Owner or User Access Administrator.
- An email address for the alerts. For the Standard SIEM feed, an event hub such as the SIEM ingest template builds.
What it creates
- A Log Analytics workspace, kept a year in the quick start, or for your own period with no daily cap (Standard)
- A diagnostic setting sending the kube-audit category to it, in resource-specific mode, so the events land in the AKSAudit table with the verb, user, and object as columns
- Alert rules for exec or attach into a pod, new cluster role bindings, and anonymous requests, each checked every five minutes
- Two more alerts: secrets read by people, and bursts of refused requests from one user (Standard)
- An identity for each rule, with the Log Analytics Reader role on the workspace (Standard)
- A second diagnostic setting sending the same events to your SIEM's event hub (Standard, optional)
- Three native activity log alerts, which Azure evaluates without the workspace: a diagnostic setting deleted, a diagnostic setting written, and a log search rule disabled by Azure
- Cannot-delete locks on the workspace, the action group, and every alert, and native alerts on a lock deleted and on the rules' role assignments deleted (Standard)
See the 10 alerts this creates
The code
Quick startReady
subscription_id = "00000000-0000-0000-0000-000000000000"main.tf, line 26cluster_name = "aks-prod"main.tf, line 27cluster_resource_group = "rg-aks-prod"main.tf, line 28alert_email = "you@example.com"main.tf, line 29
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid not runLint passed
Security scan: zero checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6.
Open main.tf as plain text
StandardReady
subscription_id. Subscription that contains the cluster. The workspace and the alerts go in the cluster's resource group.cluster_name. Name of the AKS cluster whose audit log this keeps.cluster_resource_group. The cluster's own resource group. The workspace, action group, and alerts go here, each with a cannot-delete lock, so the group cannot be deleted while they are in it.alert_emails. Addresses that receive the alerts.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" cluster_name = "" cluster_resource_group = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid not runLint passed
Security scan: zero checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
subscription_id. Subscription that contains the cluster. The workspace and the alerts go in the cluster's resource group.cluster_name. Name of the AKS cluster whose audit log this keeps.cluster_resource_group. The cluster's own resource group. The workspace, action group, and alerts go here, each with a cannot-delete lock, so the group cannot be deleted while they are in it.alert_emails. Addresses that receive the alerts.
az login
Signs the command line tool in. Terraform uses the same sign-in.export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
Tells the provider which subscription. It stopped guessing in version 4.# terraform.tfvars subscription_id = "" cluster_name = "" cluster_resource_group = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid not runLint passed
Security scan: zero checks passed, and none flagged.
Last run 2026-11-09 with OpenTofu v1.12.6.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Wait up to 90 minutes after the first apply, while the diagnostic setting starts sending. Then run kubectl exec into a test pod, and the pod-exec alert emails once the entry reaches the workspace. In the workspace, AKSAudit | summarize count() by Verb shows the log filling.
The full category, kube-audit, is the one used, and it is the costly one on a busy cluster. The admin-only category drops every get and list, and newer kubectl clients open exec as a get over a WebSocket, so a log without gets can miss an exec. Standard lets you choose, and says what the cheaper choice loses.
The AKSAudit table does not exist until the first event arrives, and Azure checks an alert's query against the workspace when the rule is made. Both levels skip that check, so the rules can exist before the log does. Azure disables a rule whose query fails for a week, and the rule-disabled alert reports that without the workspace. Check that the table is filling within the first week.
A workspace daily cap drops events once it is reached, with no alert. Standard leaves it off. An attacker who can make noise should not be able to make the log stop.
An alert rule without an identity reads the workspace as whoever last saved it, and stops when that person's access does. Standard gives each rule an identity and the Log Analytics Reader role.
A customer-managed key on the workspace needs a dedicated cluster with a 100 GB a day commitment, so neither level sets one. The events are encrypted with Microsoft's keys; put yours on the event hub or storage you copy them to.
Nothing before the diagnostic setting existed is kept. Apply this before the incident.
Every alert, the native ones included, emails through one action group. In the quick start, deleting or disabling that group, or deleting the resource group, silences them all. Standard's locks stop the deletes, but whoever can edit the group can still disable it or change its addresses.
Standard locks the alert path alone, because workloads share the cluster's resource group. Any locked resource stops that resource group being deleted, and the workspace lock also stops the rules' role assignments there being deleted.
Standard's locks block Terraform too. A change that replaces or removes a locked resource or a rule's role assignment fails while the locks are on, and so does turning full_audit off. Remove the locks with terraform destroy -target=azurerm_management_lock.alert_path, then apply, and they return. Their removal fires the lock-deleted alert.
A cannot-delete lock does not stop a workspace purge. Nothing here alerts on one, so keep the Data Purger role from anyone who does not need it.
The rule-access alert matches the rules' role assignments by ID. Microsoft documents a role assignment's activity log entry under its full ID, scope included. That an assignment on a workspace is recorded the same way is inferred; the Standard verify output tests it.
The diagnostic-setting-written alert fires on every diagnostic setting created or changed in the subscription, policy remediation included, because a rewrite can drop the audit category or redirect the log without a delete.
A diagnostic setting can take up to 90 minutes to start sending after it is made, so run the drills after that.
How to undo it
In Standard, remove the locks first with terraform destroy -target=azurerm_management_lock.alert_path, or module.standard.azurerm_management_lock.alert_path in Hardened. Then terraform destroy removes the diagnostic settings, the alerts, and the workspace, and the log with it. The cluster writes no audit log again until another setting asks.
What it costs
Log Analytics charges per gigabyte ingested, with 31 days of retention included and longer retention billed per gigabyte per month. The kube-audit category on a busy cluster runs to gigabytes a day: check the first week. Each alert rule is billed per time series at its frequency. The activity log alerts are free.
Terraform. GKE audit logs turned into alerts, with reads recorded where they matter
What you need first
- A project with GKE clusters, and rights to create log-based metrics and alert policies.
- An email address for the alerts.
- For the Standard SIEM route, a Pub/Sub topic such as the SIEM ingest template builds.
What it creates
- Log-based metrics and alert policies for exec or attach into a pod, new cluster role bindings, and anonymous requests
- An email notification channel
- Data Access logging on the Kubernetes Engine API, so reads of secrets are recorded (Standard)
- Two more alerts: secrets read by people, and bursts of refused requests (Standard)
- A sink routing every cluster audit entry to your SIEM's topic (Standard, optional)
See the five alerts this creates
The code
Quick startReady
project_id = "your-project-id"main.tf, line 17alert_email = "you@example.com"main.tf, line 18
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: zero checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
StandardReady
project_id. Project containing the GKE clusters.alert_emails. Addresses that receive the alerts.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: two checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
project_id. Project containing the GKE clusters.alert_emails. Addresses that receive the alerts.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: two checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Run kubectl exec into a test pod. The pod-exec alert opens an incident within a few minutes, and its documentation contains the Logs Explorer query that finds the entry.
GKE records changes by default and keeps them 400 days. Reads, including every read of a secret, are Data Access logs, off until you switch them on.
Newer kubectl clients open exec as a get over a WebSocket. These alerts match both the create and the get method names.
The Standard audit setting owns the Kubernetes Engine API's audit configuration for the whole project. If another tool manages it, merge the two first.
Data Access logs on a busy cluster add log volume, and Cloud Logging charges for ingestion beyond the free allowance. Check the first week's volume.
How to undo it
terraform destroy removes the metrics, alerts, sink, and read logging. The admin activity log stays, because GKE writes it regardless.
What it costs
The alerts cost next to nothing. Data Access logs are billed by volume beyond the free allowance.
Terraform. OKE API server events in the Audit service, turned into alarms
What you need first
- An OKE cluster. Its API server already sends administrative calls to the Audit service, under the source OKE API Server Admin Access, with nothing to switch on.
- Rights to create connectors, alarms, a notification topic, and policies in the cluster's compartment, and for Standard a bucket.
- An email address for the alarms. For the Standard SIEM feed, a stream such as the SIEM ingest template builds.
What it creates
- A notification topic with your addresses subscribed
- Four connectors that count matching audit events into three metrics, with the caller as a dimension: exec into a pod, attach to one, a new cluster role binding, and refused requests
- Three alarms on those metrics, the last firing on ten refused requests from one caller in five minutes (Standard: one notification per caller)
- A bucket with a retention rule, under a key of your own, and a connector copying every OKE API server event into it, kept past the Audit service's year (Standard)
- A connector sending the same events to your SIEM's stream (Standard, optional)
See the three alerts this creates
The code
Quick startReady
compartment_ocid = "ocid1.compartment.oc1..aaaaaaaa"main.tf, line 24region = "us-ashburn-1"main.tf, line 25alert_email = "you@example.com"main.tf, line 26
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: one check passed, and none flagged.
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
compartment_ocid. The cluster's compartment. Its audit events are there, and so go the connectors, the alarms, and the archive.region. Region identifier of the cluster, such as us-ashburn-1.alert_emails. Addresses that receive the alarms.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars compartment_ocid = "" region = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: three checks passed, and two rules flagged, each with a written reason:
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open keys.tf as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
HardenedReady
compartment_ocid. The cluster's compartment. Its audit events are there, and so go the connectors, the alarms, and the archive.region. Region identifier of the cluster, such as us-ashburn-1.alert_emails. Addresses that receive the alarms.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars compartment_ocid = "" region = "" alert_emails = ""
Create this file beside the others and fill in your values. Terraform reads it on its own.terraform init
Downloads the provider. Run it once per folder.terraform plan
Shows what will be created. Changes nothing. Read it.terraform apply
Shows the plan again and waits for you to type yes.terraform output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: three checks passed, and none flagged.
Last run 2026-11-12 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open .checkov.yml as plain text
Open guard.yml as plain text
Open main.tf as plain text
Open outputs.tf as plain text
Open variables.tf as plain text
Open versions.tf as plain text
How to check it worked
Confirm the subscription email first. Then run kubectl exec into a test pod, and the pod-exec alarm emails within a few minutes. In Logging Search, search "<compartment>/_Audit" | where source='OKE API Server Admin Access' | summarize count() by data.eventName lists what the API server recorded. The exec, and a test secret read, are either in that list or not.
Oracle describes the feed as administrative changes, the writes, and does not say whether reads appear. Nothing in OKE adds them if they are absent, so a person reading a secret may leave no record. The verify query settles it for your cluster. Keep secrets out of etcd with the secrets-backend outcome and keep the right to read them to a few names.
Whether an exec appears is not stated in Oracle's documentation. The verify query lists what the API server recorded in the last hour. If a test exec is missing, the pod-exec alarm cannot see it, and the Kubernetes for defenders page says what to do instead.
The Audit service keeps 365 days, tenancy-wide, and the period cannot be changed in either direction. Standard's bucket is for longer, under a retention rule.
Alarms read metrics only, so each detection is a connector counting matching events into a metric first, with the caller's OCID as a dimension. Oracle documents a small filter language for connectors: search, where, and, or, equality, and a wildcard inside a value. It documents no parentheses and no order for mixing and with or. The detections are written within what is documented, and exec and attach are two connectors feeding one metric.
The burst alarm counts points in the metric. Whether a connector posts one point per matching event or one per batch is not stated, so test it with ten refused requests and read the metric; Standard's verify output says how.
Audit events arrive in the Audit service minutes after the call, and a connector adds its own delay, so an alarm follows an exec by a few minutes.
The archive's retention rule refuses deletion while it stands, and until it is locked whoever can manage the bucket can remove the rule. Set lock_at, at least 14 days ahead, and after that time nobody can.
Deleting the notification topic or the connectors' policy stops every alarm, and none of them can report it. Deleting, deactivating, or moving a connector out of the compartment silences its own alarm. The daily drift plan in Hardened is what notices.
How to undo it
terraform destroy removes the connectors, the alarms, the topic, and the policies. The Audit service keeps recording. The archive bucket refuses destruction while it contains objects under retention.
What it costs
Nothing for the Audit service, connectors, alarms, or the first thousand emails a month. The archive bucket is billed by what it stores, and audit events are small; its HSM key is billed per key version per month.
Good to know
- An audit log answers who did it only if it was kept. EKS keeps nothing until logging is switched on, and AKS writes nothing until a diagnostic setting asks. GKE records reads only when Data Access logs are on, and OKE records the writes and never the reads.
- The detections follow the attack paths on the Kubernetes for defenders page: exec into a pod, new cluster role bindings, anonymous calls, secrets read by people, and bursts of refused requests.