✨ Add CloudFormation-based infrastructure deployment#8
Merged
Conversation
Code ReviewFound 1 issue that needs attention: Resource Leak in StackManager._get_client()File: src/zae_limiter/infra/stack_manager.py:61-72 Issue: The Problem:
Reference: The correct pattern is demonstrated in repository.py:34-50 Suggested fix: Cache the client in Additional notes:
|
Migrate from programmatic DynamoDB table creation to CloudFormation stack management for easier infrastructure lifecycle management. Changes: - Add StackManager for CloudFormation operations with auto-detection of local DynamoDB environments - Add CLI commands: deploy, delete, status, cfn-template - Update RateLimiter with create_stack parameter (deprecates create_table) - Add backward compatibility with deprecation warnings - Update documentation with CloudFormation deployment instructions - Add comprehensive tests for stack manager and CLI Benefits: - Users can easily delete all infrastructure via CloudFormation - Infrastructure-as-code best practices - Auto-detects local vs cloud environments - Maintains backward compatibility for existing users Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
- Remove unused mock_aws import from test_stack_manager.py - Add type casts for DynamoDB response to fix mypy errors
441e8e3 to
b74b40a
Compare
Critical fixes: - Make StackManager an async context manager with proper __aenter__/__aexit__ - Fix _get_client() to reuse client and properly manage lifecycle - Implement close() method to actually cleanup client and session - Update all usage sites to use async with pattern (repository, limiter, CLI) - Update test mocks to support async context manager protocol Resource leaks fixed: 1. CloudFormation client now properly closed via __aexit__ 2. Client reused across multiple API calls instead of creating new ones 3. All StackManager instances now closed after use in repository/limiter/CLI Test changes: - All CLI test mocks now include __aenter__/__aexit__ methods - All tests passing (17/17 CLI tests, 12/12 stack manager tests)
Fixes test failures caused by missing transitive dependency. Issue: - moto's cloudformation parsing imports apigateway models - apigateway models require openapi-spec-validator - moto[dynamodb] doesn't declare this dependency Solution: - Add openapi-spec-validator>=0.7.1 to dev dependencies - All previously failing tests now pass Test results: - test_limiter.py: All tests passing - test_sync_limiter.py: All 6 tests passing - No more ModuleNotFoundError
Issue: - Test fixtures were triggering CloudFormation stack creation - Moto doesn't properly mock CloudFormation in these tests - Caused cascade tests and others to fail Solution: - Update limiter fixtures to manually create table via direct API - Remove auto-creation flags (create_table/create_stack) - Call _repository.create_table() directly before entering context Test results: - All 86 tests passing - 4 skipped (require real AWS CloudFormation) - Cascade tests now working correctly
4 tasks
sodre
added a commit
that referenced
this pull request
Feb 15, 2026
- Add RepositoryBuilder .namespace() example to deployment.md (#8) - Add namespace ID discovery tip to cloudformation.md outputs (#13) - Migrate legacy StackOptions to RepositoryBuilder in auditing.md, testing.md, localstack.md, and version.md (#22) - Add migration guide link to deployment.md Next Steps (#24) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR migrates zae-limiter from programmatic DynamoDB table creation to CloudFormation-based stack management, making it easier for users to manage and delete infrastructure.
Changes
New Components
zae-limiter deploy: Deploy CloudFormation stackzae-limiter delete: Delete CloudFormation stackzae-limiter status: Check stack statuszae-limiter cfn-template: Export template for custom deploymentAPI Updates
create_stackparameter toRateLimiterandSyncRateLimitercreate_tableparameter (with deprecation warning)create_stack()anddelete_stack()methodsStackCreationError,StackAlreadyExistsErrorDocumentation
Key Features
✅ Auto-Detection: Automatically detects local DynamoDB (via
endpoint_url) and skips CloudFormation✅ Backward Compatible: Existing code continues to work with deprecation warnings
✅ CLI Convenience: Simple commands for infrastructure management
✅ Easy Cleanup: Users can delete entire stack via CloudFormation
Usage Examples
Deploy via CLI (Recommended)
Auto-create in code
Local development (auto-detects)
Testing
All tests pass:
Breaking Changes
None - fully backward compatible with deprecation warnings.
🤖 Generated with Claude Code