【问题标题】:How to create a universal override function for CDK L3 construct?如何为 CDK L3 构造创建通用覆盖函数?
【发布时间】:2023-02-04 10:26:42
【问题描述】:

我在 TypeScript 中创建了一个 AWS CDK L3 Construct,我可以在其中创建一个只启用一些安全功能的 Dynamodb 表,这样我就不必不断地一遍又一遍地设置这些功能。这是“mvp”部署

const table = new ddbTableSecure(this, "Table", {
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
    });

这是没有道具的 CDK 构造代码

export class ddbTableSecure extends Construct {
      table: dynamodb.Table;
      constructor(scope: Construct, id: string, props: TableProps) {
        super(scope, id);
    
        const haveSortKey = props.sortKey != undefined && props.sortKey != null;
    
        this.table = new dynamodb.Table(this, 'MyTable', {
          partitionKey: props.partitionKey,
          encryption: dynamodb.TableEncryption.AWS_MANAGED,
          pointInTimeRecovery: true,
          tableName: props.tableName,
          tableClass: props.tableClass,
          billingMode: props.billingMode,
          replicationRegions: props.replicationRegions,
          readCapacity: props.readCapacity,
          writeCapacity: props.writeCapacity,
          contributorInsightsEnabled: props.contributorInsightsEnabled,
          timeToLiveAttribute: props.timeToLiveAttribute,
          replicationTimeout: props.replicationTimeout,
          stream: props.stream,
          sortKey: haveSortKey ? props.sortKey : undefined,
          waitForReplicationToFinish: props.waitForReplicationToFinish,
          removalPolicy: props.removalPolicy,
          kinesisStream: props.kinesisStream,
        });
      }
    }

该代码有效,但是我正在尝试弄清楚如何添加覆盖,例如我不想使用 AWS.Managed 加密但 KMS。所以我会为我不想在构造代码中设置的属性提供一个函数/覆盖。所以这个构造的实例化看起来像这样

const table = new ddbTableSecure(this, "Table", {
  partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
  encryption: ddbSecure.override(dynamodb.TableEncryption.CUSTOMER_MANAGED)
});

如果我想覆盖上述加密功能和我构建的其他潜在 L3 构造,这将帮助我构建。我尝试创建一个名为 override 的附加参数

    const table = new ddbTableSecure(this, "Table", {
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
      encryption: ddbSecure.override(dynamodb.TableEncryption.CUSTOMER_MANAGED)
    },
    override: True
);

CDK 构造将识别覆盖已启用并允许更新 CDK 构造 DynamoDB 表。这仅适用于一个参数,并且不允许在需要时进行其他覆盖。

我该怎么做呢?我是不是从错误的角度来解决问题?任何和所有帮助将不胜感激

【问题讨论】:

    标签: typescript aws-cdk infrastructure-as-code aws-cdk-typescript


    【解决方案1】:

    你可以这样做:

    export class ddbTableSecure extends Construct {
          table: dynamodb.Table;
          constructor(scope: Construct, id: string, props: TableProps) {
            super(scope, id);
        
            this.table = new dynamodb.Table(this, 'MyTable', {
              encryption: dynamodb.TableEncryption.AWS_MANAGED,
              pointInTimeRecovery: true,
              ...props,
            });
          }
        }
    
    
    // override any props
        const table = new ddbTableSecure(this, "Table", {
          partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
          encryption: dynamodb.TableEncryption.CUSTOMER_MANAGED,
          pointInTimeRecovery: false,
        },
    

    比较Spread syntax

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      相关资源
      最近更新 更多