文章目录
aws(学习笔记第三十二课) 深入使用cdk
- 使用
cdk
生成aws API Gateway
+lambda
以及eventbridge
等等
学习内容:
- 使用
aws API Gateway
+lambda
- 使用
event bridge
练习producer
和consumer
1. 使用aws API Gateway
+lambda
1.1. 以前的练习
- 以前的例子
API Gateway + lambda这个例子中已经使用了手动创建,使用练习了aws API Gateway
+lambda
- 使用
cdk
来创建
这里,采用cdk
的方式来创建API Gateway
+lambda
。
代码链接 api-cors-lambda
1.2. 使用cdk
创建API Gateway
+ lambda
- 整体架构
- 代码解析
- 创建
lambda
函数
注意,这里没有创建base_lambda = _lambda.Function(self, 'ApiCorsLambda', handler='lambda-handler.handler', runtime=_lambda.Runtime.PYTHON_3_12, code=_lambda.Code.from_asset('lambda'))
VPC
,因为这里不需要显示的创建VPC
。
-
创建
API
并且添加resource
base_api = _apigw.RestApi(self, 'ApiGatewayWithCors', rest_api_name='ApiGatewayWithCors') example_entity = base_api.root.add_resource( 'example', default_cors_preflight_options=_apigw.CorsOptions( allow_methods=['GET', 'OPTIONS'], allow_origins=_apigw.Cors.ALL_ORIGINS)
-
创建
LambdaIntegration
将API
和lambda
进行绑定example_entity_lambda_integration = _apigw.LambdaIntegration( base_lambda, proxy=False, integration_responses=[ _apigw.IntegrationResponse( status_code="200", response_parameters={ 'method.response.header.Access-Control-Allow-Origin': "'*'" } ) ] )
-
对
API
加入method
example_entity.add_method( 'GET', example_entity_lambda_integration, method_responses=[ _apigw.MethodResponse( status_code="200", response_parameters={ 'm
-
- 创建