| 7 | |
| 8 | |
| 9 | class ApiCorsLambdaStack(Stack): |
| 10 | |
| 11 | def __init__(self, scope: Construct, id: str, **kwargs) -> None: |
| 12 | super().__init__(scope, id, **kwargs) |
| 13 | |
| 14 | base_lambda = _lambda.Function(self, 'ApiCorsLambda', |
| 15 | handler='lambda-handler.handler', |
| 16 | runtime=_lambda.Runtime.PYTHON_3_7, |
| 17 | code=_lambda.Code.from_asset('lambda')) |
| 18 | |
| 19 | base_api = _apigw.RestApi(self, 'ApiGatewayWithCors', |
| 20 | rest_api_name='ApiGatewayWithCors') |
| 21 | |
| 22 | example_entity = base_api.root.add_resource( |
| 23 | 'example', |
| 24 | default_cors_preflight_options=_apigw.CorsOptions( |
| 25 | allow_methods=['GET', 'OPTIONS'], |
| 26 | allow_origins=_apigw.Cors.ALL_ORIGINS) |
| 27 | ) |
| 28 | example_entity_lambda_integration = _apigw.LambdaIntegration( |
| 29 | base_lambda, |
| 30 | proxy=False, |
| 31 | integration_responses=[ |
| 32 | _apigw.IntegrationResponse( |
| 33 | status_code="200", |
| 34 | response_parameters={ |
| 35 | 'method.response.header.Access-Control-Allow-Origin': "'*'" |
| 36 | } |
| 37 | ) |
| 38 | ] |
| 39 | ) |
| 40 | example_entity.add_method( |
| 41 | 'GET', example_entity_lambda_integration, |
| 42 | method_responses=[ |
| 43 | _apigw.MethodResponse( |
| 44 | status_code="200", |
| 45 | response_parameters={ |
| 46 | 'method.response.header.Access-Control-Allow-Origin': True |
| 47 | } |
| 48 | ) |
| 49 | ] |
| 50 | ) |
| 51 | |
| 52 | |
| 53 | app = App() |