简体   繁体   中英

How to extract definitions inside aws-cdk typescript stack class constructor function into separate files?

Experimenting with aws-cdk and it's easy to see how the stack/backend construct class files could get quite large. In this cdk Stack class example (copied and pruned from the aws-cdk api-cors-lambda-crud-dynamodb example) is there an idiomatic way to extract the dynamodb, lambda and apigateway constructor definitions out of the stack class constructor function into separate files? They all take this as the first argument. I'd like to have a separate file for all the lambda defs, one for the db def, api gateway config, etc that are still referenced inside the main stack class. Is there a way to do this without having to create new classes for each of the definitions?

export class ApiLambdaCrudDynamoDBStack extends cdk.Stack {
    constructor(app: cdk.App, id: string) {
        super(app, id);

        const dynamoTable = new dynamodb.Table(this, "items", {
            partitionKey: {
                name: "itemId",
                type: dynamodb.AttributeType.STRING
            },
            tableName: "items"
        });

        const getOneLambda = new lambda.Function(this, "getOneItemFunction", {
            code: new lambda.AssetCode("src"),
            handler: "get-one.handler",
            runtime: lambda.Runtime.NODEJS_10_X,
            environment: {
                TABLE_NAME: dynamoTable.tableName,
                PRIMARY_KEY: "itemId"
            }
        });

        const getAllLambda = new lambda.Function(this, "getAllItemsFunction", {
            code: new lambda.AssetCode("src"),
            handler: "get-all.handler",
            runtime: lambda.Runtime.NODEJS_10_X,
            environment: {
                TABLE_NAME: dynamoTable.tableName,
                PRIMARY_KEY: "itemId"
            }
        });

        dynamoTable.grantReadWriteData(getAllLambda);
        dynamoTable.grantReadWriteData(getOneLambda);

        const api = new apigateway.RestApi(this, "itemsApi", {
            restApiName: "Items Service"
        });

        const items = api.root.addResource("items");
        const getAllIntegration = new apigateway.LambdaIntegration(getAllLambda);
        items.addMethod("GET", getAllIntegration);

        const singleItem = items.addResource("{id}");
        const getOneIntegration = new apigateway.LambdaIntegration(getOneLambda);
        singleItem.addMethod("GET", getOneIntegration);
    }
}

Yeah you can separate them into their own stacks. 'LambdaStack', 'DynamoDbStack', etc.

Or you can have just separate 'helper' classes if you don't want it directly in the constructor. Here's a simple C# example:

public class ApiLambdaCrudDynamoDbStack : Stack 
{
    internal ApiLambdaCrudDynamoDbStack(Construct scope, string id) : base (scope, id)
    {
        var helper = new MyHelper();
        
        helper.CreateTable(this, "My-Cool-Table"); 
        
    }
}

public class MyHelper
{
    public void CreateTable(Construct scope, string tableName)
    {
        var table = new Table(scope, "My-Table", new TableProps()
        {
             TableName = tableName
        });
    }
}


The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM