1
votes

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);
    }
}
1

1 Answers

0
votes

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
        });
    }
}