amazon-dynamodbaws-clilocalstackamazon-dynamodb-local

awscli doesn't consider global-secondary-indexes when validating attribute-definitions


I'm trying to initialize dynamodb table when creating a localstack container. Consider following command:

awslocal dynamodb create-table \
  --debug \
  --table-name Journal \
  --global-secondary-indexes 'IndexName=GetJournalRowsIndex, KeySchema=[{AttributeName=persistence-id, KeyType=HASH},{AttributeName=sequence-nr,KeyType=RANGE}], Projection={ProjectionType=ALL}, ProvisionedThroughput={ReadCapacityUnits=10,WriteCapacityUnits=10}' \
  --global-secondary-indexes 'IndexName=TagsIndex, KeySchema=[{AttributeName=tags,KeyType=HASH}],Projection={ProjectionType=ALL},ProvisionedThroughput={ReadCapacityUnits=10,WriteCapacityUnits=10}' \
  --key-schema \
      AttributeName=pkey,KeyType=HASH \
      AttributeName=skey,KeyType=RANGE \
  --attribute-definitions \
      AttributeName=persistence-id,AttributeType=S \
      AttributeName=pkey,AttributeType=S \
      AttributeName=skey,AttributeType=S \
      AttributeName=sequence-nr,AttributeType=N \
      AttributeName=tags,AttributeType=S \
  --billing-mode PAY_PER_REQUEST

I'm getting the following error:

An error occurred (ValidationException) when calling the CreateTable operation: The number of attributes in key schema must match the number of attributesdefined in attribute definitions.

I'm using those in GSI so I wonder what am I doing wrong here?


Solution

  • I guess you can't specify global-secondary-indexes flag twice. Try the following

    awslocal dynamodb create-table \
      --debug \
      --table-name Journal \
      --global-secondary-indexes "[{\"IndexName\": \"GetJournalRowsIndex\", \"KeySchema\": [{\"AttributeName\": \"persistence-id\", \"KeyType\": \"HASH\"}, {\"AttributeName\": \"sequence-nr\", \"KeyType\": \"RANGE\"}], \"Projection\": {\"ProjectionType\": \"ALL\"}, \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 1, \"WriteCapacityUnits\": 1}}, {\"IndexName\": \"TagsIndex\", \"KeySchema\": [{\"AttributeName\": \"tags\", \"KeyType\": \"HASH\"}], \"Projection\": {\"ProjectionType\": \"ALL\"}, \"ProvisionedThroughput\": {\"ReadCapacityUnits\": 1, \"WriteCapacityUnits\": 1}}]" \
      --key-schema \
          AttributeName=pkey,KeyType=HASH \
          AttributeName=skey,KeyType=RANGE \
      --attribute-definitions \
          AttributeName=persistence-id,AttributeType=S \
          AttributeName=pkey,AttributeType=S \
          AttributeName=skey,AttributeType=S \
          AttributeName=sequence-nr,AttributeType=N \
          AttributeName=tags,AttributeType=S \
      --billing-mode PAY_PER_REQUEST