Terraform State as a Security Map: Linking Cloud to Code
A cloud finding says an S3 bucket allows public reads, or an IAM role trusts an outside account with no external ID. The finding is correct. Then it sits, because nobody can say which repository declared the bucket, which team owns that repository, or which file to change. The cloud console knows the resource. It does not know the code.
Your Terraform state already knows. Every terraform apply writes a record of which resource address in which root produced which cloud identifier. That record is the missing join between a runtime finding and the line of code that has to change. Read it carefully, keeping addresses and identifiers and throwing away everything else, and Terraform state becomes a security map rather than an operational artifact nobody in security looks at.
This post covers what state records, how to find it from the backend block, the cases where the map has gaps, how to lock down access to it, and a checklist for building cloud-to-code links your remediation process can rely on.
Why cloud findings stall without a link to code
Cloud security posture tools are good at saying what is wrong with a running resource. They are weak at saying who should fix it. The usual fallbacks are tags, naming conventions and account ownership, and each one breaks in a familiar way:
- Tags drift. An
ownertag records who created the resource two reorganizations ago. Nobody updates it when a module moves repositories. - Names guess. A bucket called
payments-logs-prodsuggests a team. It does not tell you whether the bucket came from the payments repository, a shared platform module or a one-off script. - Accounts are too coarse. A shared production account holds resources from dozens of repositories. Routing every finding to the account owner sends a platform team a queue it cannot act on.
The fix for a misconfiguration that came from code is a change to that code. Patching the console only works until the next apply reverts it. So the useful question for every cloud finding is: where is this resource declared? Answer that, and owner, fix location and review path follow from the repository.
What Terraform state actually records
A Terraform state file is JSON. For each managed resource instance it records the resource address (for example module.storage.aws_s3_bucket.logs), the resource type, the provider, the module path and the full set of attributes the provider returned after apply. That last part includes the identifiers you need, such as id, arn, name and bucket, and much that you do not want.
State also holds values that are sensitive. A database resource's password, a generated private key and a secret passed into a resource all land in state in plain text. Marking a variable or output sensitive = true hides it from CLI output. It does not remove it from the state file.
That shapes how a security program should consume state. The useful part is small:
- the resource address, type, provider and module, which point back to a block in a file;
- a handful of identifier attributes, which match the resource in the cloud inventory.
Everything else, including outputs and any attribute the state marks sensitive, should be discarded as the state is read and never stored. A pipeline that copies whole state files into a security data lake has created a second, less protected copy of your most sensitive infrastructure secrets.
Finding Terraform state from the backend block
The location of a root's state is in its backend block. For an S3 backend it looks like this:
terraform {
backend "s3" {
bucket = "acme-tf-state-prod"
key = "network/core/terraform.tfstate"
region = "us-east-1"
encrypt = true
}
}
Scanning repositories for these blocks gives a list of roots and the bucket and key each one writes. A few details turn that list into a complete one.
Workspaces
The default workspace lives at key. Every other workspace lives at <workspace_key_prefix>/<workspace>/<key>, and the prefix defaults to env:. A reader that only fetches key sees the default workspace and misses staging and production if those are workspaces.
Partial backend configuration
Many teams leave bucket and key out of the block and pass them at init time, as in terraform init -backend-config=env/prod.s3.tfbackend. When that file is committed, it names the location just as well as the block does. When the values exist only as flags in a CI pipeline, the repository alone cannot tell you where state lives.
HCP Terraform and Pulumi
A cloud block or remote backend names an HCP Terraform organization and workspace instead of a bucket. Reading it takes an API token scoped to read state versions. Pulumi projects using a self-managed backend name an S3 URL in Pulumi.yaml, and each stack plays the role of a workspace.
Where the map has gaps
State gives an exact link for resources Terraform declares directly. Several common resources are not declared directly, and a good map handles them on purpose rather than reporting them as unowned.
- Launched instances. An EC2 instance started by an Auto Scaling group, an EKS node group or an ECS capacity provider never appears in state. The right link is to where its launcher is declared, because that is where a fix to the launch template or user data goes.
- CloudFormation stacks. Resources a stack creates belong to the stack, and the stack belongs to a template in a repository.
- Serverless Framework and AWS CDK. Both compile to CloudFormation. A
serverless.ymlfunction becomes a Lambda function in a stack, and a CDKCfn*construct becomes exactly one CloudFormation resource. Higher-level CDK constructs apply defaults that are not visible in the source, so a link to them is weaker. - Click-ops resources. Some resources were created by hand. The honest answer for those is "created by hand", which is itself a finding: it is infrastructure outside review.
- Resources IaC cannot declare. Default VPCs and similar account defaults should not count against your coverage.
Measure coverage as the share of cloud resources with open findings that link to a file and line. That number tells you how much of your cloud remediation can go through code review, and it is a better program metric than raw finding counts.
Securing Terraform state access
If state becomes a security data source, access to it becomes a security control. Reading state for attribution needs much less than the pipelines that write it.
- Use a dedicated read-only role. Grant
s3:ListBucketon the state buckets ands3:GetObjecton their objects, and nothing else. No write, no delete, no access to other buckets. Keep it separate from the role your cloud scanner uses for inventory so each can be reviewed on its own terms. - Scope KMS decrypt to S3. If state objects use a customer-managed key, grant
kms:Decryptwith a condition that limits it to requests through S3, so the role cannot decrypt anything else with that key. - Use an external ID for third-party trust. A role assumed by an outside service should require an external ID in its trust policy. This is the same gap Cloud Events now flags when a role trusts an outside account without one.
- Handle cross-account buckets explicitly. A role can read a bucket in another account only when that bucket's policy also allows it. Add a statement for the reader role alone, and watch for an explicit
Denythat overrides it. - Protect the bucket itself. Block public access, turn on versioning, encrypt objects and log data events on the bucket, so you can see who read state and when.
A reader built this way can see where resources came from, and cannot change a single one.
What to do: a checklist for cloud-to-code attribution
- Inventory every Terraform, OpenTofu and Pulumi root across your repositories, with the backend each one names.
- Commit backend configuration files that only exist as CI flags today, or export state from the pipeline after apply.
- Deploy a read-only state reader role scoped to the state buckets and keys, with an external ID if a third party assumes it.
- Add bucket and key policy statements for state buckets in other accounts.
- Read every workspace, not only the default one.
- Keep addresses and identifiers. Discard outputs and sensitive attributes at read time.
- Link launched instances to their launcher, and stacks to their templates.
- Route each finding to the team that owns the declaring repository, not the account.
- Fix through a pull request to the declaration, so the next apply does not undo the fix.
- Track the share of findings linked to code, and treat "created by hand" as its own queue.
Attribution at runtime pairs with prevention at the pull request. Our earlier post on gating IaC misconfigurations in Terraform pull requests covers the other half: stopping the misconfiguration before it reaches the cloud.
How Heeler reads Terraform state
An AWS connection in Heeler can take an optional state reader role. Heeler then reads the state file named in each Terraform root's backend block every 12 hours, including workspaces and committed backend configuration files. It reads S3 backends, including buckets in another account, and HCP Terraform workspaces with a team token. Pulumi state in S3 is read as well. OpenTofu roots are read the same way as Terraform.
From each state, Heeler keeps each managed resource's address, type, provider and module, plus the id, arn, name, bucket, self_link and unique_id attributes when the state does not mark them sensitive. Every other attribute, every output and every sensitive value is discarded as the state is read. The role's CloudFormation template grants s3:ListBucket, s3:GetObject and, when you name keys, kms:Decrypt through S3 only. It cannot write to state.
The links feed the Context Engine:
- A cloud resource's Code to Cloud panel shows the
path:linethat declares it, the repository, the IaC tool and how it was matched. EC2 instances link to the Auto Scaling group, EKS node group or ECS capacity provider that launched them, and CloudFormation stacks link to their template. - A cloud finding's owner is the sole team of the repository that holds the declaration, falling back to the team that owns the account.
- A finding whose resource is linked can be fixed with Open Fix PR, which changes the declaring block and opens a pull request for your reviewers.
- An IaC guardrail's Live resource condition gates a pull request on whether the resource the changed block declares is in production, internet-exposed or part of a toxic combination.
Administration > Connections > IaC State lists the state buckets and roots read, the IaC tool for each, buckets Heeler could not read with the IAM change that fixes access, and the share of cloud resources with findings that link to code. A root whose backend location exists only in CI shows as Backend Set in CI, and heelercli iac export can send its state manifest from the pipeline instead.
IaC scanning also covers Serverless Framework serverless.yml files, checked as the CloudFormation stack they produce, and AWS CDK L1 Cfn* constructs in TypeScript and JavaScript. The existing CloudFormation checks and IaC guardrails apply to both. For how this fits a program that spans many teams and accounts, see AppSec at scale.
FAQ
Does reading Terraform state expose secrets?
It does not have to. State contains secrets in plain text, so an attribution reader should keep only resource addresses and identifier attributes and discard outputs and sensitive values at read time. Heeler stores only addresses, types, providers, modules and a short list of identifier attributes.
What permissions does a Terraform state reader need?
List and read on the state buckets, and nothing more. That means s3:ListBucket on each bucket, s3:GetObject on its objects and, for customer-managed keys, kms:Decrypt limited to S3. Buckets in other accounts also need a bucket policy statement that allows the reader role.
What if our backend location is only set in CI?
Commit the backend configuration file the pipeline passes, or export state from the pipeline. A repository scan cannot find a location that exists only in terraform init flags, so Heeler lists those roots as Backend Set in CI and accepts a state manifest from heelercli iac export.
How are EC2 instances in an Auto Scaling group linked to code?
Through the group that launched them. The instance never appears in state, so the useful link is to where the Auto Scaling group, EKS node group or ECS capacity provider is declared, which is where a launch configuration fix belongs.
Are AWS CDK and Serverless Framework resources covered?
Yes, as the CloudFormation they compile to. Serverless Framework files are checked as their stack, and CDK L1 Cfn* constructs in TypeScript and JavaScript are checked as the resources they produce. CDK L2 and L3 constructs are not checked.
A cloud finding with a file, a line and an owning team is a pull request waiting to be opened. A cloud finding without them is a ticket waiting to be reassigned. If you want to see your Terraform state turn cloud findings into owned, reviewable code changes, Get a demo


.jpg)
