Search code examples
amazon-dynamodbaws-cloudformationaws-api-gateway

Custom IDs with AWS API Gateway and DynamoDB


I followed this tutorial to connect AWS API Gateway and DynamoDB. My DynamoDB table is defined as follows:

Resources:
  DragonsTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: mytable
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1

I was hoping to be able to use my own custom id values, but when I include one in the POST request this is overridden and replaced with a UUID. Is there any way I can use my own IDs?


Solution

  • DynamoDB does not auto generate IDs for you.

    You are setting your mapping template to use the context requestId as shown in the tutorial:

    "id": "S": "$context.requestId"

    You should set this template mapping to the Id you wish to use in your openAPI mapping.