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.11+ 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
- Reads the template (JSON or YAML, including short-form intrinsics like
!Ref) and collects the resource types. - 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). - 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. - For a type with no public schema (a private type, or a third-party type activated in your account), it calls CloudFormation
DescribeTypein your account and region. This needscfn2iam[iam], AWS credentials andcloudformation:DescribeType. With--registry, every type is looked up this way; SAM rules still come from the published rules. - Takes the permissions each schema lists for its
create,read,updateandlisthandlers as Allow, and the permissions only itsdeletehandler 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 (including a SAM rule that can't be fetched) can't be skipped; types found nowhere can, with --ignore-unknown.
One implementation, two front ends
The logic lives in cfn2iam.core: template parsing, SAM expansion, permissions from the schema handlers, and the policy document. It does no I/O of its own; it reads schemas and SAM rules through a fetch function its caller passes in, and needs only PyYAML. The CLI (cfn2iam.app) wraps it with HTTPS fetching, registry lookups, --create-role and file output. The web version installs this same package in the browser with Pyodide, so both produce the same policy.
import asyncio
from cfn2iam import core
def fetch_json(path): # e.g. "schemas/AWS_S3_Bucket.json"; may also be async
... # return the parsed JSON, or raise core.NotFound if there is no such file
analysis = asyncio.run(core.analyze_template(open("template.yaml").read(), fetch_json))
analysis.policy # the policy document
analysis.failures # resource types that couldn't be resolved, and why
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
-dif 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.
Development
The project uses uv. These are the main checks CI runs on every pull request:
uv sync # add --extra iam to include boto3 and its tests
uv run pytest
uv run ruff check . && uv run ruff format --check .
uv lock --check # uv.lock matches pyproject.toml
uv build && uvx twine check --strict dist/*
CI also runs the tests on every Python from 3.11 to 3.15 on Linux (and the oldest and newest on macOS and Windows), installs the built wheel into a clean environment and runs the CLI on samples/, and installs the wheel in Pyodide to check that cfn2iam.core still runs in the browser (tests/pyodide/smoke.mjs).
Releasing
Merge everything for the release into main through pull requests, then tag the merge commit and push the tag:
git switch main && git pull
git tag v0.3.1 && git push origin v0.3.1
The tag starts .github/workflows/publish.yml, which:
- checks that the tag is a full
vX.Y.Zversion and that its commit is onmain; - runs the whole test workflow above, including the build;
- uploads that build's wheel and sdist to PyPI with trusted publishing (the
pypienvironment); - once the upload has succeeded, checks the files against what PyPI serves and creates the GitHub Release for the tag, with the same wheel and sdist attached.
The version comes from the tag (hatch-vcs), so there is nothing to bump in the code. If any step fails, nothing after it runs: no upload without a green test run, and no Release without a successful upload. A moving tag such as v1 never publishes or creates a Release. After fixing the cause of a failed Release step, use Re-run failed jobs on the run; the release job skips what already exists.
The release notes are GitHub's generated notes: install instructions for the version, then the pull requests merged since the previous vX.Y.Z tag, grouped by label as set in .github/release.yml (breaking, enhancement, bug and documentation; unlabelled pull requests under "Changes"; Dependabot's under "Dependency updates"). So the changelog is the pull request titles: give them titles that make sense to a user. There is no CHANGELOG.md to update. Notes can be edited on GitHub after the Release is created, and a re-run leaves them alone.
License
MIT. See LICENSE.
Metadata
Release files for cfn2iam 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cfn2iam-0.3.1.tar.gz | 55.3 kB | With provenance | Details for cfn2iam-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cfn2iam-0.3.1-py3-none-any.whl | Python 3 | none | any | Details for cfn2iam-0.3.1-py3-none-any.whl |
Total release size: 69.8 kB
Release files / cfn2iam-0.3.1.tar.gz
| Download URL | cfn2iam-0.3.1.tar.gz |
|---|---|
| Size | 55.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
86988f83dbc965ddd735d8ca33c98e50e23ea51ced0b0c2806aa3a96b54bf6d2
|
|
BLAKE2b-256 checksum How to use checksums |
c72193128ffa64fb2bfdaf04ae7eee967c1ac66a341a2de2e77914fab0287b2e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.
Transparency logRelease files / cfn2iam-0.3.1-py3-none-any.whl
| Download URL | cfn2iam-0.3.1-py3-none-any.whl |
|---|---|
| Size | 14.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c6d3f8bed32c32c6eecceaa91b2eb4338dd3608e155c685517c4f0f696550d9c
|
|
BLAKE2b-256 checksum How to use checksums |
838c1c8045baad4710b65809e1030b1c54f1aa5db54aea9858f33b5c4798f712
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.8 {"installer":{"name":"uv","version":"0.13.8","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.
Transparency logSecurity for cfn2iam 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for cfn2iam-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.