aws(学习笔记第三十二课) 深入使用cdk(API Gateway + event bridge)

aws(学习笔记第三十二课) 深入使用cdk

  • 使用cdk生成aws API Gateway+lambda以及eventbridge等等

学习内容:

  • 使用aws API Gateway+lambda
  • 使用event bridge练习producerconsumer

1. 使用aws API Gateway+lambda

1.1. 以前的练习

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)
        

        在这里插入图片描述

      • 创建LambdaIntegrationAPIlambda进行绑定

              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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值