亚马逊 DynamoDB 构造库
AWS CDK v1在2023-06-01上达到了支持结束. 此软件包不再更新,用户应该迁移到 AWS CDK v2.
关于如何移徙的更多信息,请参见: 移动到 AWS CDK v2 指南。 。 。 .
以下是最低可部署的DynamomDB表格定义:
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
});
导入已存在的表格
要将已有的表格导入您的 CDK 应用程序, 请使用 Table.fromTableName, (中文(简体) ), Table.fromTableArn 或者说 Table.fromTableAttributes
工厂方法。 这种方法接受表格名称或表格 ARN 描述一个已经存在的属性
现有表格 :
declare const user: iam.User;
const table = dynamodb.Table.fromTableArn(this, 'ImportedTable', 'arn:aws:dynamodb:us-east-1:111111111:table/my-table');
// now you can just call methods on the table
table.grantReadWriteData(user);
如果你打算使用 tableStreamArn (包括间接地,例如通过建立
@aws-cdk/aws-lambda-event-source.DynamoEventSource 在导入的表格上),您 必须 使用该
Table.fromTableAttributes 方法和 tableStreamArn 属性 必须 人口密集.
密钥
当表格被定义时,您必须使用 partitionKey
(所需)和 sortKey (可选)属性.
计费模式
DynamoDB支持两种计费模式:
- Provided - 表格和全球二级索引配置读写能力的默认模式.
- PAY PER REQENT-按需定价和规模. 您只支付您使用的东西, 没有读写功能 .
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
});
进一步读作: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/How ItWorks. ReadWrite CapacityMode 互联网档案馆的存檔,存档日期2013-12-20。 。 。 .
表格类别
DynamoDB支持两个表类:
- 标准(Standard)——默认模式,被推荐为绝大多数工作量.
- STAND INFQUENT ACESS - 对存储为主要成本的表格进行优化.
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
tableClass: dynamodb.TableClass.STANDARD_INFREQUENT_ACCESS,
});
配置表格的自动缩放
你可以让DynamomDB自动提高和降低读写能力 设置自动缩放。 你可以用这个 要么留着你的 表格的预期利用率,或通过在预配置时扩大或缩小 时间:
自动缩放仅与具有计费模式的表格相关,PROVISIONED.
进一步读作: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/AutoScaling.html 互联网档案馆的存檔,存档日期2014-12-21 https://aws.amazon.com/blogs/database/how-to-use-aws-cloudform-to-conform-auto-scale- for-amazon-dinamodb-tables-and-indexs/ 互联网档案馆的存檔,存档日期2014-12-02
亚马逊迪纳摩DB 全球表
您可以通过设置 DynamoDB 全球表格创建 replicationRegions a. 财产 Table数字 :
const globalTable = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
replicationRegions: ['us-east-1', 'us-east-2', 'us-west-2'],
});
在这样做时, 堆栈将添加一个 CloudFormation 自定义资源, 以便创建复制表格 选定区域.
Global Tables 的默认计费模式是 PAY_PER_REQUEST。 。 。 .
如果你想使用的话 PROVISIONED, (中文(简体) ),
您必须确保该表启用自动缩放:
const globalTable = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
replicationRegions: ['us-east-1', 'us-east-2', 'us-west-2'],
billingMode: dynamodb.BillingMode.PROVISIONED,
});
globalTable.autoScaleWriteCapacity({
minCapacity: 1,
maxCapacity: 10,
}).scaleOnUtilization({ targetUtilizationPercent: 75 });
当为大表添加复制区域时,您可能想要增加 复制操作的超时 :
const globalTable = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
replicationRegions: ['us-east-1', 'us-east-2', 'us-west-2'],
replicationTimeout: Duration.hours(2), // defaults to Duration.minutes(30)
});
加密
所有存储在亚马逊DynamoDB的用户数据在休息时全部加密. 在创建新表格时,您可以选择使用下列客户主密钥(CMK)加密您的表格:
- AWS自有的CMK - 默认情况下,所有表格都会被加密到DynamoDB服务账户中的AWS自有客户主密钥(CMK)下(不适用额外收费).
- AWS管理的CMK - AWS KMS密钥(每个区域一个)由AWS DynamoDB(应用AWS KMS收费)在您的账户中创建,管理并代表您使用.
- 客户管理的 CMK - 您完全控制用于加密 DynamoDB 表的 KMS 密钥( 应用 AWS KMS 收费) .
用客户管理的 CMK 创建一个加密的表格 :
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: dynamodb.TableEncryption.CUSTOMER_MANAGED,
});
// You can access the CMK that was added to the stack on your behalf by the Table construct via:
const tableEncryptionKey = table.encryptionKey;
您也可以提供自己的钥匙 :
import * as kms from '@aws-cdk/aws-kms';
const encryptionKey = new kms.Key(this, 'Key', {
enableKeyRotation: true,
});
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: dynamodb.TableEncryption.CUSTOMER_MANAGED,
encryptionKey, // This will be exposed as table.encryptionKey
});
为了取而代之使用AWS管理的CMK,将代码改为:
const table = new dynamodb.Table(this, 'MyTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
encryption: dynamodb.TableEncryption.AWS_MANAGED,
});
// In this case, the CMK _cannot_ be accessed through table.encryptionKey.
获取表格或二级索引的策略
要获得表格或索引的分区密钥和排序密钥,您已配置 :
declare const table: dynamodb.Table;
const schema = table.schema();
const partitionKey = schema.partitionKey;
const sortKey = schema.sortKey;
// In case you want to get schema details for any secondary index
// const { partitionKey, sortKey } = table.schema(INDEX_NAME);
Kinesis 流线
一个Kinesis Data Stream可以在DynamoDB表中被配置,以捕捉项目级别的变化.
import * as kinesis from '@aws-cdk/aws-kinesis';
const stream = new kinesis.Stream(this, 'Stream');
const table = new dynamodb.Table(this, 'Table', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
kinesisStream: stream,
});