Serverless-application-model: SimpleTable Improvements

Created on 10 Jan 2018  路  7Comments  路  Source: aws/serverless-application-model

Parent tracking Issue for improvements to AWS::Serverless::SimpleTable:

  • [x] TableName
  • [x] Tagging
  • [ ] Stream

Feel free to add more things as you see fit

areserverless-simple-table typfeature

Most helpful comment

TableName and Tags are now available. Checkout Release Notes

All 7 comments

TableName and Tags are now available. Checkout Release Notes

Great! How can I use the new version?

@revmischa just use it in your CFN Template as described here. SAM runs as part of CloudFormation so the translation will happen before your stack will be created or updated so the latest version of SAM will be applied by AWS.

I added:

  Message:
    Type: AWS::Serverless::SimpleTable
    TableName: 'qanda.message'
    PrimaryKey:
      Name: id
      Type: String

to my template.yml in my CodeStar project. it created a table named awscodestar-qanda-lambda-MessageTable-1NKRNP43YJQHR. what am I doing wrong?

@revmischa You are not defining Properties making your template invalid.

SimpleTable Docs: https://github.com/awslabs/serverless-application-model/blob/develop/versions/2016-10-31.md#awsserverlesssimpletable
Note they can improved due to omitting the Type and LogicalId. So the full resource from the example in docs would be:

LogiclaIdForSimpleTable:
  Type: AWS::Serverless::SimpleTable
    Properties:
      TableName: my-table
      PrimaryKey:
        Name: id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
      Tags:
        Department: Engineering
        AppType: Serverless

@sanathkr Any updates on streams? And/or a definitive workaround example in the meantime?

I guess we have to fall back to using the full CloudFormation syntax for DynamoDB? (eg. this snippet)

Edit: Ok, so solving for myself, I started with this SimpleTable definition:

  UsersTable:
    Type: AWS::Serverless::SimpleTable
    Properties:
      PrimaryKey:
        Name: id
        Type: String
      ProvisionedThroughput:
        ReadCapacityUnits: 1
        WriteCapacityUnits: 1

After deploying and looking it up in the generated CloudFormation template.. it isn't actually all that more complicated:

  UsersTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      ProvisionedThroughput:
        WriteCapacityUnits: 1
        ReadCapacityUnits: 1
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - KeyType: HASH
          AttributeName: id

Now that I have access to the full power of the DynamoDB CloudFormation resource, I can use the StreamSpecification property (ref):

Putting this together, my StreamSpecification property needs to look like:

StreamSpecification:
  StreamViewType: NEW_AND_OLD_IMAGES

With my final resource looking like:

  UsersTable:
    Type: 'AWS::DynamoDB::Table'
    Properties:
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      ProvisionedThroughput:
        WriteCapacityUnits: 1
        ReadCapacityUnits: 1
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - KeyType: HASH
          AttributeName: id

I can then use Fn::GetAtt to read the StreamArn and pass it to my DynamoDB event source within my Golang lambda function:

  UsersTableChanged:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-UsersTableChanged
      CodeUri: ./target
      Handler: usersTableChanged
      Runtime: go1.x
      Events:
        UsersTableEventSource:
          Type: DynamoDB
          Properties:
            Stream: !GetAtt UsersTable.StreamArn
            StartingPosition: TRIM_HORIZON
            BatchSize: 10

All in all, much simpler than I expected.

Thanks for opening this issue! As this issue is very old, we are closing it due to inactivity. If you are still running into this issue, please re-open this issue.

Was this page helpful?
0 / 5 - 0 ratings