Create DynamoDB table

  1. Open template.yaml file in fcj-book-shop folder.

  2. Copy the following scripts into that file.

    booksTableName:
      Type: String
      Default: Books
    

    CreateDynamoDBTable

    BooksTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: !Ref booksTableName
        BillingMode: PAY_PER_REQUEST
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: rv_id
            AttributeType: N
          - AttributeName: name
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: rv_id
            KeyType: RANGE
        LocalSecondaryIndexes:
          - IndexName: name-index
            KeySchema:
              - AttributeName: id
                KeyType: HASH
              - AttributeName: name
                KeyType: RANGE
            Projection:
              ProjectionType: ALL
    

    CreateDynamoDBTable

    • The above script creates the Books table in DynamoDB with the partition key of id, the sort key of rv_id and a Local Secondary Index.
  3. Run the following command to deploy SAM.

    sam build
    sam validate
    sam deploy
    

    CreateDynamoDBTable

  4. Back to DynamoDB console. At Tables page.

    • Click Books table. CreateDynamoDBTable
    • At Books page.
      • Check information of this table. CreateDynamoDBTable
      • Click Indexes tab.
      • Check the Local secondary indexes information. CreateDynamoDBTable So you have created the Books table with the Local secondary index name-index.
  5. To add data to the table, you can download the below file. Then, open file and replace all AWS-REGION with the region that create S3 bucket - book-image-resize-shop-by-myself, such as: us-east-1.

  6. Run the following command at the directory where you save the dynamoDB.json file.

    aws dynamodb batch-write-item --request-items file://dynamoDB.json
    

    CreateDynamoDBTable