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

sam*_*han 3 amazon-dynamodb aws-amplify aws-amplify-cli

DynamoDB 有两种定价模式:预置容量模式和按需容量。

\n

Amplify 始终以预配置容量模式创建表。\是否有一个选项可以默认使用按需容量创建表?

\n
C:\\user\\samadhan\\ampplify_project>amplify add storage\n? Select from one of the below mentioned services: NoSQL Database\n\nWelcome to the NoSQL DynamoDB database wizard\nThis wizard asks you a series of questions to help determine how to set up your NoSQL database table.\n\n\xe2\x88\x9a Provide a friendly name \xc2\xb7 DynamoDB\n\xe2\x88\x9a Provide table name \xc2\xb7 AuthorazationsTable\n\nYou can now add columns to the table.\n\n\xe2\x88\x9a What would you like to name this column \xc2\xb7 id\n\xe2\x88\x9a Choose the data type \xc2\xb7 string\n\xe2\x88\x9a Would you like to add another column? (Y/n) \xc2\xb7 yes\n\xe2\x88\x9a What would you like to name this column \xc2\xb7 name\n\xe2\x88\x9a Choose the data type \xc2\xb7 string\n\xe2\x88\x9a Would you like to add another column? (Y/n) \xc2\xb7 no\n\nBefore 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.\n\nTo learn more about primary keys, see:\nhttps://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.PrimaryKey\n\n\xe2\x88\x9a Choose partition key for the table \xc2\xb7 id\n\xe2\x88\x9a Do you want to add a sort key to your table? (Y/n) \xc2\xb7 yes\n\xe2\x88\x9a Choose sort key for the table \xc2\xb7 name\n\nYou 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.\nTo learn more about indexes, see:\nhttps://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html#HowItWorks.CoreComponents.SecondaryIndexes\n\n\xe2\x88\x9a Do you want to add global secondary indexes to your table? (Y/n) \xc2\xb7 no\n\xe2\x88\x9a Do you want to add a Lambda Trigger for your Table? (y/N) \xc2\xb7 no\n\n\xe2\x9c\x85 Successfully added resource DynamoDB locally\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n
 {\n  "resourceName": "DynamoDB",\n  "tableName": "AuthorazationsTable",\n  "partitionKey": {\n    "fieldName": "id",\n    "fieldType": "string"\n  },\n  "sortKey": {\n    "fieldName": "name",\n    "fieldType": "string"\n  },\n  "gsi": [],\n  "triggerFunctions": []\n}\n
Run Code Online (Sandbox Code Playgroud)\n

C:\\user\\samadhan\\amplify\\backend\\storage\\DynamoDB\\build\\DynamoDB-cloudformation-template.json

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

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

\n
{\n  "tableName": "SefieAuthorizations",\n  "partitionKeyName": "id",\n  "partitionKeyType": "S",\n  "sortKeyName": "name",\n  "sortKeyType": "S"\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Kaz*_*oto 5

这有点令人困惑,但我终于找到了配置它的方法。您必须使用 Amplify Cli 的“覆盖”功能。

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

  1. 键入命令amplify override storage并输入。
  2. 它说“您想覆盖哪个资源?” -> 选择您要自定义的表。
  3. 它会在您选择的资源文件夹下生成“override.ts”文件。
  4. “您现在想编辑 override.ts 文件吗?(是/否)” -> 是。
  5. “选择您的默认编辑器”-> 选择您的编辑器。
  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"
}
Run Code Online (Sandbox Code Playgroud)

  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";
}
Run Code Online (Sandbox Code Playgroud)