Create DynamoDB table

  1. Open template.yaml file in fcj-book-store folder
  2. Add the following script at the end of the file to create a simple table in DynamoDB:
  SimpleTable:
    Type: AWS:Serverless::SimpleTable
    Properties:
      TableName: SimpleTable
      PrimaryKey:
        Name: id
        Type: String
  • The script defines a SimpleTable table in DynamoDB with the Partition key is id

CreateDynamoDBTable

  1. Run the following command to deploy SAM
sam build
sam deploy

CreateTable

  1. Open DynamoDB console
  2. Select Tables on the left menu. You can see SimpleTable table created

CreateTable

  1. But our table needs more config. So delete the above script and replace it with the following:
  BooksTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Books
      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
  • The above script creates the Books table in DynamoDB with the partition key of id, the sort key of rv_id and a Local Scondary Index.

CreateTable

  1. Run the following command to deploy SAM
sam build
sam deploy

CreateTable

  1. Back to DynamoDB console. The Books table have been created and SimpleTable table deleted

CreateTable

  1. Select Books table. Check informations of this table

CreateTable

  • Click Indexes tab

CreateTable

So you have created the Books table with the Local secondary index of name-index

  1. 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-store, such as: ap-southeast-1
  1. Run the following command at the directory where you save the dynamoDB.json file
aws dynamodb batch-write-item --request-items file://dynamoDB.json

TableData