lambda-api-decorators-cdk 0.2.3__tar.gz → 0.4.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lambda_api_decorators_cdk-0.4.1/.github/workflows/tests.yml +36 -0
- lambda_api_decorators_cdk-0.4.1/PKG-INFO +196 -0
- lambda_api_decorators_cdk-0.4.1/README.md +149 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/pyproject.toml +4 -3
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/ast_helper.py +21 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/lambda_api_config.py +38 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/resource_builder.py +129 -6
- lambda_api_decorators_cdk-0.4.1/src/lambda_api_decorators_cdk.egg-info/PKG-INFO +196 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk.egg-info/SOURCES.txt +9 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk.egg-info/scm_file_list.json +9 -0
- lambda_api_decorators_cdk-0.4.1/src/lambda_api_decorators_cdk.egg-info/scm_version.json +8 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_auth_ast.py +122 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorization_diagnostics.py +101 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorization_http.py +82 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorization_lambda_api.py +72 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorization_resolution.py +122 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorization_rest.py +94 -0
- lambda_api_decorators_cdk-0.4.1/tests/test_authorizer_config.py +113 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_lambda_api_config.py +5 -3
- lambda_api_decorators_cdk-0.4.1/tests/test_python_compatibility.py +65 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_resource_builder_state.py +3 -3
- lambda_api_decorators_cdk-0.2.3/PKG-INFO +0 -144
- lambda_api_decorators_cdk-0.2.3/README.md +0 -98
- lambda_api_decorators_cdk-0.2.3/src/lambda_api_decorators_cdk.egg-info/PKG-INFO +0 -144
- lambda_api_decorators_cdk-0.2.3/src/lambda_api_decorators_cdk.egg-info/scm_version.json +0 -8
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/.github/workflows/release.yml +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/.gitignore +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/AGENTS.md +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/LICENSE +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/setup.cfg +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/__init__.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/api_type.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/lambda_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk/source_layout.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk.egg-info/dependency_links.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk.egg-info/requires.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/src/lambda_api_decorators_cdk.egg-info/top_level.txt +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/conftest.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_ast_decorator_invocations.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_ast_helper.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_http_builder.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_lambda_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_lambda_function.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_layer_autodiscovery.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_packaging_current.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_permission_resolution.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_public_api.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_resource_builder_options.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_resource_graph.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_resource_registries.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_rest_builder.py +0 -0
- {lambda_api_decorators_cdk-0.2.3 → lambda_api_decorators_cdk-0.4.1}/tests/test_source_layout.py +0 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
pull_request:
|
|
6
|
+
|
|
7
|
+
permissions:
|
|
8
|
+
contents: read
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
test:
|
|
12
|
+
runs-on: ubuntu-latest
|
|
13
|
+
strategy:
|
|
14
|
+
fail-fast: false
|
|
15
|
+
matrix:
|
|
16
|
+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
|
|
17
|
+
|
|
18
|
+
steps:
|
|
19
|
+
- name: Check out repository
|
|
20
|
+
uses: actions/checkout@v4
|
|
21
|
+
|
|
22
|
+
- name: Set up Python ${{ matrix.python-version }}
|
|
23
|
+
uses: actions/setup-python@v5
|
|
24
|
+
with:
|
|
25
|
+
python-version: ${{ matrix.python-version }}
|
|
26
|
+
|
|
27
|
+
- name: Install package and test dependencies
|
|
28
|
+
run: |
|
|
29
|
+
python -m pip install --upgrade pip
|
|
30
|
+
python -m pip install --editable . pytest tomli
|
|
31
|
+
|
|
32
|
+
- name: Verify package import
|
|
33
|
+
run: python -c "import lambda_api_decorators_cdk"
|
|
34
|
+
|
|
35
|
+
- name: Run tests
|
|
36
|
+
run: pytest -q
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lambda-api-decorators-cdk
|
|
3
|
+
Version: 0.4.1
|
|
4
|
+
Summary: AWS CDK integration for Lambda API Decorators. Generate AWS Lambda and API Gateway infrastructure from decorated Python handlers.
|
|
5
|
+
Author: Mateo Marcos, Emanuel Arguinarena, Lucas Picchi
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2024 Lambda API Decorators CDK
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
Project-URL: Homepage, https://github.com/lambda-api-decorators/lambda-api-decorators-cdk
|
|
28
|
+
Project-URL: Repository, https://github.com/lambda-api-decorators/lambda-api-decorators-cdk
|
|
29
|
+
Project-URL: Bug Reports, https://github.com/lambda-api-decorators/lambda-api-decorators-cdk
|
|
30
|
+
Keywords: cdk,lambda,aws,api gateway,ast
|
|
31
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
32
|
+
Classifier: Intended Audience :: Developers
|
|
33
|
+
Classifier: Framework :: AWS CDK :: 2
|
|
34
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
35
|
+
Classifier: Topic :: Software Development :: Build Tools
|
|
36
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
42
|
+
Requires-Python: >=3.10
|
|
43
|
+
Description-Content-Type: text/markdown
|
|
44
|
+
License-File: LICENSE
|
|
45
|
+
Requires-Dist: aws-cdk.aws-lambda-python-alpha
|
|
46
|
+
Dynamic: license-file
|
|
47
|
+
|
|
48
|
+
# Lambda API Decorators CDK
|
|
49
|
+
|
|
50
|
+
AWS CDK integration for [Lambda API Decorators](https://github.com/infrastructure-as-decorator/lambda-api-decorators). Define API routes with Python decorators and let CDK create the Lambda functions, integrations, routes, and related configuration.
|
|
51
|
+
|
|
52
|
+
The complete cross-package guides and architecture documentation are available at [lambda-api-decorators.github.io](https://infrastructure-as-decorator.github.io/).
|
|
53
|
+
|
|
54
|
+
## Installation
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
pip install lambda-api-decorators-cdk
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
The package requires Python 3.10 or newer and AWS CDK v2. The `lambda-api-decorators` package provides the decorators used by the Lambda source files.
|
|
61
|
+
|
|
62
|
+
## Quick start
|
|
63
|
+
|
|
64
|
+
`LambdaApi` is the recommended high-level construct. It creates a REST API by default and discovers decorated handlers below `lambda_path`.
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from aws_cdk import Stack
|
|
68
|
+
from constructs import Construct
|
|
69
|
+
|
|
70
|
+
from lambda_api_decorators_cdk import LambdaApi
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class ExampleStack(Stack):
|
|
74
|
+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
|
|
75
|
+
super().__init__(scope, construct_id, **kwargs)
|
|
76
|
+
|
|
77
|
+
LambdaApi(self, "ExampleApi", lambda_path="lambdas")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A handler must include an HTTP method decorator from `lambda-api-decorators`:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from lambda_api_decorators import GET
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@GET("/hello")
|
|
87
|
+
def hello(event, context):
|
|
88
|
+
return {"statusCode": 200, "body": "Hello, world!"}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`lambda_path` is resolved from the directory where CDK is run. Files without a supported route decorator are ignored.
|
|
92
|
+
|
|
93
|
+
## API types and imported APIs
|
|
94
|
+
|
|
95
|
+
REST is the default. Select an HTTP API with `ApiType.HTTP`, or provide an existing supported API and let the construct infer its type.
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
from aws_cdk import aws_apigateway as apigateway
|
|
99
|
+
from lambda_api_decorators_cdk import ApiType, LambdaApi
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
LambdaApi(self, "HttpApi", lambda_path="lambdas", api_type=ApiType.HTTP)
|
|
103
|
+
|
|
104
|
+
existing_rest_api = apigateway.RestApi(self, "ExistingRestApi")
|
|
105
|
+
LambdaApi(self, "ExistingApiRoutes", lambda_path="lambdas", api=existing_rest_api)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Created and imported REST APIs are supported. Concrete `aws_apigatewayv2.HttpApi` instances are supported for HTTP APIs.
|
|
109
|
+
|
|
110
|
+
## Reusable configuration
|
|
111
|
+
|
|
112
|
+
Use `LambdaApiConfig` for defaults and named resources shared by one or more `LambdaApi` constructs. Configuration must be complete before constructing the API.
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
from aws_cdk import Duration, aws_lambda as lambda_
|
|
116
|
+
from lambda_api_decorators_cdk import LambdaApi, LambdaApiConfig
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
config = LambdaApiConfig(
|
|
120
|
+
runtime=lambda_.Runtime.PYTHON_3_12,
|
|
121
|
+
timeout=Duration.seconds(30),
|
|
122
|
+
memory_size=512,
|
|
123
|
+
environment={"ENVIRONMENT": "production"},
|
|
124
|
+
)
|
|
125
|
+
config.add_custom_environment("PAYMENTS_URL", "https://payments.example.com")
|
|
126
|
+
|
|
127
|
+
LambdaApi(self, "ConfiguredApi", lambda_path="lambdas", config=config)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`LambdaApiConfig` supports default and custom runtimes, roles, VPCs, subnets, layers, security groups, and environment variables. It also supports registries for DynamoDB tables, S3 buckets, and REST or HTTP authorizers. A reused configuration creates an isolated builder snapshot for each API.
|
|
131
|
+
|
|
132
|
+
## Source layout and layers
|
|
133
|
+
|
|
134
|
+
By default, each handler is packaged relative to the source root. For a service-oriented layout, use `SourceLayout.SERVICE`:
|
|
135
|
+
|
|
136
|
+
```python
|
|
137
|
+
from lambda_api_decorators_cdk import LambdaApi, SourceLayout
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
LambdaApi(
|
|
141
|
+
self,
|
|
142
|
+
"ServiceApi",
|
|
143
|
+
lambda_path="services",
|
|
144
|
+
source_layout=SourceLayout.SERVICE,
|
|
145
|
+
layers_path="layers",
|
|
146
|
+
)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
When `layers_path` is provided, each visible child directory is treated as a discoverable layer source. Explicit layer configuration remains available through `LambdaApiConfig`.
|
|
150
|
+
|
|
151
|
+
## Authorization and permissions
|
|
152
|
+
|
|
153
|
+
Register authorizers with a logical key and select the default authorizer for routes. A handler can override the default with `@authorizer("key")` or make itself public with `@public`.
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
from aws_cdk import aws_apigateway as apigateway
|
|
157
|
+
from lambda_api_decorators_cdk import LambdaApiConfig
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
authorizer = apigateway.TokenAuthorizer(self, "UsersAuthorizer", handler=authorizer_fn)
|
|
161
|
+
config = LambdaApiConfig(authorizers={"users": authorizer}, default_authorizer="users")
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
The resource registries also allow decorated handlers to request least-privilege grants for registered DynamoDB tables and S3 buckets. See the [complete documentation](https://lambda-api-decorators.github.io/) for decorator syntax and supported access levels.
|
|
165
|
+
|
|
166
|
+
## Low-level builder
|
|
167
|
+
|
|
168
|
+
`ResourceBuilder` remains a supported lower-level API for callers that need direct control over the API root or build lifecycle:
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
from aws_cdk import Duration
|
|
172
|
+
from lambda_api_decorators_cdk import ResourceBuilder
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
builder = ResourceBuilder()
|
|
176
|
+
builder.set_default_timeout(Duration.seconds(30))
|
|
177
|
+
builder.add_common_environment("ENVIRONMENT", "production")
|
|
178
|
+
builder.build(self, rest_api.root, "lambdas")
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Prefer `LambdaApi` for new code. It owns API creation or reuse and delegates option resolution to isolated `ResourceBuilder` snapshots.
|
|
182
|
+
|
|
183
|
+
## Releases
|
|
184
|
+
|
|
185
|
+
The Git tag is the single source of truth for this package's version. For example, `v0.3.0` produces Python package version `0.3.0`. This repository is versioned independently from `lambda-api-decorators`.
|
|
186
|
+
|
|
187
|
+
To release from `main`, push a semantic version tag:
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
git checkout main
|
|
191
|
+
git pull
|
|
192
|
+
git tag v0.3.0
|
|
193
|
+
git push origin v0.3.0
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Pushing the tag triggers the release workflow, which tests, builds, verifies the version, and publishes to PyPI using trusted publishing. The PyPI project must have a trusted publisher configured for this repository, the `release.yml` workflow, and the `pypi` GitHub environment.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Lambda API Decorators CDK
|
|
2
|
+
|
|
3
|
+
AWS CDK integration for [Lambda API Decorators](https://github.com/infrastructure-as-decorator/lambda-api-decorators). Define API routes with Python decorators and let CDK create the Lambda functions, integrations, routes, and related configuration.
|
|
4
|
+
|
|
5
|
+
The complete cross-package guides and architecture documentation are available at [lambda-api-decorators.github.io](https://infrastructure-as-decorator.github.io/).
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install lambda-api-decorators-cdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
The package requires Python 3.10 or newer and AWS CDK v2. The `lambda-api-decorators` package provides the decorators used by the Lambda source files.
|
|
14
|
+
|
|
15
|
+
## Quick start
|
|
16
|
+
|
|
17
|
+
`LambdaApi` is the recommended high-level construct. It creates a REST API by default and discovers decorated handlers below `lambda_path`.
|
|
18
|
+
|
|
19
|
+
```python
|
|
20
|
+
from aws_cdk import Stack
|
|
21
|
+
from constructs import Construct
|
|
22
|
+
|
|
23
|
+
from lambda_api_decorators_cdk import LambdaApi
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ExampleStack(Stack):
|
|
27
|
+
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
|
|
28
|
+
super().__init__(scope, construct_id, **kwargs)
|
|
29
|
+
|
|
30
|
+
LambdaApi(self, "ExampleApi", lambda_path="lambdas")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
A handler must include an HTTP method decorator from `lambda-api-decorators`:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from lambda_api_decorators import GET
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@GET("/hello")
|
|
40
|
+
def hello(event, context):
|
|
41
|
+
return {"statusCode": 200, "body": "Hello, world!"}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`lambda_path` is resolved from the directory where CDK is run. Files without a supported route decorator are ignored.
|
|
45
|
+
|
|
46
|
+
## API types and imported APIs
|
|
47
|
+
|
|
48
|
+
REST is the default. Select an HTTP API with `ApiType.HTTP`, or provide an existing supported API and let the construct infer its type.
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from aws_cdk import aws_apigateway as apigateway
|
|
52
|
+
from lambda_api_decorators_cdk import ApiType, LambdaApi
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
LambdaApi(self, "HttpApi", lambda_path="lambdas", api_type=ApiType.HTTP)
|
|
56
|
+
|
|
57
|
+
existing_rest_api = apigateway.RestApi(self, "ExistingRestApi")
|
|
58
|
+
LambdaApi(self, "ExistingApiRoutes", lambda_path="lambdas", api=existing_rest_api)
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Created and imported REST APIs are supported. Concrete `aws_apigatewayv2.HttpApi` instances are supported for HTTP APIs.
|
|
62
|
+
|
|
63
|
+
## Reusable configuration
|
|
64
|
+
|
|
65
|
+
Use `LambdaApiConfig` for defaults and named resources shared by one or more `LambdaApi` constructs. Configuration must be complete before constructing the API.
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from aws_cdk import Duration, aws_lambda as lambda_
|
|
69
|
+
from lambda_api_decorators_cdk import LambdaApi, LambdaApiConfig
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
config = LambdaApiConfig(
|
|
73
|
+
runtime=lambda_.Runtime.PYTHON_3_12,
|
|
74
|
+
timeout=Duration.seconds(30),
|
|
75
|
+
memory_size=512,
|
|
76
|
+
environment={"ENVIRONMENT": "production"},
|
|
77
|
+
)
|
|
78
|
+
config.add_custom_environment("PAYMENTS_URL", "https://payments.example.com")
|
|
79
|
+
|
|
80
|
+
LambdaApi(self, "ConfiguredApi", lambda_path="lambdas", config=config)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
`LambdaApiConfig` supports default and custom runtimes, roles, VPCs, subnets, layers, security groups, and environment variables. It also supports registries for DynamoDB tables, S3 buckets, and REST or HTTP authorizers. A reused configuration creates an isolated builder snapshot for each API.
|
|
84
|
+
|
|
85
|
+
## Source layout and layers
|
|
86
|
+
|
|
87
|
+
By default, each handler is packaged relative to the source root. For a service-oriented layout, use `SourceLayout.SERVICE`:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from lambda_api_decorators_cdk import LambdaApi, SourceLayout
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
LambdaApi(
|
|
94
|
+
self,
|
|
95
|
+
"ServiceApi",
|
|
96
|
+
lambda_path="services",
|
|
97
|
+
source_layout=SourceLayout.SERVICE,
|
|
98
|
+
layers_path="layers",
|
|
99
|
+
)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
When `layers_path` is provided, each visible child directory is treated as a discoverable layer source. Explicit layer configuration remains available through `LambdaApiConfig`.
|
|
103
|
+
|
|
104
|
+
## Authorization and permissions
|
|
105
|
+
|
|
106
|
+
Register authorizers with a logical key and select the default authorizer for routes. A handler can override the default with `@authorizer("key")` or make itself public with `@public`.
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from aws_cdk import aws_apigateway as apigateway
|
|
110
|
+
from lambda_api_decorators_cdk import LambdaApiConfig
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
authorizer = apigateway.TokenAuthorizer(self, "UsersAuthorizer", handler=authorizer_fn)
|
|
114
|
+
config = LambdaApiConfig(authorizers={"users": authorizer}, default_authorizer="users")
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The resource registries also allow decorated handlers to request least-privilege grants for registered DynamoDB tables and S3 buckets. See the [complete documentation](https://lambda-api-decorators.github.io/) for decorator syntax and supported access levels.
|
|
118
|
+
|
|
119
|
+
## Low-level builder
|
|
120
|
+
|
|
121
|
+
`ResourceBuilder` remains a supported lower-level API for callers that need direct control over the API root or build lifecycle:
|
|
122
|
+
|
|
123
|
+
```python
|
|
124
|
+
from aws_cdk import Duration
|
|
125
|
+
from lambda_api_decorators_cdk import ResourceBuilder
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
builder = ResourceBuilder()
|
|
129
|
+
builder.set_default_timeout(Duration.seconds(30))
|
|
130
|
+
builder.add_common_environment("ENVIRONMENT", "production")
|
|
131
|
+
builder.build(self, rest_api.root, "lambdas")
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Prefer `LambdaApi` for new code. It owns API creation or reuse and delegates option resolution to isolated `ResourceBuilder` snapshots.
|
|
135
|
+
|
|
136
|
+
## Releases
|
|
137
|
+
|
|
138
|
+
The Git tag is the single source of truth for this package's version. For example, `v0.3.0` produces Python package version `0.3.0`. This repository is versioned independently from `lambda-api-decorators`.
|
|
139
|
+
|
|
140
|
+
To release from `main`, push a semantic version tag:
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
git checkout main
|
|
144
|
+
git pull
|
|
145
|
+
git tag v0.3.0
|
|
146
|
+
git push origin v0.3.0
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Pushing the tag triggers the release workflow, which tests, builds, verifies the version, and publishes to PyPI using trusted publishing. The PyPI project must have a trusted publisher configured for this repository, the `release.yml` workflow, and the `pypi` GitHub environment.
|
|
@@ -27,7 +27,7 @@ description = "AWS CDK integration for Lambda API Decorators. Generate AWS Lambd
|
|
|
27
27
|
readme = "README.md"
|
|
28
28
|
|
|
29
29
|
# https://packaging.python.org/guides/distributing-packages-using-setuptools/#python-requires
|
|
30
|
-
requires-python = ">=3.
|
|
30
|
+
requires-python = ">=3.10"
|
|
31
31
|
|
|
32
32
|
|
|
33
33
|
# https://packaging.python.org/en/latest/specifications/core-metadata/#license
|
|
@@ -54,10 +54,11 @@ classifiers = [
|
|
|
54
54
|
'Topic :: Software Development :: Libraries :: Python Modules',
|
|
55
55
|
'Topic :: Software Development :: Build Tools',
|
|
56
56
|
'License :: OSI Approved :: MIT License',
|
|
57
|
-
'Programming Language :: Python :: 3.9',
|
|
58
57
|
'Programming Language :: Python :: 3.10',
|
|
59
58
|
'Programming Language :: Python :: 3.11',
|
|
60
|
-
'Programming Language :: Python :: 3.12'
|
|
59
|
+
'Programming Language :: Python :: 3.12',
|
|
60
|
+
'Programming Language :: Python :: 3.13',
|
|
61
|
+
'Programming Language :: Python :: 3.14'
|
|
61
62
|
]
|
|
62
63
|
|
|
63
64
|
# For an analysis of this field vs pip's requirements files see:
|
|
@@ -256,6 +256,8 @@ def _validate_permission(invocation):
|
|
|
256
256
|
|
|
257
257
|
|
|
258
258
|
def _parse_invocation(decorator):
|
|
259
|
+
if isinstance(decorator, ast.Name) and decorator.id == "public":
|
|
260
|
+
return DecoratorInvocation(name="public", args=(), kwargs=())
|
|
259
261
|
if not isinstance(decorator, ast.Call) or not isinstance(decorator.func, ast.Name):
|
|
260
262
|
return None
|
|
261
263
|
name = decorator.func.id
|
|
@@ -275,6 +277,12 @@ def _parse_invocation(decorator):
|
|
|
275
277
|
_validate_grant(invocation, "bucket_name")
|
|
276
278
|
elif name == "permission":
|
|
277
279
|
invocation = _validate_permission(invocation)
|
|
280
|
+
elif name == "authorizer":
|
|
281
|
+
if len(invocation.args) != 1 or invocation.kwargs:
|
|
282
|
+
raise ValueError("authorizer requires exactly one positional key")
|
|
283
|
+
_require_non_empty_string(invocation.args[0], name, "key")
|
|
284
|
+
elif name == "public":
|
|
285
|
+
raise ValueError("public must be used as a bare decorator")
|
|
278
286
|
return invocation
|
|
279
287
|
|
|
280
288
|
|
|
@@ -301,6 +309,19 @@ def get_file_nodes(parsed_tree, id, directory):
|
|
|
301
309
|
is_lambda_http = True
|
|
302
310
|
paths.append((path, invocation.name))
|
|
303
311
|
|
|
312
|
+
auth_invocations = [
|
|
313
|
+
invocation for invocation in decorator_invocations
|
|
314
|
+
if invocation.name in ("authorizer", "public")
|
|
315
|
+
]
|
|
316
|
+
if len(auth_invocations) > 1:
|
|
317
|
+
raise ValueError(
|
|
318
|
+
"Conflicting or repeated authentication decorators are not allowed"
|
|
319
|
+
)
|
|
320
|
+
if len(paths) > 1 and auth_invocations:
|
|
321
|
+
raise ValueError(
|
|
322
|
+
"Authentication on a handler with multiple routes is ambiguous"
|
|
323
|
+
)
|
|
324
|
+
|
|
304
325
|
#ast.FunctionDef ends. If the Function had an HTTP Decorator it means it's a lambda function
|
|
305
326
|
if is_lambda_http:
|
|
306
327
|
for key,value in paths:
|
|
@@ -27,6 +27,8 @@ class LambdaApiConfig:
|
|
|
27
27
|
environment: Optional[Mapping[str, str]] = None,
|
|
28
28
|
dynamodb_tables: Optional[Mapping[str, dynamodb.ITable]] = None,
|
|
29
29
|
s3_buckets: Optional[Mapping[str, s3.IBucket]] = None,
|
|
30
|
+
authorizers: Optional[Mapping[str, object]] = None,
|
|
31
|
+
default_authorizer: Optional[str] = None,
|
|
30
32
|
) -> None:
|
|
31
33
|
self._default_runtime = runtime
|
|
32
34
|
self._default_timeout = timeout
|
|
@@ -49,10 +51,15 @@ class LambdaApiConfig:
|
|
|
49
51
|
|
|
50
52
|
self._dynamodb_tables = {}
|
|
51
53
|
self._s3_buckets = {}
|
|
54
|
+
self._authorizers = {}
|
|
52
55
|
for key, table in (dynamodb_tables or {}).items():
|
|
53
56
|
self.add_dynamodb_table(key, table)
|
|
54
57
|
for key, bucket in (s3_buckets or {}).items():
|
|
55
58
|
self.add_s3_bucket(key, bucket)
|
|
59
|
+
for key, authorizer in (authorizers or {}).items():
|
|
60
|
+
self.add_authorizer(key, authorizer)
|
|
61
|
+
self._default_authorizer = None
|
|
62
|
+
self.set_default_authorizer(default_authorizer)
|
|
56
63
|
|
|
57
64
|
def set_default_runtime(self, runtime: Optional[lambda_.Runtime]) -> None:
|
|
58
65
|
self._default_runtime = runtime
|
|
@@ -117,6 +124,35 @@ class LambdaApiConfig:
|
|
|
117
124
|
def add_s3_bucket(self, key: str, bucket: s3.IBucket) -> None:
|
|
118
125
|
self._add_resource(key, bucket, self._s3_buckets, "S3 bucket")
|
|
119
126
|
|
|
127
|
+
def add_authorizer(self, key: str, authorizer: object) -> None:
|
|
128
|
+
if not isinstance(key, str):
|
|
129
|
+
raise TypeError("Authorizer registry keys must be strings")
|
|
130
|
+
if not key.strip():
|
|
131
|
+
raise ValueError("Authorizer registry keys must not be empty or whitespace")
|
|
132
|
+
interfaces = getattr(authorizer, "__jsii_ifaces__", ()) if authorizer is not None else ()
|
|
133
|
+
supported = {
|
|
134
|
+
"aws_cdk.aws_apigateway.IAuthorizer",
|
|
135
|
+
"aws_cdk.aws_apigatewayv2.IHttpRouteAuthorizer",
|
|
136
|
+
}
|
|
137
|
+
if not any(f"{interface.__module__}.{interface.__name__}" in supported
|
|
138
|
+
for interface in interfaces):
|
|
139
|
+
raise TypeError("Authorizer must be a CDK authorizer object")
|
|
140
|
+
if key in self._authorizers:
|
|
141
|
+
raise ValueError(f"Authorizer key {key!r} is already registered")
|
|
142
|
+
self._authorizers[key] = authorizer
|
|
143
|
+
|
|
144
|
+
def set_default_authorizer(self, key: Optional[str]) -> None:
|
|
145
|
+
if key is None:
|
|
146
|
+
self._default_authorizer = None
|
|
147
|
+
return
|
|
148
|
+
if not isinstance(key, str):
|
|
149
|
+
raise TypeError("Default authorizer key must be a string or None")
|
|
150
|
+
if not key.strip():
|
|
151
|
+
raise ValueError("Default authorizer key must not be empty or whitespace")
|
|
152
|
+
if key not in self._authorizers:
|
|
153
|
+
raise KeyError(f"Authorizer key {key!r} is not registered")
|
|
154
|
+
self._default_authorizer = key
|
|
155
|
+
|
|
120
156
|
@staticmethod
|
|
121
157
|
def _add_resource(key: str, resource, registry: dict, resource_type: str) -> None:
|
|
122
158
|
if not isinstance(key, str):
|
|
@@ -148,4 +184,6 @@ class LambdaApiConfig:
|
|
|
148
184
|
custom_vpcs=dict(self._custom_vpcs),
|
|
149
185
|
dynamodb_tables=dict(self._dynamodb_tables),
|
|
150
186
|
s3_buckets=dict(self._s3_buckets),
|
|
187
|
+
authorizers=dict(self._authorizers),
|
|
188
|
+
default_authorizer=self._default_authorizer,
|
|
151
189
|
)
|