How to create DynamoDB tables in on-demand capacity mode on Amplify?

Viewed 417

DynamoDB has two pricing models: provisioned capacity mode and on-demand capacity.

Amplify always creates tables in provisioned capacity mode. is there have an option to have tables get created with on-demand capacity by default ?

C:\user\samadhan\ampplify_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\DynamoDB\cli-inputs.json

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

C:\user\samadhan\amplify\backend\storage\DynamoDB\build\DynamoDB-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\DynamoDB\build\parameters.json"

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

It's bit confusing but I finally managed to find the way to configure this. You're gonna have to use something called "override" function of Amplify Cli.

Document: Customize Amplify-generated DynamoDB tables

  1. type command amplify override storage and enter.
  2. It says "Which resource would you like to override?" -> Select table you want to customize.
  3. It generates "override.ts" file under the resource folder you selected.
  4. "Do you want to edit override.ts file now? (Y/n)" -> Yes.
  5. "Choose your default editor" -> select your editor.
  6. edit "override.ts" something like this:

import { AmplifyDDBResourceTemplate } from '@aws-amplify/cli-extensibility-helper';

export function override(resources: AmplifyDDBResourceTemplate) {
    delete(resources.dynamoDBTable.provisionedThroughput)
    resources.dynamoDBTable.billingMode = "PAY_PER_REQUEST"
}

  1. after saving it, run amplify push

It'll take minutes to complete but I hope it succeeds.

If you have one GSI, override.ts looks like this.

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";
}
Related