Search code examples
amazon-web-servicesaws-lambdaserverless-frameworkserverlessaws-serverless

Unable to fetch TableName Serverless Framework: Missing required key 'TableName' in params


I followed this YouTube tutorial first to try and develop a serverless rest api using serverless framework. It gave an error of MissingRequiredParameter and initialised table name to undefined.

To alleviate that, I found a similar code on GitHub but deploying this also gives the same error.

Here are the console logs in cloud watch:

2021-06-19T23:12:12.606Z    97a3c3b8-375b-4e12-9b89-7ad1e44f1873    INFO    Error saving the kitten to the DB:  MissingRequiredParameter: Missing required key 'TableName' in params
    at ParamValidator.fail (/var/task/node_modules/aws-sdk/lib/param_validator.js:50:37)
    at ParamValidator.validateStructure (/var/task/node_modules/aws-sdk/lib/param_validator.js:61:14)
    at ParamValidator.validateMember (/var/task/node_modules/aws-sdk/lib/param_validator.js:88:21)
    at ParamValidator.validate (/var/task/node_modules/aws-sdk/lib/param_validator.js:34:10)
    at Request.VALIDATE_PARAMETERS (/var/task/node_modules/aws-sdk/lib/event_listeners.js:132:42)
    at Request.callListeners (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at callNextListener (/var/task/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
    at /var/task/node_modules/aws-sdk/lib/event_listeners.js:86:9
    at finish (/var/task/node_modules/aws-sdk/lib/config.js:386:7)
    at /var/task/node_modules/aws-sdk/lib/config.js:404:9 {
  code: 'MissingRequiredParameter',
  time: 2021-06-19T23:12:12.603Z
}



2021-06-19T23:12:12.606Z    97a3c3b8-375b-4e12-9b89-7ad1e44f1873    DEBUG   putParams:  { TableName: undefined, Item: { name: 'Fluffy', age: 4 } }


It gives an error 502 Bad Gateway

serverless.yaml file

service: lambda-kittens-app

provider:
    name: aws
    runtime: nodejs12.x

iamRoleStatements:
    - Effect: 'Allow'
      Action:
          - dynamodb:PutItem
          - dynamodb:Scan*
          - dynamodb:GetItem
          - dynamodb:UpdateItem
          - dynamodb:DeleteItem
      Resource: arn:aws:dynamodb:#{AWS::Region}:#{AWS::AccountId}:table/${self:service}-kittens-${opt:stage}

environment:
    DYNAMODB_KITTEN_TABLE: ${self:service}-kittens-${opt:stage}
functions:
    create:
        handler: handler.createKitten
        events:
            - http:
                  path: /v1/kitten
                  method: post
    list:
        handler: handler.listKitten
        events:
            - http:
                  path: /v1/kitten
                  method: get
    get:
        handler: handler.getKitten
        events:
            - http:
                  path: /v1/kitten/{name}
                  method: get
    update:
        handler: handler.updateKitten
        events:
            - http:
                  path: /v1/kitten/{name}
                  method: patch
    delete:
        handler: handler.deleteKitten
        events:
            - http:
                  path: /v1/kitten/{name}
                  method: delete
resources:
    Resources:
        kittensTable:
            Type: 'AWS::DynamoDB::Table'
            Properties:
                TableName: ${self:service}-kittens-${opt:stage}
                AttributeDefinitions:
                    - AttributeName: name
                      AttributeType: S
                KeySchema:
                    - AttributeName: name
                      KeyType: HASH
                BillingMode: PAY_PER_REQUEST
Plugins:
    - serverless-pseudo-parameters
#  Outputs:
#     NewOutput:
#       Description: "Description for the output"
#       Value: "Some output value"

Changes made to db-handler.js

AWS.config.update({ region: 'us-east-1' });

Help is appreciated. Thanks!

NOTE: I did check several solutions but none of them seems to work.


Solution

  • Without seeing your application code, I can't be certain. However I presume you are doing something like this:

    const documentClient = new DynamoDB.DocumentClient({ params: { TableName: process.env.DYNAMODB_KITTEN_TABLE } });
    
    ... // Put call
    

    I noticed in your serverless.yml, the environment: block is at the root level. That won't work, so your environment variable is undefined/not set.

    The environment block goes inside the provider block, or inside the function block (depending on if you want to set an env var per function, or for all functions in your stack):

    provider:
        name: aws
        runtime: nodejs12.x
        environment:
          DYNAMODB_KITTEN_TABLE: ${self:service}-kittens-${opt:stage}