List all the notes in the table (for all users)

Hi!

First of all, best tutorial I’ve ever completed and it’s saved my life, I understand so much more of AWS than I did before.

I’m trying to push the app a bit further and list all the notes from all users (basically the contents of the bucket) which i thought would be simple as I could query the table but without the KeyConditionExpression and ExpressionAttributeValues, like below in a file named listall.js:

import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {
  const params = {
    TableName: "MY_TABLE_NAME",
    // 'KeyConditionExpression' defines the condition for the query
    // - 'userId = :userId': only return items with matching 'userId'
    //   partition key
    // 'ExpressionAttributeValues' defines the value in the condition
    // - ':userId': defines 'userId' to be Identity Pool identity id
    //   of the authenticated user
    // KeyConditionExpression: "userId = :userId",
    ExpressionAttributeValues: {
      ":userId": event.requestContext.identity.cognitoIdentityId
    }
  };

  try {
    const result = await dynamoDbLib.call("query", params);
    // Return the matching list of items in response body
    callback(null, success(result.Items));
  } catch (e) {
    callback(null, failure({ status: false }));
    console.log('wtf');
  }
}

My additions to my severless.yml below the ‘list’:

    # Defines an HTTP API endpoint that calls the main function in list.js
    # - path: url path is /feedsall
    # - method: GET request
    handler: listall.main
    events:
      - http:
          path: feedsall
          method: get
          cors: true
          authorizer: aws_iam

I’ve also added a different authenticated role in Federated Identities section:

  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "mobileanalytics:PutEvents",
        "cognito-sync:*",
        "cognito-identity:*"
      ],
      "Resource": [
        "*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:*"
      ],
      "Resource": [
        "arn:aws:s3:::MY_TABLE_NAME/private/${cognito-identity.amazonaws.com:sub}/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:ListBucket"
      ],
      "Resource": [
        "arn:aws:s3:::MY_TABLE_NAME/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"
      ],
      "Resource": [
        "arn:aws:execute-api:eu-west-1:*:BLABLABLA/*"
      ]
    }
  ]
} 

However I’ve been getting a 500 error code. Any help would b greatly appreciated

(error code, when i put “serverless invoke local --function listall --path mocks/list-event.json” in the terminal: )

    "statusCode": 500,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    "body": "{\"status\":false}"
}
1 Like

Fixed it! used ‘scan’ instead of ‘query’

import * as dynamoDbLib from "./libs/dynamodb-lib";
import { success, failure } from "./libs/response-lib";

export async function main(event, context, callback) {

  const params = {
    TableName: "MY_TABLE_NAME"
  };

  try {
    const result = await dynamoDbLib.call("scan", params);
    // Return the matching list of items in response body
    callback(null, success(result.Items));
  } catch (e) {
    callback(null, failure({ status: false }));
    console.log('wtf');
    console.log(e);

  }
}

3 Likes

Glad you figured it out and reported back!

If I write a function like hannahwynnjones, but I keep my original request that includes a query, how can I differentiate between the 2 functions when calling in the front end?

How do I specify which get request I want to use?

Hmm I’m not sure if I understand. You want to add another Lambda function and call it from the frontend?

Where is the federated identities section?

In this chapter where we edit the IAM role - https://serverless-stack.com/chapters/create-a-cognito-identity-pool.html.