22 aws_lambda as lambda_ ,
33 Duration
44)
5- from aws_cdk .aws_apigateway import Resource , LambdaIntegration
5+ from aws_cdk .aws_apigateway import Resource , LambdaIntegration , AuthorizationType , CognitoUserPoolsAuthorizer
66from constructs import Construct
77
88
99class LambdaStack (Construct ):
1010 functions_that_need_dynamo_permissions = []
1111
12- def __init__ (self , scope : Construct , api_gateway_resource : Resource , environment_variables : dict ) -> None :
12+ def __init__ (self , scope : Construct , api_gateway_resource : Resource , environment_variables : dict , user_pool ) -> None :
1313 super ().__init__ (scope , "KnowlyApiLambdas" )
1414
15+ # Authorizer Cognito (User Pool)
16+ self .authorizer = CognitoUserPoolsAuthorizer (
17+ self ,
18+ "KnowlyCognitoAuthorizer" ,
19+ cognito_user_pools = [user_pool ]
20+ )
21+
1522 self .lambda_layer = lambda_ .LayerVersion (self , "Knowly_Layer" ,
1623 code = lambda_ .Code .from_asset ("./lambda_layer_out_temp" ),
1724 compatible_runtimes = [lambda_ .Runtime .PYTHON_3_13 ]
@@ -24,7 +31,8 @@ def __init__(self, scope: Construct, api_gateway_resource: Resource, environment
2431 module_name = "get_user" ,
2532 http_method = "GET" ,
2633 target_resource = user_resource ,
27- environment_variables = environment_variables
34+ environment_variables = environment_variables ,
35+ requires_authorizer = True
2836 )
2937
3038 self .create_user_function = self ._add_method_to_resource (
@@ -38,14 +46,16 @@ def __init__(self, scope: Construct, api_gateway_resource: Resource, environment
3846 module_name = "delete_user" ,
3947 http_method = "DELETE" ,
4048 target_resource = user_resource ,
41- environment_variables = environment_variables
49+ environment_variables = environment_variables ,
50+ requires_authorizer = True
4251 )
4352
4453 self .update_user_function = self ._add_method_to_resource (
4554 module_name = "update_user" ,
4655 http_method = "PATCH" ,
4756 target_resource = user_resource ,
48- environment_variables = environment_variables
57+ environment_variables = environment_variables ,
58+ requires_authorizer = True
4959 )
5060
5161 # ---- Auth Resource ----
@@ -65,7 +75,8 @@ def __init__(self, scope: Construct, api_gateway_resource: Resource, environment
6575 module_name = "get_transactions_by_user" ,
6676 http_method = "GET" ,
6777 target_resource = transactions_resource ,
68- environment_variables = environment_variables
78+ environment_variables = environment_variables ,
79+ requires_authorizer = True
6980 )
7081
7182 # ---- Subscriptions Resource ----
@@ -75,14 +86,16 @@ def __init__(self, scope: Construct, api_gateway_resource: Resource, environment
7586 module_name = "get_subscriptions_by_user" ,
7687 http_method = "GET" ,
7788 target_resource = subscriptions_resource ,
78- environment_variables = environment_variables
89+ environment_variables = environment_variables ,
90+ requires_authorizer = True
7991 )
8092
8193 self .update_subscription_function = self ._add_method_to_resource (
8294 module_name = "update_subscription" ,
8395 http_method = "PUT" ,
8496 target_resource = subscriptions_resource ,
85- environment_variables = environment_variables
97+ environment_variables = environment_variables ,
98+ requires_authorizer = True
8699 )
87100
88101 self .functions_that_need_dynamo_permissions = [self .get_user_function , self .create_user_function ,
@@ -96,7 +109,8 @@ def _add_method_to_resource(
96109 module_name : str ,
97110 http_method : str ,
98111 target_resource : Resource ,
99- environment_variables : dict
112+ environment_variables : dict ,
113+ requires_authorizer : bool = False
100114 ) -> lambda_ .Function :
101115 fn = lambda_ .Function (
102116 self ,
@@ -109,5 +123,14 @@ def _add_method_to_resource(
109123 timeout = Duration .seconds (15 ),
110124 )
111125
112- target_resource .add_method (http_method , LambdaIntegration (fn ))
126+ integration = LambdaIntegration (fn )
127+ if requires_authorizer :
128+ target_resource .add_method (
129+ http_method ,
130+ integration ,
131+ authorization_type = AuthorizationType .COGNITO ,
132+ authorizer = self .authorizer
133+ )
134+ else :
135+ target_resource .add_method (http_method , integration )
113136 return fn
0 commit comments