lambda-api-decorators-cdk 0.2.3__py3-none-any.whl → 0.4.1__py3-none-any.whl
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/ast_helper.py +21 -0
- lambda_api_decorators_cdk/lambda_api_config.py +38 -0
- lambda_api_decorators_cdk/resource_builder.py +129 -6
- lambda_api_decorators_cdk-0.4.1.dist-info/METADATA +196 -0
- lambda_api_decorators_cdk-0.4.1.dist-info/RECORD +12 -0
- lambda_api_decorators_cdk-0.2.3.dist-info/METADATA +0 -144
- lambda_api_decorators_cdk-0.2.3.dist-info/RECORD +0 -12
- {lambda_api_decorators_cdk-0.2.3.dist-info → lambda_api_decorators_cdk-0.4.1.dist-info}/WHEEL +0 -0
- {lambda_api_decorators_cdk-0.2.3.dist-info → lambda_api_decorators_cdk-0.4.1.dist-info}/licenses/LICENSE +0 -0
- {lambda_api_decorators_cdk-0.2.3.dist-info → lambda_api_decorators_cdk-0.4.1.dist-info}/top_level.txt +0 -0
|
@@ -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
|
)
|
|
@@ -8,13 +8,16 @@ from aws_cdk import (
|
|
|
8
8
|
aws_apigatewayv2_integrations as integrations,
|
|
9
9
|
aws_lambda as lambda_,
|
|
10
10
|
aws_s3 as s3,
|
|
11
|
-
aws_lambda_python_alpha as _lambda_python
|
|
11
|
+
aws_lambda_python_alpha as _lambda_python,
|
|
12
|
+
Annotations)
|
|
12
13
|
import hashlib
|
|
14
|
+
import inspect
|
|
13
15
|
import os
|
|
14
16
|
from pathlib import Path
|
|
15
17
|
from lambda_api_decorators_cdk import ast_helper
|
|
16
18
|
from lambda_api_decorators_cdk.source_layout import SourceLayout
|
|
17
19
|
from typing import Optional, List, Dict
|
|
20
|
+
from constructs import Construct
|
|
18
21
|
|
|
19
22
|
class ResourceBuilder():
|
|
20
23
|
'''
|
|
@@ -42,10 +45,12 @@ class ResourceBuilder():
|
|
|
42
45
|
custom_vpcs = None, ####TODO
|
|
43
46
|
dynamodb_tables: Optional[Dict[str, dynamodb.ITable]] = None,
|
|
44
47
|
s3_buckets: Optional[Dict[str, s3.IBucket]] = None,
|
|
48
|
+
authorizers: Optional[Dict[str, object]] = None,
|
|
49
|
+
default_authorizer: Optional[str] = None,
|
|
45
50
|
) -> 'ResourceBuilder':
|
|
46
51
|
|
|
47
52
|
'''
|
|
48
|
-
Creates base instance of a Resource Builder. By default, there are no predetermined custom, common nor default settings with the exception of the following custom runtimes: python3.8, python3.9,
|
|
53
|
+
Creates base instance of a Resource Builder. By default, there are no predetermined custom, common nor default settings with the exception of the following custom runtimes: python3.8, python3.9, python3.10, python3.11, python3.12, python3.13, python3.14.
|
|
49
54
|
You can optionally specify arguments (Keep in mind some of them are constructs of the aws_cdk toolkit) such as:
|
|
50
55
|
@param default_runtime
|
|
51
56
|
@param default_timeout
|
|
@@ -83,6 +88,8 @@ class ResourceBuilder():
|
|
|
83
88
|
self.custom_vpcs = custom_vpcs if custom_vpcs is not None else {}
|
|
84
89
|
self.dynamodb_tables = dynamodb_tables if dynamodb_tables is not None else {}
|
|
85
90
|
self.s3_buckets = s3_buckets if s3_buckets is not None else {}
|
|
91
|
+
self.authorizers = authorizers if authorizers is not None else {}
|
|
92
|
+
self.default_authorizer = default_authorizer
|
|
86
93
|
self._physical_dynamodb_tables = {}
|
|
87
94
|
self._physical_s3_buckets = {}
|
|
88
95
|
|
|
@@ -91,6 +98,8 @@ class ResourceBuilder():
|
|
|
91
98
|
self.custom_runtimes.update({'python3.10':lambda_.Runtime.PYTHON_3_10})
|
|
92
99
|
self.custom_runtimes.update({'python3.11':lambda_.Runtime.PYTHON_3_11})
|
|
93
100
|
self.custom_runtimes.update({'python3.12':lambda_.Runtime.PYTHON_3_12})
|
|
101
|
+
self.custom_runtimes.update({'python3.13':lambda_.Runtime.PYTHON_3_13})
|
|
102
|
+
self.custom_runtimes.update({'python3.14':lambda_.Runtime.PYTHON_3_14})
|
|
94
103
|
|
|
95
104
|
|
|
96
105
|
#Setters
|
|
@@ -236,6 +245,7 @@ class ResourceBuilder():
|
|
|
236
245
|
if print_tree:
|
|
237
246
|
ast_helper.dump_tree(graph)
|
|
238
247
|
self._prepare_layers(construct, graph, layer_sources)
|
|
248
|
+
self._emit_authorization_diagnostics(construct, graph)
|
|
239
249
|
self.build_from_graph(
|
|
240
250
|
construct, graph, api_resource, lambda_root, source_layout)
|
|
241
251
|
|
|
@@ -257,6 +267,7 @@ class ResourceBuilder():
|
|
|
257
267
|
if print_tree:
|
|
258
268
|
ast_helper.dump_tree(graph)
|
|
259
269
|
self._prepare_layers(construct, graph, layer_sources)
|
|
270
|
+
self._emit_authorization_diagnostics(construct, graph)
|
|
260
271
|
self.build_http_from_graph(
|
|
261
272
|
construct, graph, http_api, lambda_root, source_layout)
|
|
262
273
|
|
|
@@ -493,6 +504,114 @@ class ResourceBuilder():
|
|
|
493
504
|
decorators[invocation.name] = value
|
|
494
505
|
return decorators
|
|
495
506
|
|
|
507
|
+
@staticmethod
|
|
508
|
+
def _auth_override(method: ast_helper.Method):
|
|
509
|
+
"""Return inherit, public, or an explicit logical authorizer key."""
|
|
510
|
+
for invocation in method.get_decorator_invocations():
|
|
511
|
+
if invocation.name == "public":
|
|
512
|
+
return ("public", None)
|
|
513
|
+
if invocation.name == "authorizer":
|
|
514
|
+
return ("authorizer", invocation.args[0])
|
|
515
|
+
return ("inherit", None)
|
|
516
|
+
|
|
517
|
+
def _effective_authorizer_key(self, method: ast_helper.Method):
|
|
518
|
+
state, key = self._auth_override(method)
|
|
519
|
+
if state == "public":
|
|
520
|
+
return None
|
|
521
|
+
return key if state == "authorizer" else self.default_authorizer
|
|
522
|
+
|
|
523
|
+
@staticmethod
|
|
524
|
+
def _implements_interface(authorizer, interface) -> bool:
|
|
525
|
+
return interface in getattr(authorizer, "__jsii_ifaces__", ())
|
|
526
|
+
|
|
527
|
+
def _resolve_authorizer(self, method: ast_helper.Method, family: str):
|
|
528
|
+
key = self._effective_authorizer_key(method)
|
|
529
|
+
if key is None:
|
|
530
|
+
return None
|
|
531
|
+
try:
|
|
532
|
+
authorizer = self.authorizers[key]
|
|
533
|
+
except KeyError:
|
|
534
|
+
raise KeyError(f"Authorizer key {key!r} is not registered") from None
|
|
535
|
+
interface = (
|
|
536
|
+
apigateway.IAuthorizer
|
|
537
|
+
if family == "REST"
|
|
538
|
+
else apigateway2.IHttpRouteAuthorizer
|
|
539
|
+
)
|
|
540
|
+
if not self._implements_interface(authorizer, interface):
|
|
541
|
+
raise TypeError(
|
|
542
|
+
f"Authorizer key {key!r} is not compatible with {family} APIs"
|
|
543
|
+
)
|
|
544
|
+
return authorizer
|
|
545
|
+
|
|
546
|
+
def _rest_method_options(self, method: ast_helper.Method):
|
|
547
|
+
authorizer = self._resolve_authorizer(method, "REST")
|
|
548
|
+
if authorizer is None:
|
|
549
|
+
return {
|
|
550
|
+
"authorizer": None,
|
|
551
|
+
"authorization_type": apigateway.AuthorizationType.NONE,
|
|
552
|
+
}
|
|
553
|
+
return {
|
|
554
|
+
"authorizer": authorizer,
|
|
555
|
+
"authorization_type": authorizer.authorization_type,
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
def _add_rest_method(self, resource, method, integration):
|
|
559
|
+
options = self._rest_method_options(method)
|
|
560
|
+
parameters = inspect.signature(resource.add_method).parameters.values()
|
|
561
|
+
accepts_options = any(
|
|
562
|
+
parameter.kind is inspect.Parameter.VAR_KEYWORD
|
|
563
|
+
or parameter.name == "authorization_type"
|
|
564
|
+
for parameter in parameters
|
|
565
|
+
)
|
|
566
|
+
if accepts_options:
|
|
567
|
+
return resource.add_method(method.get_method(), integration, **options)
|
|
568
|
+
# Preserve compatibility with narrow direct-builder test doubles and
|
|
569
|
+
# existing callers that expose only the historical two-argument shape.
|
|
570
|
+
return resource.add_method(method.get_method(), integration)
|
|
571
|
+
|
|
572
|
+
def _http_route_authorizer(self, method: ast_helper.Method):
|
|
573
|
+
authorizer = self._resolve_authorizer(method, "HTTP")
|
|
574
|
+
return authorizer if authorizer is not None else apigateway2.HttpNoneAuthorizer()
|
|
575
|
+
|
|
576
|
+
def _emit_authorization_diagnostics(self, construct, graph):
|
|
577
|
+
if not isinstance(construct, Construct):
|
|
578
|
+
return
|
|
579
|
+
if self.default_authorizer is None:
|
|
580
|
+
Annotations.of(construct).add_warning_v2(
|
|
581
|
+
"LAD_AUTH_PUBLIC_DEFAULT",
|
|
582
|
+
"LAD_AUTH_PUBLIC_DEFAULT: Routes without explicit authorization are public because no "
|
|
583
|
+
"default authorizer is configured.",
|
|
584
|
+
)
|
|
585
|
+
|
|
586
|
+
deviations = []
|
|
587
|
+
|
|
588
|
+
def visit(resource):
|
|
589
|
+
for method in resource.get_methods():
|
|
590
|
+
state, key = self._auth_override(method)
|
|
591
|
+
if self.default_authorizer is None:
|
|
592
|
+
if state == "authorizer":
|
|
593
|
+
deviations.append((resource.get_path(), method.get_method(), key))
|
|
594
|
+
elif state == "public":
|
|
595
|
+
deviations.append((resource.get_path(), method.get_method(), "PUBLIC"))
|
|
596
|
+
elif state == "authorizer" and key != self.default_authorizer:
|
|
597
|
+
deviations.append((resource.get_path(), method.get_method(), key))
|
|
598
|
+
for child in resource.get_connections():
|
|
599
|
+
visit(child)
|
|
600
|
+
|
|
601
|
+
visit(graph)
|
|
602
|
+
if not deviations:
|
|
603
|
+
return
|
|
604
|
+
deviations.sort(key=lambda item: (item[0], item[1]))
|
|
605
|
+
if self.default_authorizer is None:
|
|
606
|
+
heading = "Default authorizer: PUBLIC\n\nProtected routes:"
|
|
607
|
+
else:
|
|
608
|
+
heading = f"Default authorizer: {self.default_authorizer}\n\nOverrides:"
|
|
609
|
+
details = "\n".join(
|
|
610
|
+
f" {method} {path} {value}"
|
|
611
|
+
for path, method, value in deviations
|
|
612
|
+
)
|
|
613
|
+
Annotations.of(construct).add_info(f"{heading}\n{details}")
|
|
614
|
+
|
|
496
615
|
@staticmethod
|
|
497
616
|
def _physical_resource_id(resource_type: str, physical_name: str) -> str:
|
|
498
617
|
"""Create a stable, collision-safe construct ID for an imported resource."""
|
|
@@ -734,7 +853,8 @@ class ResourceBuilder():
|
|
|
734
853
|
for method in graph.get_methods():
|
|
735
854
|
lbda = self._build_discovered_lambda(
|
|
736
855
|
construct, method, lambda_root, source_layout)
|
|
737
|
-
|
|
856
|
+
self._add_rest_method(
|
|
857
|
+
new_resource, method, apigateway.LambdaIntegration(lbda))
|
|
738
858
|
else:
|
|
739
859
|
#We can get a skip from /something to /something/one/two/method, so resources with no methods "one" and "two" should be created
|
|
740
860
|
new_api_resources = path[len(api_resource.path):].lstrip('/').split('/')
|
|
@@ -746,7 +866,8 @@ class ResourceBuilder():
|
|
|
746
866
|
for method in graph.get_methods():
|
|
747
867
|
lbda = self._build_discovered_lambda(
|
|
748
868
|
construct, method, lambda_root, source_layout)
|
|
749
|
-
|
|
869
|
+
self._add_rest_method(
|
|
870
|
+
new_resource, method, apigateway.LambdaIntegration(lbda))
|
|
750
871
|
|
|
751
872
|
for node in graph.get_connections():
|
|
752
873
|
self.build_from_graph(
|
|
@@ -776,7 +897,8 @@ class ResourceBuilder():
|
|
|
776
897
|
http_api.add_routes(
|
|
777
898
|
path='/',
|
|
778
899
|
methods=[method_mapping[method.get_method()]],
|
|
779
|
-
integration=
|
|
900
|
+
integration=api_lbda_integration,
|
|
901
|
+
authorizer=self._http_route_authorizer(method),
|
|
780
902
|
)
|
|
781
903
|
else:
|
|
782
904
|
for method in graph.get_methods():
|
|
@@ -786,7 +908,8 @@ class ResourceBuilder():
|
|
|
786
908
|
http_api.add_routes(
|
|
787
909
|
path=path,
|
|
788
910
|
methods=[method_mapping[method.get_method()]],
|
|
789
|
-
integration=
|
|
911
|
+
integration=api_lbda_integration,
|
|
912
|
+
authorizer=self._http_route_authorizer(method),
|
|
790
913
|
)
|
|
791
914
|
|
|
792
915
|
for node in graph.get_connections():
|
|
@@ -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,12 @@
|
|
|
1
|
+
lambda_api_decorators_cdk/__init__.py,sha256=LkPu_ySBeRlGKVYbeLV86H78kYFrEQHBuzmskZo3nf8,197
|
|
2
|
+
lambda_api_decorators_cdk/api_type.py,sha256=UjhwP6KBwILl7NpL17e4mL-Bi5JdcfDs3oq2bxoESL0,146
|
|
3
|
+
lambda_api_decorators_cdk/ast_helper.py,sha256=5aXh7dFjTKaBdEfO9Vd9l8HEOnO7WkgnXCQaUA-bmCU,15996
|
|
4
|
+
lambda_api_decorators_cdk/lambda_api.py,sha256=wqiwe10-XOuPG8kxn2YGcpk4IHag1WG6mMHFENLgRhc,3148
|
|
5
|
+
lambda_api_decorators_cdk/lambda_api_config.py,sha256=NWJPjAvFXhxjiFN0wCrWGyT3-UJ_jJJp12KDyQOzya4,7989
|
|
6
|
+
lambda_api_decorators_cdk/resource_builder.py,sha256=z3m_Qzq-c_r69CVWgn1FdLcN_XxOj8q_o3SsTLC_p4Q,41791
|
|
7
|
+
lambda_api_decorators_cdk/source_layout.py,sha256=rvw2D0UTOyhHmiCg_NTuxFf2wvsFCBugnwNKd3NLWLo,167
|
|
8
|
+
lambda_api_decorators_cdk-0.4.1.dist-info/licenses/LICENSE,sha256=kABKIBEzZQqtNkBsAA9tA_Ume6OKdVUnChslMCL2Baw,1081
|
|
9
|
+
lambda_api_decorators_cdk-0.4.1.dist-info/METADATA,sha256=D7jZG1z7ywoK4eAh-QyYCbLpUBOvqXp_Nn_WEZfn_so,8181
|
|
10
|
+
lambda_api_decorators_cdk-0.4.1.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
11
|
+
lambda_api_decorators_cdk-0.4.1.dist-info/top_level.txt,sha256=1goCGc19BssxULn6ejZNLDYP0sQHEyqCMhivbGjxDCE,26
|
|
12
|
+
lambda_api_decorators_cdk-0.4.1.dist-info/RECORD,,
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: lambda-api-decorators-cdk
|
|
3
|
-
Version: 0.2.3
|
|
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.9
|
|
38
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
39
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
40
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
41
|
-
Requires-Python: >=3.9
|
|
42
|
-
Description-Content-Type: text/markdown
|
|
43
|
-
License-File: LICENSE
|
|
44
|
-
Requires-Dist: aws-cdk.aws-lambda-python-alpha
|
|
45
|
-
Dynamic: license-file
|
|
46
|
-
|
|
47
|
-
# Lambda API Decorators CDK
|
|
48
|
-
|
|
49
|
-
## A library for AWS API Gateway/Lambda Proxy Integration
|
|
50
|
-
|
|
51
|
-
Lambda API Decorators CDK allows simplified resource creation for AWS Lambda functions and Rest API resources by using decorators and setting a builder with default, common or custom values for IAM Roles, Runtimes, Timeouts, Layers, Environment values, etc. This project relies abstract syntactic trees (ast) to analyze the code of your lambda functions and generate infraestructure accordingly.
|
|
52
|
-
|
|
53
|
-
### Installation
|
|
54
|
-
|
|
55
|
-
`lambda_api_decorators_cdk` is available from PyPI as `lambda-api-decorators-cdk`:
|
|
56
|
-
|
|
57
|
-
pip install lambda-api-decorators-cdk
|
|
58
|
-
|
|
59
|
-
Installation of [lambda-api-decorators](https://pypi.org/project/lambda-api-decorators/) is also required as a dependency for your lambda functions, since it provides the definition of decorators used within this module.
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
### Example
|
|
63
|
-
|
|
64
|
-
```python
|
|
65
|
-
import lambda_api_decorators_cdk
|
|
66
|
-
or
|
|
67
|
-
from lambda_api_decorators_cdk import ResourceBuilder
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Builder instance
|
|
71
|
-
|
|
72
|
-
You may define a builder using lambda_api_decorators_cdk's constructor `ResourceBuilder`. This method returns an instance of the class that will be used to configure and create your Lambda Functions. By default, no parameters are required to instantiate the object, but custom options may be passed in advanced use cases.
|
|
73
|
-
|
|
74
|
-
```python
|
|
75
|
-
from lambda_api_decorators_cdk import ResourceBuilder
|
|
76
|
-
|
|
77
|
-
builder = ResourceBuilder()
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
### Builder Configuration
|
|
81
|
-
|
|
82
|
-
If opted to, you can set default values for IAM Roles, Memory Size, Timeout, Runtime and VPC.
|
|
83
|
-
|
|
84
|
-
On the same note, support for common configuration that all the Lambda Functions will receive, such as Security Groups, Environment variables and Layers, is provided.
|
|
85
|
-
|
|
86
|
-
Lastly you can setup custom environments, layers, security groups, vpcs a Lambda Function will receive ONLY if they have the decorators defined.
|
|
87
|
-
|
|
88
|
-
```python
|
|
89
|
-
from lambda_api_decorators_cdk import ResourceBuilder
|
|
90
|
-
from aws_cdk import Duration
|
|
91
|
-
|
|
92
|
-
builder = ResourceBuilder()
|
|
93
|
-
builder.set_default_timeout(Duration.seconds(30))
|
|
94
|
-
builder.add_common_environment("DATABASE_URI", "something-db-related")
|
|
95
|
-
builder.add_custom_environment("URL-PREFIX", "lambda-api-decorators-cdk-") #Lambda Function should have decorator @environment("URL-PREFIX")
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
### Building
|
|
99
|
-
|
|
100
|
-
Assuming you have already instantiated a Builder, configured it and ready to deploy your stack, then simply define the directory of your Lambda Functions and build!
|
|
101
|
-
|
|
102
|
-
Note: For a Lambda Function to be recognised and built, it has to have a decorator specifying the HTTP method it responds to. Decorators are defined in the [lambda-api-decorators](https://pypi.org/project/lambda-api-decorators/) package.
|
|
103
|
-
|
|
104
|
-
```python
|
|
105
|
-
[...] # Imports
|
|
106
|
-
|
|
107
|
-
class LambdaApiDecoratorsExampleStack(Stack):
|
|
108
|
-
|
|
109
|
-
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
|
|
110
|
-
super().__init__(scope, construct_id, **kwargs)
|
|
111
|
-
|
|
112
|
-
[...] # Instantiating builder, defining options and layers...
|
|
113
|
-
|
|
114
|
-
lambda_path = 'lambdas'
|
|
115
|
-
|
|
116
|
-
restapi = apigateway.RestApi(
|
|
117
|
-
self, 'lambda-api-decorators-RestApi',
|
|
118
|
-
rest_api_name= 'lambda-api-decorators-restApi')
|
|
119
|
-
root_resource = restapi.root
|
|
120
|
-
|
|
121
|
-
builder.build(self, root_resource, lambda_path, print_tree=True)
|
|
122
|
-
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
## Maintainer releases
|
|
126
|
-
|
|
127
|
-
The Git tag is the single source of truth for this package's version. For
|
|
128
|
-
example, `v0.3.0` produces Python package version `0.3.0`. This repository is
|
|
129
|
-
versioned independently from `lambda-api-decorators`.
|
|
130
|
-
|
|
131
|
-
To release from `main`, choose and push a semantic version tag:
|
|
132
|
-
|
|
133
|
-
```bash
|
|
134
|
-
git checkout main
|
|
135
|
-
git pull
|
|
136
|
-
|
|
137
|
-
git tag v0.3.0
|
|
138
|
-
git push origin v0.3.0
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
Pushing the tag triggers the release workflow, which tests, builds, verifies
|
|
142
|
-
the version, and publishes with PyPI trusted publishing. The PyPI project must
|
|
143
|
-
have a trusted publisher configured for this repository, the `release.yml`
|
|
144
|
-
workflow, and the `pypi` GitHub environment.
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
lambda_api_decorators_cdk/__init__.py,sha256=LkPu_ySBeRlGKVYbeLV86H78kYFrEQHBuzmskZo3nf8,197
|
|
2
|
-
lambda_api_decorators_cdk/api_type.py,sha256=UjhwP6KBwILl7NpL17e4mL-Bi5JdcfDs3oq2bxoESL0,146
|
|
3
|
-
lambda_api_decorators_cdk/ast_helper.py,sha256=zx1albQktG5D3Rlf5eXEJXiwoZsPF7qwEgTb_sATGEg,14978
|
|
4
|
-
lambda_api_decorators_cdk/lambda_api.py,sha256=wqiwe10-XOuPG8kxn2YGcpk4IHag1WG6mMHFENLgRhc,3148
|
|
5
|
-
lambda_api_decorators_cdk/lambda_api_config.py,sha256=Ngi0g-tN-M9ifv8xrX3UQqjA2RxtVmf6ejqhkYc2nNo,6103
|
|
6
|
-
lambda_api_decorators_cdk/resource_builder.py,sha256=IRYlILYasYywF1_kua-zNHYxG2jGJn4qF5QeAoJqFEQ,36350
|
|
7
|
-
lambda_api_decorators_cdk/source_layout.py,sha256=rvw2D0UTOyhHmiCg_NTuxFf2wvsFCBugnwNKd3NLWLo,167
|
|
8
|
-
lambda_api_decorators_cdk-0.2.3.dist-info/licenses/LICENSE,sha256=kABKIBEzZQqtNkBsAA9tA_Ume6OKdVUnChslMCL2Baw,1081
|
|
9
|
-
lambda_api_decorators_cdk-0.2.3.dist-info/METADATA,sha256=9vYl2q28NItUYeN16dmg4bzqKZgM1tF38KJ3Mf6DBq8,6434
|
|
10
|
-
lambda_api_decorators_cdk-0.2.3.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
11
|
-
lambda_api_decorators_cdk-0.2.3.dist-info/top_level.txt,sha256=1goCGc19BssxULn6ejZNLDYP0sQHEyqCMhivbGjxDCE,26
|
|
12
|
-
lambda_api_decorators_cdk-0.2.3.dist-info/RECORD,,
|
{lambda_api_decorators_cdk-0.2.3.dist-info → lambda_api_decorators_cdk-0.4.1.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|