iac.htora.dev · security templates

Home/Templates/Audit logs nobody can delete

Audit logs nobody can delete

Send cloud audit logs to storage that refuses deletes for a set number of days.

Terraform

Why bother

An audit log only helps if it is still there after an incident. In a normal setup anyone with admin rights can delete it, and that is often the first thing someone does to cover their tracks. This sends the logs to storage with a retention lock, so deletes fail for everyone, including the account owner.

How you know it worked

Try to delete a stored log file. The delete should fail. The exact commands differ by platform, and each tab below spells them out.

How it works

1Something changes in the accountevery API call is recorded2The cloud's audit serviceCloudTrail, Activity Log, Cloud Audit Logs, or Audit3A delivery pipethe trail, a diagnostic setting, a sink, or a connector4Locked storagedeletes refused until the retention period ends
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. CloudTrail writing to S3 with Object Lock turned on

What you need first

  • An account where you can create S3 buckets, KMS keys, and CloudTrail trails.
  • For the organization option, run it in the management account with CloudTrail trusted access already on.

What it creates

  • A bucket with Object Lock, versioning, and public access blocked
  • A trail covering every region, with log file validation on
  • A KMS key with yearly rotation (Standard only)
  • A CloudWatch log group so metric alarms have something to read (Standard, optional)

The code

Quick startReadyone file, 234 lines
One file. Bucket, one-day lock, policy, trail. Encryption is AWS-managed. Built to try in a scratch account and remove the next day.
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 output verify
    Prints the commands that prove it worked.
  6. 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: 28 checks passed, and seven rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/audit-log-immutable/aws/t0
main.tf
Open main.tf as plain text
StandardReadyfive files, 493 lines
Adds your own KMS key, the organization trail switch, CloudWatch delivery, lifecycle to cold storage, and a deny on anyone changing the lock.
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 output verify
    Prints the commands that prove it worked.
Download all five files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 73 checks passed, and nine rules flagged, each with a written reason:

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/audit-log-immutable/aws/t1
kms.tf
Open kms.tf 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
HardenedReadysix files, 272 lines
Standard as a module. Plan rules refuse the bucket, key, or trail deleted, Object Lock off, retention under 90 days, public access allowed, and encryption other than KMS. They also refuse the trail losing multi-region, global events, file validation, or logging. A daily plan reports drift, and the scan fails on any unexplained finding.
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 output verify
    Prints the commands that prove it worked.
Download all six files as a zip
Checks

Formatting passedValid passedLint passed

Security scan: 64 checks passed, and none flagged.

Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.

outcomes/audit-log-immutable/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

Wait about fifteen minutes for the first logs, then run terraform output verify. It prints two commands. The first finds a stored log file and its version ID. The second tries to delete that version, and must fail with AccessDenied.

What catches people

Object Lock can only be switched on at the moment a bucket is created. You cannot add it to the log bucket you already have. Moving to this means a new bucket and a copy, which is worth knowing before you plan the work.

A plain delete-object on this bucket reports success. The bucket is versioned, so that call only adds a delete marker, and the locked file is still there underneath it. The lock protects each stored version, so the only honest test deletes a version by its ID.

How to undo it

The quick start locks for one day, so after that terraform destroy empties and removes the bucket. The Standard version locks for 400 days. In GOVERNANCE mode a principal with s3:BypassGovernanceRetention can delete sooner. In COMPLIANCE mode nobody can, and you wait it out.

What it costs

Storage for the logs, plus about a dollar a month for the KMS key. The first management trail in an account carries no CloudTrail charge.

Good to know

  • The copy is only as complete as what the cloud logs in the first place. Every cloud records configuration changes by default. Reads of your data are usually off by default, and you switch them on per service. Decide which ones you need before an incident. A read that was never logged cannot be recovered afterwards.
  • Keep the account or project containing the copy separate from the ones it watches, when you can. An administrator of the watched account then has no path to the copy at all.

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.