Skip to content

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:

sh
curl -fsSLO https://tfcensus.sasskovacs.dev/iam/tfcensus-readonly.json
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 ​

StatementUsed byNotes
Identityevery command that calls AWSsts:GetCallerIdentity names the account of each profile.
Organizations--orgLists the accounts and their OUs from the management account.
EnabledRegions--all-regionsaccount:ListRegions lists the regions each account has enabled. Drop it if you never set all_regions.
StateBucketsAndAuditinventory, audit backend, scanBucket settings for the audit checks and the S3 layer, and s3:ListBucket to find workspaces.
ReadStateObjectsevery command that reads stateState files hold secrets. tfcensus prints only identifiers, but the role can read everything in the objects.
DecryptStateViaS3Onlyreading SSE-KMS stateS3 calls KMS with the caller's permissions. The kms:ViaService condition limits decryption to requests made through S3.
KeyAuditaudit backendReads the state key's description and policy (S3-ENC, S3-KMSPOL).
StateAccessLoggingaudit backendReads trail event selectors to see whether CloudTrail logs S3 data events for a bucket without server access logging (S3-LOG).
LockTablesaudit backenddynamodb:Scan looks for stale locks and orphaned digests. Lock items hold lock metadata, not state.
ResourceExplorerdoctor, scan, whois --liveThe index, the default view and its resources.
Configdoctor, scan, whois --liveRecorder status, aggregators and Select* queries over recorded resources.
CloudControlReadscanThe Cloud Control layer.
CloudControlHandlersscanCloud 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).
CloudFormationStacksscanLists 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.
NativeEnumeratorsscan, whois --liveThe 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: replace arn:aws:s3:::*/* with your state buckets, such as arn:aws:s3:::acme-tfstate-*/*.
  • DecryptStateViaS3Only: limit Resource to the KMS keys that encrypt state.
  • LockTables: limit Resource to your lock tables, such as arn:aws:dynamodb:*:*:table/terraform-locks*.
  • Drop the statements of features you don't use: EnabledRegions, Organizations without --org, CloudFormationStacks with scan.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:

sh
aws accessanalyzer validate-policy --policy-type IDENTITY_POLICY \
  --policy-document file://tfcensus-readonly.json

Move 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:

CommandPermissions
move suggestwhat terragrunt plan needs in each unit
move plan, move statuss3:GetObject, s3:GetBucketVersioning, plus what terragrunt plan needs in the unit
move applythe 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 rollbackthe move apply set, plus s3:DeleteObject on the state key
move mark-applieds3:GetObject and s3:PutObject on <key>.tfcensus-migrations.json