1
+ from pathlib import Path
1
2
from typing import Dict , Optional
2
3
3
- from aws_cdk import CfnOutput
4
+ from aws_cdk import CfnOutput , Duration , Expiration
4
5
from aws_cdk import aws_apigateway as apigwv1
5
6
from aws_cdk import aws_apigatewayv2_alpha as apigwv2
6
7
from aws_cdk import aws_apigatewayv2_authorizers_alpha as apigwv2authorizers
7
8
from aws_cdk import aws_apigatewayv2_integrations_alpha as apigwv2integrations
8
9
from aws_cdk import aws_ec2 as ec2
9
10
from aws_cdk import aws_elasticloadbalancingv2 as elbv2
10
11
from aws_cdk import aws_elasticloadbalancingv2_targets as targets
12
+ from aws_cdk import aws_appsync_alpha as appsync
13
+ from aws_cdk import aws_iam
11
14
from aws_cdk .aws_lambda import Function , FunctionUrlAuthType
12
15
13
16
from tests .e2e .utils .infrastructure import BaseInfrastructure
@@ -17,10 +20,11 @@ class EventHandlerStack(BaseInfrastructure):
17
20
def create_resources (self ):
18
21
functions = self .create_lambda_functions ()
19
22
20
- self ._create_alb (function = functions ["AlbHandler" ])
21
- self ._create_api_gateway_rest (function = functions ["ApiGatewayRestHandler" ])
22
- self ._create_api_gateway_http (function = functions ["ApiGatewayHttpHandler" ])
23
- self ._create_lambda_function_url (function = functions ["LambdaFunctionUrlHandler" ])
23
+ # self._create_alb(function=functions["AlbHandler"])
24
+ # self._create_api_gateway_rest(function=functions["ApiGatewayRestHandler"])
25
+ # self._create_api_gateway_http(function=functions["ApiGatewayHttpHandler"])
26
+ # self._create_lambda_function_url(function=functions["LambdaFunctionUrlHandler"])
27
+ self ._create_appsync_endpoint (function = functions ["AppsyncResolverHandler" ])
24
28
25
29
def _create_alb (self , function : Function ):
26
30
vpc = ec2 .Vpc .from_lookup (
@@ -84,3 +88,40 @@ def _create_lambda_function_url(self, function: Function):
84
88
# Maintenance: move auth to IAM when we create sigv4 builders
85
89
function_url = function .add_function_url (auth_type = FunctionUrlAuthType .AWS_IAM )
86
90
CfnOutput (self .stack , "LambdaFunctionUrl" , value = function_url .url )
91
+
92
+ def _create_appsync_endpoint (self , function : Function ):
93
+ api = appsync .GraphqlApi (
94
+ self .stack ,
95
+ "Api" ,
96
+ name = "e2e-tests" ,
97
+ schema = appsync .SchemaFile .from_asset (str (Path (self .feature_path , "files/schema.graphql" ))),
98
+ authorization_config = appsync .AuthorizationConfig (
99
+ default_authorization = appsync .AuthorizationMode (
100
+ authorization_type = appsync .AuthorizationType .API_KEY ,
101
+ api_key_config = appsync .ApiKeyConfig (
102
+ description = "public key for getting data" ,
103
+ expires = Expiration .after (Duration .hours (25 )),
104
+ name = "API Token" ,
105
+ ),
106
+ )
107
+ ),
108
+ xray_enabled = False ,
109
+ )
110
+ lambda_datasource = api .add_lambda_data_source ("DataSource" , lambda_function = function )
111
+
112
+ lambda_datasource .create_resolver (
113
+ "QueryGetAllPostsResolver" ,
114
+ type_name = "Query" ,
115
+ field_name = "allPosts" ,
116
+ )
117
+ lambda_datasource .create_resolver (
118
+ "QueryGetPostResolver" ,
119
+ type_name = "Query" ,
120
+ field_name = "getPost" ,
121
+ )
122
+ lambda_datasource .create_resolver (
123
+ "QueryGetPostRelatedResolver" , type_name = "Post" , field_name = "relatedPosts" , max_batch_size = 10
124
+ )
125
+
126
+ CfnOutput (self .stack , "GraphQLHTTPUrl" , value = api .graphql_url )
127
+ CfnOutput (self .stack , "GraphQLAPIKey" , value = api .api_key )
0 commit comments