aws-lambdaamazon-dynamodbserverless-frameworkdynamoose

User is not authorized to perform: dynamodb:CreateTable on resource:


When I tried to run my lambda function register which queries the table example_user, it will throw the error below. My code is only trying to get data from the table example_user and not create any table.

{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"AccessDeniedException: User: arn:aws:sts::577777777777:assumed-role/example-user-api-dev-ap-southeast-1-lambdaRole/example-user-api-dev-register is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:ap-southeast-1:577777777777:table/example_user","reason":{"errorType":"AccessDeniedException","errorMessage":"User: arn:aws:sts::577777777777:assumed-role/example-user-api-dev-ap-southeast-1-lambdaRole/example-user-api-dev-register is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:ap-southeast-1:577777777777:table/example_user"

The error was thrown after 13 UserController with email

This is my codes:

User.js

const schema = new dynamoose.Schema({
    "email": String,
    "uid": String,
    "name": String,
    "gender": {
        "type": Number,
        "default": 0
    },
    "profileImageType": {
        "type": Number,
        "default": 0
    },
    "profileImage": String,
    "accountType": Number,
}, {
    "saveUnknown": true,
    "timestamps": true
});

module.exports = dynamoose.model('example_user', schema);

UserController.js

const User = require("./User.js");
exports.getProfile = async function(email,res){
  console.log("13 UserController with email " + email)
  var profile = await User.get(email)
  console.log("15 profile")
  console.log(profile)
  if (profile){
    return profile;
  }else{
    return false;
  }
};

Below is a snippet from my serverless.yml file

iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:*"
      Resource: 
        - "arn:aws:s3:::profiles.example.app/*"
    - Effect: "Allow"
      Action:
        - dynamodb:Query
        - dynamodb:Scan
        - dynamodb:GetItem
        - dynamodb:PutItem
        - dynamodb:UpdateItem
        - dynamodb:DeleteItem
      Resource: 
        - "arn:aws:dynamodb:ap-southeast-1:577777777777:table/example_user"

Solution

  • You should be able to do dynamoose.model('example_user', schema, {"create": false}) to get away from the need to create a table https://dynamoosejs.com/guide/Model/