1
votes

I am trying to create an AWS DynamoDB table using AWS CloudFormation,
I am getting an error while creating the same.

Previously I had specified all the columns of the table that I wanted,
But it seemed like AttributeDefinitions and KeySchema Properties need to somehow match, not sure though.

I narrowed down to just 2 columns which are must have in my table,
ie. user_id and expiry,
The user_id being the Primary Key and expiry being the column used for TTL purpose.

I am still getting the following error though -
Property AttributeDefinitions is inconsistent with the KeySchema of the table and the secondary indexes

Following is the snippet of my template where I'm getting an issue as per CloudFormation StatusReason -

userExpiry:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      TableName: !Join ['_', [!Ref "prefix",'user_expiry'] ]
      AttributeDefinitions:
        -
          AttributeName: 'expiry'
          AttributeType: 'N'
        -
          AttributeName: 'user_id'
          AttributeType: 'S'
      KeySchema:
        - AttributeName: "user_id"
          KeyType: "HASH"
      TimeToLiveSpecification:
        AttributeName: "expiry"
        Enabled: true
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1
      Tags:
        -
          Key: 'Project'
          Value: !Ref "ProjectName"
        -
          Key: 'Environment'
          Value: !Ref "ResourceEnvironment"

I am following this reference Doc -
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html

1

1 Answers

0
votes

Yaml uses fixed indentation Pattern/Scheme for the files, probably that could be a reason of getting Errors.I tried the following code and was able to create the DynamoDB Table. Hope you can make the sufficient changes as per the requirement at your end:

AWSTemplateFormatVersion: "2010-09-09" Description: "AWS CloudFormation Template for creating the DynamoDB Table - userExpiry." Resources: userExpiry: Type: 'AWS::DynamoDB::Table' Properties: TableName: NewTableName AttributeDefinitions: - AttributeName: 'user_id' AttributeType: 'S'

KeySchema: - AttributeName: "user_id" KeyType: "HASH" TimeToLiveSpecification: AttributeName: "expiry" Enabled: true ProvisionedThroughput: ReadCapacityUnits: 1 WriteCapacityUnits: 1 Tags: - Key: 'MyKey' Value: "ProjectName"

(*) The table(NewTableName) created using the above Yml file - contain only a single column for "user_id". The TTL Specification Column "expiry" can be placed during the insertion of Record.