Skip to main content

cfn2iam

Generate the IAM policy a CloudFormation stack needs, from its template, before you deploy it.

Deploy roles are usually far broader than the stack needs, often AdministratorAccess, because working out the minimum by hand means reading the docs for every resource type in the template. IAM Access Analyzer can generate a policy, but it works from CloudTrail, so you have to deploy with broad permissions first and narrow them afterwards. cfn2iam reads the template instead.

Web version · PyPI

Install

pip install cfn2iam            # or: uv tool install cfn2iam
pip install "cfn2iam[iam]"     # adds boto3, for --create-role and registry lookups of private types

Requires Python 3.9+ and network access to the schema host (see How it works).

Usage

cfn2iam template.yaml

This writes policy-<random>.json to the current directory:

{
  "Version": "2012-10-17",
  "Statement": [
    { "Effect": "Allow", "Action": ["s3:CreateBucket", "s3:GetBucketTagging", "..."], "Resource": "*" },
    { "Effect": "Deny",  "Action": ["s3:DeleteBucket", "..."], "Resource": "*" }
  ]
}
Option Effect
-d, --allow-delete Allow the delete-only actions instead of denying them
-c, --create-role Also create an IAM role CloudFormation can assume, with this policy inline (needs cfn2iam[iam] and AWS credentials)
-r, --role-name NAME Role name for --create-role (default cfn2iam-<random>)
-p, --permissions-boundary ARN Permissions boundary for the created role
--ignore-unknown Skip resource types found in neither the public schemas nor the CloudFormation registry, instead of failing
--registry Look up every resource type with CloudFormation DescribeType in your account instead of the published schemas (needs cfn2iam[iam] and AWS credentials)
--region REGION Region for CloudFormation registry lookups (default: your AWS config)
--version Print the version

Examples:

cfn2iam template.yaml -d                       # allow deletes, e.g. for stacks you tear down
cfn2iam template.yaml -c -r MyDeployRole       # create the role as well
cfn2iam template.yaml -c -p arn:aws:iam::123456789012:policy/boundary

How it works

  1. Reads the template (JSON or YAML, including short-form intrinsics like !Ref) and collects the resource types.
  2. Expands SAM resources (AWS::Serverless::*) into the resources SAM creates, using rules that check the resource's properties (for example, a function with an SQS event also needs the event source mapping).
  3. Fetches each type's CloudFormation resource schema from mrlikl.github.io/cfn2iam/backend/schemas. These are AWS's published schemas, refreshed weekly by a GitHub workflow. The SAM expansion rules live next to them, in backend/sam_rules.
  4. For a type with no public schema (a private type, or a third-party type activated in your account), it calls CloudFormation DescribeType in your account and region. This needs cfn2iam[iam], AWS credentials and cloudformation:DescribeType. With --registry, every type is looked up this way; SAM rules still come from the published rules.
  5. Takes the permissions each schema lists for its create, read, update and list handlers as Allow, and the permissions only its delete handler needs as Deny (or Allow with -d).

If any resource type can't be resolved, cfn2iam lists them, writes no policy, and exits with status 1. A partial policy would fail mid-deploy at best. Network failures can't be skipped; types found nowhere can, with --ignore-unknown.

Limitations

  • Actions, not resources. Every statement uses "Resource": "*". Resource ARNs mostly don't exist until the stack does, so the policy is least-privilege on actions only.
  • The delete statement is an explicit Deny. An explicit Deny overrides an Allow from any other policy attached to the same role. Use -d if the role also has to delete these resources.
  • Custom resources are skipped (Custom::*, AWS::CloudFormation::CustomResource, AWS::CDK::Metadata). Anything they need, such as permission to invoke their Lambda function, isn't included.
  • The policy is only as complete as AWS's schemas. The handler permission lists are maintained by AWS and can be incomplete for some resource types or configurations. Test the policy on a non-production stack first.

License

MIT. See LICENSE.

Metadata

Release files for cfn2iam 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cfn2iam 0.2.0
File Size Uploaded
cfn2iam-0.2.0.tar.gz 32.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cfn2iam 0.2.0
File Interpreter ABI Platform
cfn2iam-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.0 kB

Release files / cfn2iam-0.2.0.tar.gz

Download URL cfn2iam-0.2.0.tar.gz
Size 32.7 kB
Tags Source
SHA-256 checksum
How to use checksums
23ed29fc48bd744636df406cae5c3494107e7fd7002a8e886784065fab5f8ce0
BLAKE2b-256 checksum
How to use checksums
75db9362e7e8c3cda7daafde08c8d94a434eb4d9a91b9dceae438420c992bd52
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cfn2iam-0.2.0-py3-none-any.whl

Download URL cfn2iam-0.2.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
eee70c6108c373aae4f27c5b686fcafe73a3cdf541c27d822a8392bee8baf290
BLAKE2b-256 checksum
How to use checksums
3d9b199ef4f48b978cdebed689ec8758b9da6bc91508bebb014fa2c5e4760a0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.0 {"installer":{"name":"uv","version":"0.13.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page