Interview Prep/Terraform/Testing and Policy as Code

How do you write a custom Checkov policy?

IntermediateProductionTesting and Policy as Code
Answer

A YAML policy (or Python check) defines a resource type and attribute condition.

metadata:
  id: "CKV2_ACME_1"
  name: "S3 buckets must have Owner tag"
  category: "GENERAL_SECURITY"
definition:
  cond_type: attribute
  resource_types: ["aws_s3_bucket"]
  attribute: "tags.Owner"
  operator: exists

Run with checkov -d . --external-checks-dir ./policies. Keep custom policies in one repo, versioned and tested.

All Terraform questions Roadmap: Terraform in production

More on Testing and Policy as Code