Skip to main content

No project description provided

Project description

AWS SAM Tools

CI Test Build PyPI version Python 3.13+

A comprehensive Python package for processing AWS CloudFormation templates with advanced YAML parsing capabilities and extended processing features.

Overview

aws-sam-tools provides utilities for working with AWS CloudFormation templates, including a custom YAML parser that properly handles CloudFormation-specific intrinsic function tags (like !Ref, !GetAtt, !Sub) which standard YAML parsers cannot handle correctly. The package also includes extended processing capabilities for advanced template manipulation and an OpenAPI specification processor.

Key Features

🔧 CloudFormation YAML Processing

  • Custom YAML Loader: Properly parses all CloudFormation intrinsic function tags
  • Tag Preservation: Maintains CloudFormation syntax while enabling programmatic access
  • Validation: Built-in validation for CloudFormation tag syntax according to AWS specifications
  • Error Handling: Detailed error messages with YAML position information

⚡ Extended Processing Capabilities

  • File Inclusion: Include content from external files (!CFNToolsIncludeFile)
  • String Conversion: Convert data structures to JSON/YAML strings (!CFNToolsToString)
  • UUID Generation: Generate unique identifiers (!CFNToolsUUID)
  • Version Stamping: Include git version information (!CFNToolsVersion)
  • Timestamp Generation: Add timestamps with formatting options (!CFNToolsTimestamp)
  • Checksum Calculation: Calculate hashes of data or files (!CFNToolsCRC)

🌐 OpenAPI Processing

  • Rule-Based Transformations: Filter and modify OpenAPI specifications
  • Flexible Expressions: Use Python expressions for complex filtering logic
  • Multiple Formats: Support for both JSON and YAML OpenAPI specifications

🖥️ Command Line Interface

  • Template Processing: Process CloudFormation templates from command line
  • OpenAPI Processing: Apply transformations to OpenAPI specifications
  • Format Conversion: Convert between different output formats
  • Integration Ready: Easy integration into build pipelines and CI/CD workflows

Supported CloudFormation Tags

The package supports all standard CloudFormation intrinsic functions:

Tag Description Example
!Ref Reference parameters or resources !Ref MyBucket
!GetAtt Get resource attributes !GetAtt MyBucket.DomainName
!Sub String substitution !Sub 'Hello ${Name}'
!Join Join values with delimiter !Join [',', [a, b, c]]
!Split Split string into array !Split [',', 'a,b,c']
!Select Select from array !Select [0, !GetAZs '']
!FindInMap Find value in mapping !FindInMap [RegionMap, !Ref 'AWS::Region', AMI]
!Base64 Base64 encode !Base64 'Hello World'
!Cidr Generate CIDR blocks !Cidr ['10.0.0.0/16', 6, 8]
!ImportValue Import from another stack !ImportValue SharedVPC
!GetAZs Get availability zones !GetAZs 'us-east-1'
!Transform Apply transforms !Transform {'Name': 'AWS::Include', 'Parameters': {...}}

Plus all condition functions: !And, !Equals, !If, !Not, !Or, !Condition

Installation

pip install aws-sam-tools

For development:

git clone https://github.com/yourusername/aws-sam-tools.git
cd aws-sam-tools
make init

Quick Start

Basic CloudFormation Template Processing

from aws_sam_tools.cfn_yaml import load_yaml_file

# Load a CloudFormation template
template = load_yaml_file('template.yaml')

# Access CloudFormation tags as objects
bucket_name = template['Resources']['MyBucket']['Properties']['BucketName']
print(type(bucket_name))  # <class 'aws_sam_tools.cfn_yaml.RefTag'>
print(bucket_name.value)  # 'MyBucketParameter'

Extended Processing with CFNTools Tags

# template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: !CFNToolsToString
  - Template built on: !CFNToolsTimestamp
    Version: !CFNToolsVersion
    ID: !CFNToolsUUID
  - ConvertTo: JSONString
    OneLine: true

Resources:
  UserDataScript: !CFNToolsIncludeFile scripts/setup.sh
  
  ConfigurationHash: !CFNToolsCRC
    - !CFNToolsIncludeFile config/app-config.json
    - Algorithm: sha256
      Encoding: hex
from aws_sam_tools.cfn_processing import load_yaml_file

# Process template with CFNTools tags
template = load_yaml_file('template.yaml')

# CFNTools tags are resolved:
# - !CFNToolsTimestamp becomes actual timestamp
# - !CFNToolsUUID becomes generated UUID
# - !CFNToolsIncludeFile includes file content
# - !CFNToolsCRC calculates checksum

Convert to AWS-Compatible Format

from aws_sam_tools.cfn_processing import load_yaml_file
import yaml

# Load and convert CloudFormation tags to intrinsic functions
template = load_yaml_file('template.yaml', replace_tags=True)

# Output AWS-compatible YAML
aws_yaml = yaml.dump(template, default_flow_style=False)
print(aws_yaml)
# BucketName: 
#   Ref: MyBucketParameter

Command Line Usage

Process CloudFormation templates:

# Process CFNTools tags and convert CloudFormation tags to intrinsic functions
aws-sam-tools template process --template template.yaml --output processed.yaml --replace-tags

# Process without converting CloudFormation tags
aws-sam-tools template process --template template.yaml --output processed.yaml

Process OpenAPI specifications:

# Remove operations without security requirements
aws-sam-tools openapi process \
  --rule "path/method : delete : resource.security == 'none'" \
  --input api.yaml \
  --output filtered-api.yaml

# Multiple rules
aws-sam-tools openapi process \
  --rule "path/method : delete : resource.security == 'none'" \
  --rule "path/method : delete : method == 'options'" \
  --input api.yaml

CFNTools Processing Tags

File Inclusion (!CFNToolsIncludeFile)

Include content from external files:

# Include shell script
UserData: !CFNToolsIncludeFile scripts/userdata.sh

# Include JSON configuration  
Config: !CFNToolsIncludeFile config/app.json

# Include nested YAML with CFNTools support
NestedTemplate: !CFNToolsIncludeFile templates/nested.yaml

String Conversion (!CFNToolsToString)

Convert data structures to strings:

# Convert to JSON string
PolicyDocument: !CFNToolsToString
  - Version: '2012-10-17'
    Statement:
      - Effect: Allow
        Action: 's3:GetObject'
  - ConvertTo: JSONString
    OneLine: true

# Convert to YAML string
ConfigData: !CFNToolsToString
  - database:
      host: localhost
      port: 5432
  - ConvertTo: YAMLString

Unique Identifiers (!CFNToolsUUID)

Generate UUIDs:

Resources:
  MyResource:
    Properties:
      UniqueId: !CFNToolsUUID

Version Information (!CFNToolsVersion)

Include git version:

Parameters:
  Version:
    Type: String
    Default: !CFNToolsVersion

  PEP440Version:
    Type: String  
    Default: !CFNToolsVersion
      Style: pep440

Timestamps (!CFNToolsTimestamp)

Generate timestamps:

Parameters:
  BuildTime:
    Default: !CFNToolsTimestamp
    
  ExpiryTime:
    Default: !CFNToolsTimestamp
      Offset: 30
      OffsetUnit: days
      Format: '%Y-%m-%d'

Checksums (!CFNToolsCRC)

Calculate hashes:

Parameters:
  ConfigHash:
    Default: !CFNToolsCRC
      - !CFNToolsIncludeFile config.json
      - Algorithm: sha256
        Encoding: hex
        
  FileHash:
    Default: !CFNToolsCRC ["file://./setup.sh"]

OpenAPI Processing

Process OpenAPI specifications with rule-based transformations:

from aws_sam_tools.openapi import process_openapi

# Remove all operations without security
rules = ["path/method : delete : resource.security == 'none'"]
result = process_openapi(openapi_content, rules)

# Complex filtering
rules = [
    "path/method : delete : resource.security == 'none'",
    "path/method : delete : method == 'options'",
    "path/method : delete : path.startswith('/internal')"
]
result = process_openapi(openapi_content, rules)

Development

Requirements

  • Python 3.13+
  • uv (package manager)

Setup

# Clone repository
git clone <repository-url>
cd aws-sam-tools

# Initialize development environment
make init

# Run tests
make test

# Type checking
make pyright

# Format code
make format

# Build package
make build

Running Tests

# Run all tests
make test

# Run specific test
uv run pytest tests/test_cfn.py::test_ref_tag -v

# Run with coverage
uv run pytest --cov=cfn_tools

Architecture

Core Components

  1. cfn_tools/cfn_yaml.py - Core YAML parser with CloudFormation tag support
  2. cfn_tools/cfn_processing.py - Extended processing with CFNTools tags
  3. cfn_tools/cli.py - Command line interface
  4. cfn_tools/openapi.py - OpenAPI specification processing

Design Principles

  • Tag Preservation: CloudFormation tags are preserved as objects for programmatic access
  • Validation: All tag constructors validate syntax according to AWS specifications
  • Error Handling: Detailed error messages with YAML position information
  • Extensibility: Easy to add new processing tags and capabilities

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add tests for new functionality
  5. Run the test suite (make test)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes and version history.

Support

Project details


Download files

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

Source Distribution

aws_sam_tools-0.1.1.tar.gz (65.2 kB view details)

Uploaded Source

Built Distribution

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

aws_sam_tools-0.1.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

Details for the file aws_sam_tools-0.1.1.tar.gz.

File metadata

  • Download URL: aws_sam_tools-0.1.1.tar.gz
  • Upload date:
  • Size: 65.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.12

File hashes

Hashes for aws_sam_tools-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d2eeac09aca7b5f67f04c0ce4bd345985e41bd8f455a3366c7d134ec3438e0ae
MD5 e566addf5f1ffd4cee4494a548f28eaf
BLAKE2b-256 221f53439ec2226e8d5ee7e2a675061cec8dec13f4f4d08ccf2392d059ac7540

See more details on using hashes here.

File details

Details for the file aws_sam_tools-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aws_sam_tools-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 619bb7ac78607bf527d10810229d33463a272ab68b3dde5387fabd5933ab6609
MD5 75d946f50d2591f24dd3c990d5814680
BLAKE2b-256 1313fbbd2f94dc961f48eb7728ba64b2818f73ba09f4ea3781a35533d854faea

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