簡體   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