Home > Software design >  How to properly create a Dynamodb table with SAM
How to properly create a Dynamodb table with SAM

Time:12-10

Just getting started with AWS SAM. Wanted to create a CRUD micro service using Lambda and DynamoDB My template.yaml:

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
  A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
Resources:
  microservicehttpendpoint:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: index.handler
      Runtime: nodejs12.x
      CodeUri: .
      Description: >-
        A simple backend (read/write to DynamoDB) with a RESTful API endpoint using Amazon API Gateway.
      MemorySize: 512
      Timeout: 10
      Policies:
        - DynamoDBCrudPolicy:
            TableName: MyTable
      Events:
        Api1:
          Type: Api
          Properties:
            Path: /MyResource
            Method: ANY
  MyTable:
  Type: AWS::Serverless::SimpleTable
  Properties:
    PrimaryKey: name
    TableName: dynocrud-table

I would expect three resources to be created: a) Lambda, b) Api Gateway endpoint, 3) DynamoDB table The Lambda and the API Gateway endpoint are being created as expected, but no DynamoDB table and no errors. Here is the cli output:

sam deploy
Uploading to dyno-crud/5c01af22215fe32fe7f18fa00be  5706 / 5706  (100.00%)

        Deploying with following values
        ===============================
        Stack name                   : dyno-crud
        Region                       : us-east-1
        Confirm changeset            : True
        Disable rollback             : False
        Deployment s3 bucket         : aws-sam-cli-managed-default-samclisourcebucket-141rt5mvjki0m
        Capabilities                 : ["CAPABILITY_IAM"]
        Parameter overrides          : {"TableNameParameter": "dynocrud-table"}
        Signing Profiles             : {}

Initiating deployment
=====================
Uploading to dyno-crud/d085f2bf53ae0ab235eabed4814.template  897 / 897  (100.00%)

Waiting for changeset to be created..

CloudFormation stack changeset
-----------------------------------------------------------------------------------------------------------------
Operation                    LogicalResourceId            ResourceType                 Replacement                
-----------------------------------------------------------------------------------------------------------------
  Add                        ServerlessRestApiDeploymen   AWS::ApiGateway::Deploymen   N/A                        
                             t1fee5cc247                  t                                                       
  Add                        ServerlessRestApiProdStage   AWS::ApiGateway::Stage       N/A                        
  Add                        ServerlessRestApi            AWS::ApiGateway::RestApi     N/A                        
  Add                        microservicehttpendpointAp   AWS::Lambda::Permission      N/A                        
                             i1PermissionProd                                                                     
  Add                        microservicehttpendpointRo   AWS::IAM::Role               N/A                        
                             le                                                                                   
  Add                        microservicehttpendpoint     AWS::Lambda::Function        N/A                        
-----------------------------------------------------------------------------------------------------------------

Changeset created successfully. arn:aws:cloudformation:us-east-1:458815622243:changeSet/samcli-deploy1639025652/bcbadc92-6106-4cb4-b551-54f3b9774936


Previewing CloudFormation changeset before deployment
======================================================
Deploy this changeset? [y/N]: y

2021-12-08 22:54:43 - Waiting for stack create/update to complete

CloudFormation events from stack operations
-----------------------------------------------------------------------------------------------------------------
ResourceStatus               ResourceType                 LogicalResourceId            ResourceStatusReason       
-----------------------------------------------------------------------------------------------------------------
CREATE_IN_PROGRESS           AWS::IAM::Role               microservicehttpendpointRo   -                          
                                                          le                                                      
CREATE_IN_PROGRESS           AWS::IAM::Role               microservicehttpendpointRo   Resource creation          
                                                          le                           Initiated                  
CREATE_COMPLETE              AWS::IAM::Role               microservicehttpendpointRo   -                          
                                                          le                                                      
CREATE_IN_PROGRESS           AWS::Lambda::Function        microservicehttpendpoint     -                          
CREATE_IN_PROGRESS           AWS::Lambda::Function        microservicehttpendpoint     Resource creation          
                                                                                       Initiated                  
CREATE_COMPLETE              AWS::Lambda::Function        microservicehttpendpoint     -                          
CREATE_IN_PROGRESS           AWS::ApiGateway::RestApi     ServerlessRestApi            -                          
CREATE_COMPLETE              AWS::ApiGateway::RestApi     ServerlessRestApi            -                          
CREATE_IN_PROGRESS           AWS::ApiGateway::RestApi     ServerlessRestApi            Resource creation          
                                                                                       Initiated                  
CREATE_IN_PROGRESS           AWS::Lambda::Permission      microservicehttpendpointAp   -                          
                                                          i1PermissionProd                                        
CREATE_IN_PROGRESS           AWS::Lambda::Permission      microservicehttpendpointAp   Resource creation          
                                                          i1PermissionProd             Initiated                  
CREATE_IN_PROGRESS           AWS::ApiGateway::Deploymen   ServerlessRestApiDeploymen   -                          
                             t                            t1fee5cc247                                             
CREATE_IN_PROGRESS           AWS::ApiGateway::Deploymen   ServerlessRestApiDeploymen   Resource creation          
                             t                            t1fee5cc247                  Initiated                  
CREATE_COMPLETE              AWS::ApiGateway::Deploymen   ServerlessRestApiDeploymen   -                          
                             t                            t1fee5cc247                                             
CREATE_IN_PROGRESS           AWS::ApiGateway::Stage       ServerlessRestApiProdStage   -                          
CREATE_IN_PROGRESS           AWS::ApiGateway::Stage       ServerlessRestApiProdStage   Resource creation          
                                                                                       Initiated                  
CREATE_COMPLETE              AWS::ApiGateway::Stage       ServerlessRestApiProdStage   -                          
CREATE_COMPLETE              AWS::Lambda::Permission      microservicehttpendpointAp   -                          
                                                          i1PermissionProd                                        
CREATE_COMPLETE              AWS::CloudFormation::Stack   dyno-crud                    -                          
-----------------------------------------------------------------------------------------------------------------

Successfully created/updated stack - dyno-crud in us-east-1

What should I change in order for the DynamoDB table to be created along the side with other resources?

CodePudding user response:

Please ensure your yaml file is properly indented. The Type: should be indented below the name of the resource, e.g.

MyTable:
  Type: AWS::Serverless::SimpleTable <-- indented here
  Properties:
    PrimaryKey: name
    TableName: dynocrud-table
  • Related