Most cloud security incidents do not start with an exploit. They start with a configuration: a storage bucket made public, a database open to the internet, an IAM role with wildcard permissions, or logging switched off. Cloud security posture tools find these problems in running environments, after they already exist.
Infrastructure as code (IaC) offers a better opportunity. When infrastructure is defined in Terraform, CloudFormation, Pulumi or Kubernetes manifests, every change is a file in a pull request, and misconfigurations can be caught before anything is deployed.
What IaC scanning finds
IaC scanners parse configuration files and check them against hundreds of rules:
- public storage buckets and missing encryption,
- security groups open to
0.0.0.0/0on sensitive ports, - databases without encryption, backups or deletion protection,
- IAM policies with wildcard actions or resources,
- missing logging (CloudTrail, VPC flow logs, access logs), and
- Kubernetes pods running privileged or as root.
Popular open-source tools include Checkov, tfsec, KICS and Terrascan.
# Flagged: public-read ACL, no encryption, no versioning
resource "aws_s3_bucket" "reports" {
bucket = "acme-customer-reports"
acl = "public-read"
}
# Better: private, encrypted, versioned, public access blocked
resource "aws_s3_bucket" "reports" {
bucket = "acme-customer-reports"
}
resource "aws_s3_bucket_public_access_block" "reports" {
bucket = aws_s3_bucket.reports.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_server_side_encryption_configuration" "reports" {
bucket = aws_s3_bucket.reports.id
rule {
apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" }
}
}
resource "aws_s3_bucket_versioning" "reports" {
bucket = aws_s3_bucket.reports.id
versioning_configuration { status = "Enabled" }
}Put scanning in the pull request
# CI job: fail the PR on high-severity IaC findings
iac-scan:
image: bridgecrew/checkov:2
script:
- checkov -d infra/ --framework terraform --compact --soft-fail-on LOW,MEDIUMReport findings as pull request comments with the fix, so developers can resolve them before merge. Start in report-only mode, tune rules and suppressions, then block on the findings that matter.
Policy as code: your rules, automated
Generic rules are a start. Every organisation also has its own requirements: approved regions, mandatory tags, allowed instance types, which teams may create public endpoints. Policy as code expresses those rules in a language such as Rego (Open Policy Agent) and evaluates them automatically.
Evaluating the Terraform plan, rather than the source, catches issues that depend on variables and modules:
# policy/tags.rego: every resource must have owner and cost_centre tags
package terraform
deny[msg] {
rc := input.resource_changes[_]
rc.change.actions[_] == "create"
tags := object.get(rc.change.after, "tags", {})
missing := {t | t := ["owner", "cost_centre"][_]; not tags[t]}
count(missing) > 0
msg := sprintf("%s is missing required tags: %v", [rc.address, missing])
}terraform plan -out tfplan && terraform show -json tfplan > tfplan.json
conftest test tfplan.json --policy policy/Build secure modules
The most effective way to prevent misconfiguration is to make secure configuration the default. Publish internal Terraform modules for common resources (buckets, databases, clusters) with encryption, logging, private networking and backups built in, and encourage teams to use them rather than raw resources.
Protect Terraform state
Terraform state files record the full configuration of your infrastructure, and often contain secrets in plain text: database passwords, generated keys and connection strings.
- Store state in a remote backend with encryption at rest and strict access control.
- Enable state locking to prevent concurrent corruption.
- Never commit state files to Git.
- Limit who, and which pipelines, can read state for each environment.
Apply from the pipeline
Run terraform apply from CI/CD with reviewed plans, not from engineers' laptops with personal admin credentials. That creates an audit trail, applies the same checks every time and removes the need for broad standing access.
Detect drift
Manual changes in the console ("just this once") create drift between code and reality, and bypass every check above. Detect it by running scheduled terraform plan jobs against each environment and alerting on unexpected changes, and complement IaC scanning with runtime posture monitoring.
The bottom line
Infrastructure as code turns cloud security from an after-the-fact audit into a code review. Scan every change, encode your organisation's rules as policy, provide secure modules, protect state and apply only through the pipeline. Most of the misconfigurations that lead to cloud breaches will never reach production.
Our AWS, Azure and Google Cloud security audits include IaC and pipeline reviews, and our DevSecOps team builds policy-as-code workflows.