CDK split API Gateway stack into 2 small stacks

后端 未结 3 2114
情话喂你
情话喂你 2021-02-09 07:27

I\'m trying to create a CDK stack in order to create API Gateway. Everything working as excepted if I create the stack in "small pieces" (comment part of the resources

3条回答
  •  爱一瞬间的悲伤
    2021-02-09 07:49

    I think you can achieve your goal by separating resources into smaller stacks. It doesn't seem like you need cross stack references or nested stacks.

    Here is an example (in Python) with 295 resources split between two stacks.

    #!/usr/bin/env python3
    
    from aws_cdk import core
    
    from lambda_api.lambda_api_stack import APIStack
    from lambda_api.lambda_api_stack import LambdasStack
    
    
    app = core.App()
    lambdas_stack = LambdasStack(app, 'lambdasstack')
    APIStack(app, 'apistack', lambdas=lambdas_stack.lambdas)
    
    
    app.synth()
    
    from aws_cdk import (
        aws_apigateway as apigateway,
        aws_lambda as _lambda,
        aws_s3 as s3,
        core
    )
    
    
    class LambdasStack(core.Stack):
    
        @property
        def lambdas(self):
            return self._lambdas
    
        def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
            super().__init__(scope, id, **kwargs)
    
            self._lambdas = list()
            for i in range(48):
                handler = _lambda.Function(
                    self, f'Handler_{i}',
                    function_name=f'Handler_{i}',
                    runtime=_lambda.Runtime.PYTHON_3_8,
                    code=_lambda.Code.from_asset('resources'),
                    handler='handler.main'
                )
                self._lambdas.append(handler)
    
    
    class APIStack(core.Stack):
    
        def __init__(self, scope: core.Construct, id: str, lambdas: list,
                **kwargs) -> None:
            super().__init__(scope, id, **kwargs)
    
            api = apigateway.RestApi(
                self, 'lambdas-api',
                rest_api_name='Lambdas Service',
                description='This service serves lambdas.'
            )
    
            for i in range(len(lambdas)):
                get_lambda_integration = apigateway.LambdaIntegration(
                    lambdas[i],
                    request_templates={
                        'application/json':
                        '{ "statusCode": "200" }'
                    }
                )
                model = api.root.add_resource(f'Resource_{i}')
                model.add_method('GET', get_lambda_integration)
    

    For this example, the API resource + Lambda integrations generate the most resources. Here is an outline of the resources created.

    97 resources are created in lambdasstack.

    • 1 AWS::CDK::Metadata
    • 48 x 2 resources
      • 1 AWS::Lambda::Function
      • 1 AWS::IAM::Role

    198 resources are created in apistack.

    • 1 AWS::CDK::Metadata
    • 1 AWS::ApiGateway::Account
    • 1 AWS::IAM::Role
    • 1 AWS::ApiGateway::RestApi
    • 1 AWS::ApiGateway::Deployment
    • 1 AWS::ApiGateway::Stage
    • 48 x 4 resources
      • 1 AWS::ApiGateway::Resource
      • 1 AWS::ApiGateway::Method
      • 2 AWS::IAM::Role

提交回复
热议问题