[英]AWS CDK - Declare input variables from StackProps
I've started to use the AWS CDK recently, and I've faced an issue.我最近开始使用 AWS CDK,但遇到了一个问题。 I would like to have an ability to define custom input variables, so end users who are using my AWS CDK, were able to define inputs by them selves, without editing whole code.
我希望能够定义自定义输入变量,以便使用我的 AWS CDK 的最终用户能够自行定义输入,而无需编辑整个代码。 I've managed to adopt standard types like;
我已经设法采用标准类型,例如; string, number, booleans etc, however I do not quite understand how to do the same with custom types, for example:
字符串、数字、布尔值等,但是我不太明白如何对自定义类型执行相同的操作,例如:
I've tried to create an AWS DynamoDB, firstly I've defined an interface then make sure, that end user will precisely input required data我尝试创建一个 AWS DynamoDB,首先我定义了一个接口,然后确保最终用户将准确输入所需的数据
Here is my interface:这是我的界面:
export interface IDynamoDB extends StackProps {
tableClass: unknow,
billingMode: unknow
sortKeyName: string,
sortKeyType: unknow,
partitionKeyName: string,
partitionKeyType: unknow
inTimeRecovery: boolean
}
Here is my class:这是我的 class:
export class Ddb extends Stack {
constructor(scope: Construct, name: string, props: IDynamoDB) {
super(scope, name, props);
const table = new ddb.Table(this, name, {
tableName: name,
tableClass: ddb.TableClass.STANDARD,
billingMode: ddb.BillingMode.PROVISIONED,
sortKey: {
name: props.sortKeyName,
type: ddb.AttributeType.NUMBER
},
partitionKey: {
name: props.partitionKeyName,
type: ddb.AttributeType.STRING
},
})
}
}
Imports进口
import { Construct } from 'constructs';
import { Stack, StackProps } from 'aws-cdk-lib';
import * as ddb from 'aws-cdk-lib/aws-dynamodb';
My problem with my interface, as you can see I've putted unknown as a type, and I don't know how to make more easier.我的界面存在问题,如您所见,我将unknown作为一种类型,但我不知道如何使它变得更容易。 At the end I would like to be able to just put a string for example:
最后,我希望能够只放置一个字符串,例如:
ddb.TableClass.STANDARD = standartTableClass ddb.TableClass.STANDARD = 标准表类
I hope I've managed to explain my problem我希望我已经设法解释了我的问题
Normally your props would have the same ddb
Enum types as the construct (see tableClass
below).通常你的道具将具有与构造相同的
ddb
枚举类型(参见下面的tableClass
)。 However, as you are optimizing for short values per your comment, you could define your props with the Enum's string values and lookup the type in your constructor (see billingMode
).但是,当您根据评论优化短值时,您可以使用枚举的字符串值定义道具并在构造函数中查找类型(请参阅
billingMode
)。
export interface IDynamoDB extends StackProps {
tableClass: ddb.TableClass;
billingMode: "PROVISIONED" | "PAY_PER_REQUEST"; // string values of the Typescript Enum type
sortKeyName: string;
sortKeyType: ddb.AttributeType;
partitionKeyName: string;
partitionKeyType: ddb.AttributeType;
inTimeRecovery: boolean;
}
table
constructor: table
构造函数:
tableClass: props.tableClass,
billingMode: ddb.BillingMode[props.billingMode], // map the enum
Notes of caution about the billingMode
approach: (1) you may want to apply stricter checks and (2) your users will lose intellisense documentation context.关于
billingMode
方法的注意事项:(1) 您可能想要应用更严格的检查,并且 (2) 您的用户将失去智能感知文档上下文。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.