iac.htora.dev · security templates

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.

How you know it worked

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

1Someone calls the cluster APIexec, get secrets, create binding2The control plane writes an audit eventwho, what, which object3It lands in the cloud's log servicekept for a year or more4Matching events raise an alertand can flow on to the SIEM
Step by step, from the thing that happens to the result you want.

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 startReadyone file, 103 lines
One file. A year's retention, three alarms, and the command that switches audit logging on.
Change these first
  • cluster_name = "your-cluster" main.tf, line 17
  • alert_email = "you@example.com" main.tf, line 18
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. terraform init
    Downloads the provider. Run it once per folder.
  3. terraform plan
    Shows what will be created. Changes nothing. Read it.
  4. terraform apply
    Shows the plan again and waits for you to type yes.
  5. terraform destroy
    When you have finished. If it refuses, How to undo it below explains why.
Download main.tf
Checks

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.

outcomes/k8s-audit-logging/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfour files, 250 lines
Adds the log under its own key, your own retention, and alarms on secrets read by people and on bursts of refused requests. It also adds an existing topic or several emails, and an optional feed into the SIEM.
Change these first
  • cluster_name. The EKS cluster whose audit log this keeps and watches.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    cluster_name = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all four files as a zip
Checks

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.

outcomes/k8s-audit-logging/aws/t1
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
Open versions.tf as plain text
HardenedReadysix files, 211 lines
Standard as a module. Plan rules refuse the key, log group, or alarms deleted, key rotation off, logs or alerts without a KMS key, silent alarms, a SIEM filter that drops events. A daily plan reports drift, and the scan fails on any unexplained finding.
Change these first
  • cluster_name. The EKS cluster whose audit log this keeps and watches.
Run it
  1. aws sts get-caller-identity
    Shows the account and role you are signed in as. Check it before every apply.
  2. # terraform.tfvars
    cluster_name = ""
    Create this file beside the others and fill in your values. Terraform reads it on its own.
  3. terraform init
    Downloads the provider. Run it once per folder.
  4. terraform plan
    Shows what will be created. Changes nothing. Read it.
  5. terraform apply
    Shows the plan again and waits for you to type yes.
  6. terraform output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

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.

outcomes/k8s-audit-logging/aws/t2
.checkov.yml
Open .checkov.yml as plain text
guard.yml
Open guard.yml as plain text
main.tf
Open main.tf as plain text
outputs.tf
Open outputs.tf as plain text
variables.tf
Open variables.tf as plain text
versions.tf
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.

What catches people

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.

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.

Registry 0.23.0. Built 2026-11-17.

Made by Habibullah Tora. Source on GitHub. Code under the MIT licence, writing under CC BY 4.0.