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.1.tar.gz (303.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.1-py3-none-any.whl (193.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pycfmodel-2.1.1.tar.gz
Algorithm Hash digest
SHA256 95535d05c6fc0c89a05c610802ea1f7d95c66b07236d2fa7118ba5ad2964f642
MD5 de42a3f13f3fdaaf8a45093340d2e7c6
BLAKE2b-256 da1809339f32224d2bf0d68638bfee7abb033f944173adf4f0e5350200cf721c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for pycfmodel-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5340cdbc31c871fa2b0d17910b9df9b25a8a255e40d05d8aacd2ea567f8a3ef5
MD5 3bba7df1e15e910e42517557a8cba37b
BLAKE2b-256 b722f626f4bbecc7b8f9c9ea46aeb6ddea05bdd2a9e39542b6dffda2171116d6

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