繁体   English   中英

如何将 aws-cdk typescript 堆栈 class 构造函数 function 中的定义提取到单独的文件中?

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

使用 aws-cdk 进行试验,很容易看出堆栈/后端如何构造 class 文件可能会变得非常大。 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文件? 他们都this为第一论据。 我想为所有 lambda defs 提供一个单独的文件,一个用于 db def、api 网关配置等,这些文件仍然在主堆栈 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6D 中引用。 有没有办法做到这一点而不必为每个定义创建新类?

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

是的,您可以将它们分成自己的堆栈。 “LambdaStack”、“DynamoDbStack”等

或者,如果您不想直接在构造函数中使用它,则可以只使用单独的“帮助器”类。 这是一个简单的 C# 示例:

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


暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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