Can someone give me a simple guide to display a ta...
# help
p
Can someone give me a simple guide to display a table from dynamodb in a react or html page? Ive tried to adapt the nextjs or react guides to show a table rather then a counter clicky thing but i'm getting lost
d
Are you getting the response back?
s
Are you trying to interact with DynamoDB directly from the React application, or from a backend API that your React application interacts with? In other words, React->DynamoDB or React->API->DynamoDB. You'll need AWS credentials to interact with DynamoDB, usually using the AWS SDK. Doing this in the front-end means you'll need to figure out how to securely access AWS credentials from your client application. Partially due to this reason, many prefer to do this in back-end code (e.g. a Lambda). Is this helping, or are you looking for something else?
p
That's helpful thanks. Yeah most tutorials seem to go via an api and I was wondering why. With nextjs or node or similar is it possible for the js file to interact directly w dynamo and cache results before the user side code to bypass that? Like environment variables or secrets manager for the creds? Or even directly in react if its public unauthenticated db for read only
s
Absolutely, either is possible. You just need to be mindful of how you're managing credentials in the front end. I'm not aware of a way to do this that doesn't expose your creds to users, but there may be a mechanism I'm unaware of
I suppose you could use cognito identity pools for your credentials, but that's another topic altogether
f
+1 on Identity Pool suggested by @Seth Geoghegan
@Paul Stone if you refer to this Identity Pool example, it grants the frontend to call the Api like this:
Copy code
auth.attachPermissionsForAuthUsers([api]);
Similarly, if you created a DynamoDB table, you can grant the frontend permissions like this:
Copy code
const table = new sst.Table(...);
auth.attachPermissionsForAuthUsers([api, table]);
p
How about a dynamodb table that's read only public access? If the data is not sensitive? Thanks for all your help everyone
s
A DynamoDB table itself cannot be "read only" in the way you're suggesting. AWS secures access to DynamoDB via IAM policies. To provide any sort of access to your table (read-only or otherwise), you'd need to create an IAM policy that explicitly allows access to the table. In order to use that policy, you'd need to have AWS credentials that use the given IAM policy. Other services (like S3) support resource-level permission policies (e.g. grant public read on a given S3 bucket), but DynamoDB does not support resource-based policies. In order to access DynamoDB directly from a client application, you'd need to provide AWS credentials to the client. AWS has an article on this topic where they specifically recommend not hard coding credentials in the front-end.