Ejemplo n.º 1
0
 def add_cors_options(api_gateway_resource: aws_apigateway.Resource):
     api_gateway_resource.add_method(
         'OPTIONS',
         aws_apigateway.MockIntegration(integration_responses=[
             CDKMasterStack.get_options_integration_response()
         ],
                                        passthrough_behavior=aws_apigateway.
                                        PassthroughBehavior.WHEN_NO_MATCH,
                                        request_templates={
                                            "application/json":
                                            "{\"statusCode\":200}"
                                        }),
         method_responses=[CDKMasterStack.get_options_method_response()])
 def __add_resource_method(resource: apigw.Resource, http_method: str, integration: apigw.LambdaIntegration,
                           authorizer: apigw.CfnAuthorizer) -> None:
     method = resource.add_method(
         http_method=http_method,
         integration=integration,
         authorization_type=apigw.AuthorizationType.COGNITO,
     )
     method_resource: apigw.Resource = method.node.find_child("Resource")
     method_resource.add_property_override("AuthorizerId", {"Ref": authorizer.logical_id})
Ejemplo n.º 3
0
    def _add_generic_api_method(
            self, api_resource: aws_apigateway.Resource,
            integration: aws_apigateway.LambdaIntegration, methods: List[str],
            authorizer: Optional[aws_apigateway.CfnAuthorizer]):

        auth_type = None if authorizer is None else aws_apigateway.AuthorizationType.COGNITO
        for method in methods:
            api_method = api_resource.add_method(
                method,
                integration,
                method_responses=[self.get_method_response()],
                authorization_type=auth_type)

            if authorizer:
                api_method.node.find_child("Resource").add_property_override(
                    'AuthorizerId', authorizer.ref)
Ejemplo n.º 4
0
    def add_get_method(
        self,
        api: aws_apigateway.RestApi,
        resource: aws_apigateway.Resource,
        table: aws_dynamodb.Table,
    ) -> aws_apigateway.Method:
        list_announcements_lambda = create_function(
            stack=self,
            id="ListAnnouncementLambda",
            settings={
                "handler":
                "list_announcements.main",
                "runtime":
                aws_lambda.Runtime.PYTHON_3_8,
                "timeout":
                core.Duration.minutes(
                    self.settings.AWS_LAMBDA_GET_ANNOUNCEMENT_TIMEOUT),
                "retry_attempts":
                self.settings.AWS_LAMBDA_GET_ANNOUNCEMENT_RETRY_ATTEMPTS,
            },
        )
        table.grant_read_data(list_announcements_lambda)
        list_announcements_lambda.add_environment(
            "TABLE_NAME",
            table.table_name,
        )

        list_announcements_method = resource.add_method(
            "GET",
            integration=aws_apigateway.LambdaIntegration(
                list_announcements_lambda,
                proxy=True,
                integration_responses=[
                    aws_apigateway.IntegrationResponse(status_code="200", ),
                    aws_apigateway.IntegrationResponse(status_code="404", ),
                ],
            ),
        )
        self.methods_to_deploy.append(list_announcements_method)
        return list_announcements_method
Ejemplo n.º 5
0
    def add_post_method(
        self,
        api: aws_apigateway.RestApi,
        resource: aws_apigateway.Resource,
        table: aws_dynamodb.Table,
    ) -> aws_apigateway.Method:
        create_announcement_lambda = create_function(
            stack=self,
            id="CreateAnnouncementLambda",
            settings={
                "handler":
                "create_announcement.main",
                "runtime":
                aws_lambda.Runtime.PYTHON_3_8,
                "timeout":
                core.Duration.minutes(
                    self.settings.AWS_LAMBDA_CREATE_ANNOUNCEMENT_TIMEOUT),
                "retry_attempts":
                self.settings.AWS_LAMBDA_CREATE_ANNOUNCEMENT_RETRY_ATTEMPTS,
            },
        )
        create_announcement_lambda.add_environment(
            "TABLE_NAME",
            table.table_name,
        )

        table.grant_read_write_data(create_announcement_lambda)

        create_announcement_request_validator = aws_apigateway.RequestValidator(
            self,
            "CreateAnnouncementRequestValidator",
            rest_api=api,
            validate_request_body=True,
            request_validator_name="Create Announcement Request Validator",
        )

        create_announcement_request_model = aws_apigateway.Model(
            self,
            "CreateAnnouncementRequestModel",
            model_name="CreateAnnouncementRequest",
            rest_api=api,
            schema=aws_apigateway.JsonSchema(
                type=aws_apigateway.JsonSchemaType.OBJECT,
                required=["Item"],
                properties={
                    "Item":
                    aws_apigateway.JsonSchema(
                        type=aws_apigateway.JsonSchemaType.OBJECT,
                        required=["title", "date", "description"],
                        properties={
                            "title":
                            aws_apigateway.JsonSchema(
                                type=aws_apigateway.JsonSchemaType.STRING),
                            "description":
                            aws_apigateway.JsonSchema(
                                type=aws_apigateway.JsonSchemaType.STRING, ),
                            "date":
                            aws_apigateway.JsonSchema(
                                type=aws_apigateway.JsonSchemaType.STRING,
                                min_length=1,
                                format="date",
                                pattern=
                                "^\d{4}-([0]\d|1[0-2])-([0-2]\d|3[01])$",
                            ),
                        },
                    )
                },
            ),
        )

        create_announcement_response_success_model = aws_apigateway.Model(
            self,
            "CreateAnnouncementResponseSuccess",
            model_name="CreateAnnouncementResponseSuccess",
            rest_api=api,
            schema=aws_apigateway.JsonSchema(
                type=aws_apigateway.JsonSchemaType.OBJECT,
                required=["id"],
                properties={
                    "id":
                    aws_apigateway.JsonSchema(
                        type=aws_apigateway.JsonSchemaType.STRING)
                },
            ),
        )
        create_announcement_response_error_model = aws_apigateway.Model(
            self,
            "CreateAnnouncementResponseError",
            model_name="CreateAnnouncementResponseError",
            rest_api=api,
            schema=aws_apigateway.JsonSchema(
                type=aws_apigateway.JsonSchemaType.OBJECT,
                required=["error"],
                properties={
                    "error":
                    aws_apigateway.JsonSchema(
                        type=aws_apigateway.JsonSchemaType.STRING)
                },
            ),
        )

        create_announcement_method = resource.add_method(
            "POST",
            integration=aws_apigateway.LambdaIntegration(
                create_announcement_lambda,
                proxy=True,
                integration_responses=[
                    aws_apigateway.IntegrationResponse(
                        status_code="200",
                        response_parameters={
                            "method.response.header.Access-Control-Allow-Origin":
                            "'*'"
                        },
                    ),
                    aws_apigateway.IntegrationResponse(
                        status_code="404",
                        response_parameters={
                            "method.response.header.Access-Control-Allow-Origin":
                            "'*'"
                        },
                    ),
                ],
                passthrough_behavior=aws_apigateway.PassthroughBehavior.NEVER,
            ),
            request_validator=create_announcement_request_validator,
            request_models={
                "application/json": create_announcement_request_model
            },
            method_responses=[
                aws_apigateway.MethodResponse(
                    status_code="200",
                    response_models={
                        "application/json":
                        create_announcement_response_success_model
                    },
                    response_parameters={
                        "method.response.header.Access-Control-Allow-Origin":
                        True
                    },
                ),
                aws_apigateway.MethodResponse(
                    status_code="404",
                    response_models={
                        "application/json":
                        create_announcement_response_error_model
                    },
                    response_parameters={
                        "method.response.header.Access-Control-Allow-Origin":
                        True
                    },
                ),
            ],
        )

        self.methods_to_deploy.append(create_announcement_method)
        return create_announcement_method