IAM policies
Read-only policy
tfcensus-readonly.json grants every AWS action tfcensus calls, except those of move apply, move rollback and move mark-applied. Those three are the only commands that write, and only to state (safety model). Download it from /iam/tfcensus-readonly.json:
curl -fsSLO https://tfcensus.sasskovacs.dev/iam/tfcensus-readonly.json{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Identity",
"Effect": "Allow",
"Action": ["sts:GetCallerIdentity"],
"Resource": "*"
},
{
"Sid": "Organizations",
"Effect": "Allow",
"Action": ["organizations:List*"],
"Resource": "*"
},
{
"Sid": "EnabledRegions",
"Effect": "Allow",
"Action": ["account:ListRegions"],
"Resource": "*"
},
{
"Sid": "StateBucketsAndAudit",
"Effect": "Allow",
"Action": [
"s3:ListAllMyBuckets",
"s3:ListBucket",
"s3:GetBucket*",
"s3:GetEncryptionConfiguration",
"s3:GetLifecycleConfiguration",
"s3:GetReplicationConfiguration",
"s3:GetAccountPublicAccessBlock"
],
"Resource": "*"
},
{
"Sid": "ReadStateObjects",
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::*/*"
},
{
"Sid": "DecryptStateViaS3Only",
"Effect": "Allow",
"Action": ["kms:Decrypt"],
"Resource": "*",
"Condition": {
"StringLike": { "kms:ViaService": "s3.*.amazonaws.com" }
}
},
{
"Sid": "KeyAudit",
"Effect": "Allow",
"Action": ["kms:DescribeKey", "kms:GetKeyPolicy"],
"Resource": "*"
},
{
"Sid": "StateAccessLogging",
"Effect": "Allow",
"Action": ["cloudtrail:DescribeTrails", "cloudtrail:GetEventSelectors"],
"Resource": "*"
},
{
"Sid": "LockTables",
"Effect": "Allow",
"Action": ["dynamodb:DescribeTable", "dynamodb:DescribeContinuousBackups", "dynamodb:Scan"],
"Resource": "arn:aws:dynamodb:*:*:table/*"
},
{
"Sid": "ResourceExplorer",
"Effect": "Allow",
"Action": ["resource-explorer-2:Get*", "resource-explorer-2:List*"],
"Resource": "*"
},
{
"Sid": "Config",
"Effect": "Allow",
"Action": ["config:Describe*", "config:Get*", "config:Select*"],
"Resource": "*"
},
{
"Sid": "CloudControlRead",
"Effect": "Allow",
"Action": ["cloudformation:ListResources", "cloudformation:GetResource"],
"Resource": "*"
},
{
"Sid": "CloudControlHandlers",
"Effect": "Allow",
"Action": ["vpc-lattice:List*", "vpc-lattice:GetResourceConfiguration"],
"Resource": "*"
},
{
"Sid": "CloudFormationStacks",
"Effect": "Allow",
"Action": ["cloudformation:ListStacks", "cloudformation:ListStackResources"],
"Resource": "*"
},
{
"Sid": "NativeEnumerators",
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"iam:GetAccountAuthorizationDetails",
"route53:List*",
"eks:ListPodIdentityAssociations"
],
"Resource": "*"
}
]
}The policy is a subset of the AWS-managed ReadOnlyAccess and ViewOnlyAccess policies combined. A role with those two managed policies works too, plus kms:Decrypt when state uses SSE-KMS.
What each statement is for
| Statement | Used by | Notes |
|---|---|---|
Identity | every command that calls AWS | sts:GetCallerIdentity names the account of each profile. |
Organizations | --org | Lists the accounts and their OUs from the management account. |
EnabledRegions | --all-regions | account:ListRegions lists the regions each account has enabled. Drop it if you never set all_regions. |
StateBucketsAndAudit | inventory, audit backend, scan | Bucket settings for the audit checks and the S3 layer, and s3:ListBucket to find workspaces. |
ReadStateObjects | every command that reads state | State files hold secrets. tfcensus prints only identifiers, but the role can read everything in the objects. |
DecryptStateViaS3Only | reading SSE-KMS state | S3 calls KMS with the caller's permissions. The kms:ViaService condition limits decryption to requests made through S3. |
KeyAudit | audit backend | Reads the state key's description and policy (S3-ENC, S3-KMSPOL). |
StateAccessLogging | audit backend | Reads trail event selectors to see whether CloudTrail logs S3 data events for a bucket without server access logging (S3-LOG). |
LockTables | audit backend | dynamodb:Scan looks for stale locks and orphaned digests. Lock items hold lock metadata, not state. |
ResourceExplorer | doctor, scan, whois --live | The index, the default view and its resources. |
Config | doctor, scan, whois --live | Recorder status, aggregators and Select* queries over recorded resources. |
CloudControlRead | scan | The Cloud Control layer. |
CloudControlHandlers | scan | Cloud Control runs each type's list and read handlers with the caller's permissions. By default scan lists the VPC Lattice resource gateways, resource configurations and service network associations this way, because neither AWS Config nor Resource Explorer lists them. Types you add to scan.cloudcontrol_types need their own handler permissions (the handlers of the type's schema in the CloudFormation registry). |
CloudFormationStacks | scan | Lists stack resources to find what CloudFormation owns. It reads stack and resource identifiers, never templates or outputs. Drop it if you set scan.cloudformation: false. |
NativeEnumerators | scan, whois --live | The native layers: security group rules, NAT gateways, IAM attachments, Route 53 records and EKS Pod Identity associations (inventory layers). |
The policy grants nothing that tfcensus doesn't call. A test takes every AWS action from the recorded API traffic of the test suite and fails when the policy is missing one. It also fails when the policy grants an action that no recorded call needs. Three actions are exempt from that second check, because AWS calls them on the caller's behalf: kms:Decrypt (S3 reads an SSE-KMS object) and the two vpc-lattice actions (Cloud Control's handlers for the VPC Lattice types).
Narrow it
ReadStateObjects: replacearn:aws:s3:::*/*with your state buckets, such asarn:aws:s3:::acme-tfstate-*/*.DecryptStateViaS3Only: limitResourceto the KMS keys that encrypt state.LockTables: limitResourceto your lock tables, such asarn:aws:dynamodb:*:*:table/terraform-locks*.- Drop the statements of features you don't use:
EnabledRegions,Organizationswithout--org,CloudFormationStackswithscan.cloudformation: false.
Assumed roles
--org also needs sts:AssumeRole on the member-account role (default OrganizationAccountAccessRole, or the roles in accounts.org.ou_roles). So does a backend whose remote_state names a role_arn. Grant it on the scanning principal for exactly those roles. It isn't part of the policy, because what an assumed role may do is defined in its own account, and that role needs this policy.
Check it
AWS IAM Access Analyzer checks the policy's grammar and flags overly broad statements:
aws accessanalyzer validate-policy --policy-type IDENTITY_POLICY \
--policy-document file://tfcensus-readonly.jsonMove permissions
The move commands read and write the state of every involved unit with that unit's own backend credentials. They need, for every involved backend:
| Command | Permissions |
|---|---|
move suggest | what terragrunt plan needs in each unit |
move plan, move status | s3:GetObject, s3:GetBucketVersioning, plus what terragrunt plan needs in the unit |
move apply | the above, s3:GetObjectVersion, and what terraform state push needs: s3:PutObject on the state key, plus the lock (s3:PutObject and s3:DeleteObject on <key>.tflock, or dynamodb:PutItem, dynamodb:GetItem and dynamodb:DeleteItem) and s3:PutObject on <key>.tfcensus-migrations.json |
move rollback | the move apply set, plus s3:DeleteObject on the state key |
move mark-applied | s3:GetObject and s3:PutObject on <key>.tfcensus-migrations.json |