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.
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
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 startReady
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 output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 28 checks passed, and seven rules flagged, each with a written reason:
- CloudTrail sent to CloudWatch Logs
- S3 event notifications
- S3 cross-region replication
- S3 encrypted with KMS
- S3 server access logging
- CloudTrail SNS topic
- CloudTrail encrypted with KMS
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open main.tf as plain text
StandardReady
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 output verify
Prints the commands that prove it worked.
Formatting passedValid passedLint passed
Security scan: 73 checks passed, and nine rules flagged, each with a written reason:
- CloudTrail sent to CloudWatch Logs
- S3 event notifications
- IAM permissions management without constraints
- IAM write access without constraints
- S3 cross-region replication
- S3 server access logging
- CloudTrail SNS topic
- CloudWatch log retention of a year
- IAM resource of *
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/aws 6.66.0.
Open kms.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
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 output verify
Prints the commands that prove it worked.
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.
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 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.
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.
Terraform. Activity Log writing to a blob container with a retention policy
What you need first
- Owner or Contributor on the subscription, and az login done. The quick start reads the subscription from ARM_SUBSCRIPTION_ID, which the provider has required since version 4.
- Permission to create a diagnostic setting at subscription scope, which is separate from resource-level rights.
What it creates
- A resource group, a storage account, and the insights-activity-logs container
- A time-based immutability policy on that container
- A diagnostic setting sending eight activity log categories
- A Log Analytics workspace so KQL and alert rules have something to read (Standard, optional)
- A Key Vault with purge protection, a rotating key, and an identity the storage account uses to reach it (Standard)
The code
Quick startReady
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 output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: 11 checks passed, and six rules flagged, each with a written reason:
- Storage encrypted with your own key
- Classic blob read logging
- Storage private endpoint
- Storage soft delete
- Classic queue logging
- Storage public network access off
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.0.
Open main.tf as plain text
StandardReady
subscription_id. Subscription the resources go in, and whose activity log is copied. The provider has required it since version 4.
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 = ""
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: 18 checks passed, and nine rules flagged, each with a written reason:
- Classic blob read logging
- Key Vault private endpoint
- Storage private endpoint
- Key backed by an HSM
- Key Vault public network access off
- Classic queue logging
- Key expiration date
- Storage account naming
- Storage public network access off
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.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
subscription_id. Subscription the resources go in, and whose activity log is copied. The provider has required it since version 4.
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 = ""
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: 18 checks passed, and none flagged.
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/azurerm 5.6.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
Wait for the first hourly blob to land in insights-activity-logs, then try to delete it with az storage blob delete. It must be refused as an operation not permitted on an immutable blob.
Azure creates the container itself on first delivery, named insights-activity-logs. If you name yours anything else, the policy protects an empty container and the real logs land unprotected next to it. Both templates declare that exact name up front.
Azure Monitor appends to one blob per hour. A retention policy blocks every change to a blob, appends included, unless protected append writes are allowed. Leave that flag off and delivery stops with no error anyone is likely to see.
How to undo it
The quick start policy is unlocked and lasts one day, so terraform destroy removes everything. A locked policy cannot be shortened or removed by anyone until the period expires. The Standard version also leaves the Key Vault soft-deleted for seven days, and its name cannot be reused until then.
What it costs
Blob storage for the logs, and a small monthly charge per Key Vault key plus its operations. The Log Analytics workspace is charged per gigabyte ingested and is the part worth watching.
Terraform. A log sink writing to a Cloud Storage bucket with a retention policy
What you need first
- A project where you can create buckets and log sinks. The Standard version also switches on the Cloud KMS API, which needs permission to enable services.
- For the organization-wide option, roles/logging.configWriter on the organization.
- Application default credentials. Run gcloud auth application-default login once. A plain gcloud auth login is not what Terraform reads.
What it creates
- A bucket with a retention policy, uniform access, and public access prevention enforced
- A log sink copying every audit log entry, and a grant that lets the sink write to the bucket
- A Cloud KMS key ring and a key that rotates every 90 days (Standard)
- An organization sink covering every project, including ones created later (Standard, optional)
The code
Quick startReady
project_id = "your-project-id"main.tf, line 26
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 output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: three checks passed, and three rules flagged, each with a written reason:
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 that contains the bucket and the key. Also the source of logs when org_id is empty.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
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: nine checks passed, and two rules flagged, each with a written reason:
Last run 2026-10-02 with OpenTofu v1.12.6 against hashicorp/google 8.4.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
project_id. Project that contains the bucket and the key. Also the source of logs when org_id is empty.
gcloud auth application-default login
The sign-in Terraform reads. A plain gcloud auth login is not enough.# terraform.tfvars project_id = ""
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: nine 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
Cloud Logging writes to Cloud Storage in hourly batches, so allow up to an hour for the first file. Then run terraform output verify. It prints two commands. The first finds a stored file. The second tries to delete it, and must fail with a retention policy error.
The sink writes as its own service account, which Terraform calls its writer identity. If that identity cannot create objects in the bucket, the sink still exists, Terraform reports no error, and nothing is delivered. Both templates grant it.
A sink only copies logs that are being written. Admin Activity logs are always on. Data Access logs are off by default for almost every service, so reads of your data leave no record until you switch them on service by service.
An empty bucket ten minutes after apply is normal. Delivery to Cloud Storage happens once an hour.
How to undo it
The quick start policy is unlocked and lasts one day, so after that terraform destroy empties and removes the bucket. A locked policy can never be shortened or removed, and the bucket stays until every object has aged past it. Cloud KMS key rings and keys can never be deleted. Destroy removes them from state and destroys the key versions, and their names stay used in that project.
What it costs
Cloud Storage for the logs, moving to Archive class after 90 days in the Standard version. Cloud KMS charges a small monthly amount per active key version, plus operations. If you switch on Data Access logs, that is where the volume comes from.
Terraform. Connector Hub copying audit events to Object Storage with a retention rule
What you need first
- Rights to create IAM policies in the root compartment. Both templates add policies there, which usually means a tenancy administrator.
- The OCI command line tool set up with an API key, so ~/.oci/config exists. Terraform reads the same file.
- Your tenancy OCID. The Standard version also needs a compartment OCID and a region identifier such as us-ashburn-1.
What it creates
- A bucket with a retention rule, no public access, and versioning off
- A Connector Hub connector reading the audit log group and writing to the bucket
- An IAM policy letting that connector write to that bucket and nowhere else
- A vault, an HSM-protected key, and a policy letting Object Storage use it (Standard)
The code
Quick startReady
tenancy_ocid = "ocid1.tenancy.oc1..change-me"main.tf, line 23
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 output verify
Prints the commands that prove it worked.terraform destroy
When you have finished. If it refuses, How to undo it below explains why.
Formatting passedValid passedLint passed
Security scan: two checks passed, and three rules flagged, each with a written reason:
Last run 2026-11-09 with OpenTofu v1.12.6 against oracle/oci 9.8.0.
Open main.tf as plain text
StandardReady
tenancy_ocid. The tenancy. Audit events are read from here, and the IAM policies are created here.compartment_ocid. Compartment that contains the bucket, vault, and connector.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
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-09 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
tenancy_ocid. The tenancy. Audit events are read from here, and the IAM policies are created here.compartment_ocid. Compartment that contains the bucket, vault, and connector.region. Region identifier, such as us-ashburn-1.
oci os ns get
Prints your Object Storage namespace, which proves ~/.oci/config works.# terraform.tfvars tenancy_ocid = "" compartment_ocid = "" region = ""
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-09 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
The connector writes a file every few minutes. Then run terraform output verify. It prints two commands. The first names a stored file. The second tries to delete it, and must be refused because of the retention rule.
A retention rule cannot lock straight away. The lock date has to be at least 14 days ahead, and until then the rule can still be changed or removed. The window is deliberate, and it means a rule you just locked is not protecting anything yet.
The audit log group comes in two forms. _Audit covers one compartment. _Audit_Include_Subcompartment covers that compartment and everything below it. Pointing the connector at the tenancy with the first one misses every child compartment, which in most tenancies is where the workloads live.
Retention rules and versioning cannot both be on for one bucket. The templates switch versioning off, which is right here because the rule already blocks overwrites and deletes.
Deleting the connector's policy, or the Standard key policy, stops new copies, and nothing reports it. Hardened refuses both deletes, and its daily drift plan notices one made by hand.
How to undo it
The quick start rule is unlocked and lasts one day. After that, empty the bucket with oci os object bulk-delete and terraform destroy removes the rest. A locked rule cannot be removed or shortened. Deleting the Standard vault and key only schedules them, with a waiting period of at least seven days during which the deletion can be cancelled.
What it costs
Object Storage for the logs. Vault keys beyond your tenancy's free allowance are billed, so check the rate card before choosing HSM protection.
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.