AWS CDK - 如何将 "placeholder" 令牌与低级 cfn 构造一起使用
AWS CDK - How to use "placeholder" token with Low Level cfn constructs
我正在使用 CDK(在打字稿中)定义一个 AWS Timestream 数据库和其中的一个 table。
我想允许 AWS 设置数据库的名称(并避免对其进行硬编码)。问题是如何在 table 构造中引用该数据库名称。
CDK 用户会知道实际的数据库名称直到在 AWS 中创建(这是在执行定义 table 的 CDK 代码之后)才定义的。为此,CDK 创建了 placeholder Token(我强调)的想法。
我知道在上层CDK构造中,已经定义了占位符Token。 低级别 cfn 构造似乎并非如此。
下面是一些示例代码来解释:
此代码使用更高级别的 dynamo 构造和 lambda 并且有效:
const table = new dynamodb.Table(this, id);
const lambda = new lambda.Function(this, id);
lambda.addEnvironment("TABLE_NAME", table.tableName)
console.log(`table Token is: ${table.tableName}`); // prints *like* "${Token[TOKEN.540]}"
这是我为 AWS Timestream 编写的代码,没有生成令牌:
const db = new ts.CfnDatabase(this, id);
console.log(`database name is is: ${db.databaseName}`); // prints null
const table = new ts.CfnTable(this, id, {
databaseName: db.databaseName,
});// this will break as databaseName is null
我的问题:
- 原因:为什么下层构造不生成令牌。
- 如何:我 认为 我可以创建一个令牌并将其分配给
db.databaseName
但我不知道如何在 [=14] 时用数据填充该令牌=] 是 运行.
对于生成令牌和填充令牌的示例代码的任何帮助,我们将不胜感激。
您可能正在寻找 ref
而不是 databaseName
:
const db = new ts.CfnDatabase(this, id);
console.log(`database name is is: ${db.ref}`); // prints null
const table = new ts.CfnTable(this, id, {
databaseName: db.ref,
});
这对应 1:1 AWS::Timestream::Database CloudFormation 资源。正如 docs 所描述的那样,Ref
return 值表示数据库名称。
背景:“CFN 资源”(也称为 L1 资源)中公开的属性,包括资源 属性(因为它们是在对象初始化时配置的)和资源属性,这就是你想要的。
我正在使用 CDK(在打字稿中)定义一个 AWS Timestream 数据库和其中的一个 table。
我想允许 AWS 设置数据库的名称(并避免对其进行硬编码)。问题是如何在 table 构造中引用该数据库名称。
CDK 用户会知道实际的数据库名称直到在 AWS 中创建(这是在执行定义 table 的 CDK 代码之后)才定义的。为此,CDK 创建了 placeholder Token(我强调)的想法。
我知道在上层CDK构造中,已经定义了占位符Token。 低级别 cfn 构造似乎并非如此。
下面是一些示例代码来解释:
此代码使用更高级别的 dynamo 构造和 lambda 并且有效:
const table = new dynamodb.Table(this, id);
const lambda = new lambda.Function(this, id);
lambda.addEnvironment("TABLE_NAME", table.tableName)
console.log(`table Token is: ${table.tableName}`); // prints *like* "${Token[TOKEN.540]}"
这是我为 AWS Timestream 编写的代码,没有生成令牌:
const db = new ts.CfnDatabase(this, id);
console.log(`database name is is: ${db.databaseName}`); // prints null
const table = new ts.CfnTable(this, id, {
databaseName: db.databaseName,
});// this will break as databaseName is null
我的问题:
- 原因:为什么下层构造不生成令牌。
- 如何:我 认为 我可以创建一个令牌并将其分配给
db.databaseName
但我不知道如何在 [=14] 时用数据填充该令牌=] 是 运行.
对于生成令牌和填充令牌的示例代码的任何帮助,我们将不胜感激。
您可能正在寻找 ref
而不是 databaseName
:
const db = new ts.CfnDatabase(this, id);
console.log(`database name is is: ${db.ref}`); // prints null
const table = new ts.CfnTable(this, id, {
databaseName: db.ref,
});
这对应 1:1 AWS::Timestream::Database CloudFormation 资源。正如 docs 所描述的那样,Ref
return 值表示数据库名称。
背景:“CFN 资源”(也称为 L1 资源)中公开的属性,包括资源 属性(因为它们是在对象初始化时配置的)和资源属性,这就是你想要的。