Search code examples
aws-lambdaamazon-dynamodbserverless-framework

Serverless Framework - Setting up resource permissions for dynamodb


I've the following serverless.yml file. I'm trying to assign read write permissions to the generated dynamodb..

So far it generates my lambda and the dynamodb table but the lambda isn't assigned permissions to access it.

I get no errors and it doesn't seem to add the permission to the dynamodb table.

Can anyone shed any light please?

service:
  name: catcam

custom:
  stage: ${opt:stage, self:provider.stage}
  tableName: ${self:custom.stage}-notes

environment:
  tableName: ${self:custom.tableName}

plugins:
  - '@hewmen/serverless-plugin-typescript'
  - serverless-plugin-optimize
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  iamRoleStatements:
    - Effect: Allow
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource:
        - { "Fn::GetAtt": ["NotesTable", "Arn" ] }
          #        - { !GetAtt NotesTable.Arn }

functions:
  main: # The name of the lambda function
    # The module 'handler' is exported in the file 'src/lambda'
    handler: src/lambda.handler
    events:
      - http:
          method: any
          path: /{any+}


resources:
  Resources:
    NotesTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:custom.tableName}
        AttributeDefinitions:
          - AttributeName: userId
            AttributeType: S
          - AttributeName: noteId
            AttributeType: S
        KeySchema:
          - AttributeName: userId
            KeyType: HASH
          - AttributeName: noteId
            KeyType: RANGE
        # Set the capacity to auto-scale
        BillingMode: PAY_PER_REQUEST



Solution

  • Turns out there was nothing wrong with the above, it's correct!!.. it's was me being a banana and not matching the full name of the table with the environment in the application.. i.e. notes table becomes dev-notes for instance.. maybe the above will help someone.