如何在Amplify上以按需容量模式创建DynamoDB表



DynamoDB有两种定价模式: 供应容量模式和按需容量

Amplify始终在供应容量模式下创建表。是否可以选择在默认情况下创建具有按需容量的表

C:usersamadhanampplify_project>amplify add storage
? Select from one of the below mentioned services: NoSQL Database
Welcome to the NoSQL DynamoDB database wizard
This wizard asks you a series of questions to help determine how to set up your NoSQL database table.
√ Provide a friendly name · DynamoDB
√ Provide table name · AuthorazationsTable
You can now add columns to the table.
√ What would you like to name this column · id
√ Choose the data type · string
√ Would you like to add another column? (Y/n) · yes
√ What would you like to name this column · name
√ Choose the data type · string
√ Would you like to add another column? (Y/n) · no
Before you create the database, you must specify how items in your table are uniquely organized. You do this by specifying a primary key. The primary key uniquely identifies each item in the table so that no two items can have the same key. This can be an individual column, or a combination that includes a primary key and a sort key.
To learn more about primary keys, see:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey
√ Choose partition key for the table · id
√ Do you want to add a sort key to your table? (Y/n) · yes
√ Choose sort key for the table · name
You can optionally add global secondary indexes for this table. These are useful when you run queries defined in a different column than the primary key.
To learn more about indexes, see:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.SecondaryIndexes
√ Do you want to add global secondary indexes to your table? (Y/n) · no
√ Do you want to add a Lambda Trigger for your Table? (y/N) · no
✅ Successfully added resource DynamoDB locally

C: \user\samadhan\amplify\backend\storage\DynmoDB\cli-inputs.json

{
"resourceName": "DynamoDB",
"tableName": "AuthorazationsTable",
"partitionKey": {
"fieldName": "id",
"fieldType": "string"
},
"sortKey": {
"fieldName": "name",
"fieldType": "string"
},
"gsi": [],
"triggerFunctions": []
}

C: \user\samadhan\aamplify\backend\storage\DynamiDB\build\Dynamidb-cloudformation-template.json

{
"Description": "DDB Resource for AWS Amplify CLI",
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"DynamoDBTable": {
"Type": "AWS::DynamoDB::Table",
"Properties": {
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
},
{
"AttributeName": "name",
"KeyType": "RANGE"
}
],
"AttributeDefinitions": [
{
"AttributeName": "id",
"AttributeType": "S"
},
{
"AttributeName": "name",
"AttributeType": "S"
}
],
"GlobalSecondaryIndexes": [],
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
},
"StreamSpecification": {
"StreamViewType": "NEW_IMAGE"
}

C: \user\samadhan\amplify\backend\storage\DynmoDB\build\parameters.json">

{
"tableName": "SefieAuthorizations",
"partitionKeyName": "id",
"partitionKeyType": "S",
"sortKeyName": "name",
"sortKeyType": "S"
}

这有点令人困惑,但我终于找到了配置它的方法。你必须使用一种名为";覆盖";Amplify Cli的功能。

文档:自定义Amplify生成的DynamoDB表

  1. 键入命令amplify override storage并输入
  2. 上面写着";您要覆盖哪个资源&quot->选择要自定义的表
  3. 它产生";override.ts";文件
  4. "是否要立即编辑override.ts文件?(Y/n("->是的
  5. "选择您的默认编辑器&quot->选择您的编辑器
  6. 编辑";override.ts";像这样的东西:

import { AmplifyDDBResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
export function override(resources: AmplifyDDBResourceTemplate) {
delete(resources.dynamoDBTable.provisionedThroughput)
resources.dynamoDBTable.billingMode = "PAY_PER_REQUEST"
}

  1. 保存后,运行amplify push

这需要几分钟才能完成,但我希望它能成功。

如果你有一个GSI,override.ts看起来是这样的。

import { AmplifyDDBResourceTemplate } from '@aws-amplify/cli-extensibility-helper';
export function override(resources: AmplifyDDBResourceTemplate) {
delete(resources.dynamoDBTable.provisionedThroughput);
delete(resources.dynamoDBTable.globalSecondaryIndexes[0].provisionedThroughput);
resources.dynamoDBTable.billingMode = "PAY_PER_REQUEST";
}

实现这一点的最简单方法如下:

  1. 转到您的amplify/backend/api/[PROJECT_NAME]/parameters.json文件
  2. "DynamoDBBillingMode"更改为"PAY_PER_REQUEST"

最新更新