Skip to main content

A python model for Cloud Formation scripts

Project description

pycfmodel

Build Status PyPI version Documentation Status License

A python model for Cloud Formation scripts.

pycfmodel makes it easier to work with CloudFormation scripts in Python by creating a model comprised of python objects. Objects have various helper functions which help with performing common tasks related to parsing and inspecting CloudFormation scripts.

pip install pycfmodel

Currently Supported

  • AWSTemplateFormatVersion
  • Conditions
  • Description
  • Mappings
  • Metadata
  • Outputs
  • Parameters
  • Resources:
    • Properties:
      • Policy
      • Policy Document
      • Principal
      • Security Group Egress Prop
      • Security Group Ingress Prop
      • Statement
      • Tag
    • EC2 VPC Endpoint Policy
    • Generic Resource
    • IAM Group
    • IAM Managed Policy
    • IAM Policy
    • IAM Role
    • IAM User
    • KMS Key
    • OpenSearch Service (legacy ElasticSearch resource)
      • Elasticsearch Domain
    • OpenSearch Service
      • OpenSearchService Domain
    • S3 Bucket
    • S3 Bucket Policy
    • Security Group
    • Security Group Egress
    • Security Group Ingress
    • SNS Topic Policy
    • SQS Queue Policy
    • WAFv2 IP Set
  • Transform

Example

from pycfmodel import parse

template = {
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters": {"StarParameter": {"Type": "String", "Default": "*", "Description": "Star Param"}},
    "Resources": {
        "rootRole": {
            "Type": "AWS::IAM::Role",
            "Properties": {
                "AssumeRolePolicyDocument": {
                    "Version": "2012-10-17",
                    "Statement": [
                        {
                            "Effect": "Allow",
                            "Principal": {"AWS": {"Fn::Sub": "arn:aws:iam::${AWS::AccountId}:root"}},
                            "Action": ["sts:AssumeRole"],
                        }
                    ],
                },
                "Path": "/",
                "Policies": [
                    {
                        "PolicyName": "root",
                        "PolicyDocument": {
                            "Version": "2012-10-17",
                            "Statement": [
                                {
                                    "Effect": "Allow",
                                    "Action": {"Ref": "StarParameter"},
                                    "Resource": {"Ref": "StarParameter"},
                                }
                            ],
                        },
                    }
                ],
            },
        }
    },
}

model = parse(template).resolve(extra_params={"AWS::AccountId": "123"})
rootRole = model.Resources["rootRole"]
policy = rootRole.Properties.Policies[0]
statement = policy.PolicyDocument.Statement[0]

assert statement.Action == "*"
assert statement.Resource == "*"
assert rootRole.Properties.AssumeRolePolicyDocument.Statement[0].Principal == {"AWS": "arn:aws:iam::123:root"}

Local Development Commands

This project uses uv for dependency management.

make install-dev    # Install all development dependencies
make coverage       # Run tests with coverage
make test           # Run linting and tests
make lock           # Update uv.lock file
make lock-upgrade   # Upgrade all dependencies and update uv.lock

Updating CloudFormation Actions

If the test tests/test_constants.py::test_cloudformation_actions is failing, it can be resolved by updating the known AWS Actions:

make install-cloudformation-update
make cloudformation-update

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycfmodel-2.1.0.tar.gz (299.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pycfmodel-2.1.0-py3-none-any.whl (188.5 kB view details)

Uploaded Python 3

File details

Details for the file pycfmodel-2.1.0.tar.gz.

File metadata

  • Download URL: pycfmodel-2.1.0.tar.gz
  • Upload date:
  • Size: 299.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pycfmodel-2.1.0.tar.gz
Algorithm Hash digest
SHA256 cbe0fb918cd27938ee995649bfc88ff0b4bd84299c4b2dc71d6eb500406af176
MD5 25b910fcc8745c32f036ee17b57ce241
BLAKE2b-256 9905467d9f6faf905a8f6ec63e432d2f63294c63a2aeaa6d0fd44b55db16d722

See more details on using hashes here.

File details

Details for the file pycfmodel-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: pycfmodel-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 188.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pycfmodel-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70fa832a31f822805a973037872e61ffad27509dcfeeeefaa513aedd2b2c6de6
MD5 2daac67dc8b7dfeb162bd755f9713d07
BLAKE2b-256 0d01d5da9acf8ef2760de6eb4c5a8bbbe185475abaeb523053197889c1a20a1f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page