#✅ - Unable to access the created Dynamo DB tables from data
16 messages · Page 1 of 1 (latest)
Here is the sample to use the data.resources
const backend = defineBackend({
auth,
data,
});
const dataResources = backend.resources.data.resources;
dataResources.amplifyDynamoDbTables["Todo"].provisionedThroughput = {
readCapacityUnits: 5,
writeCapacityUnits: 5,
};
Object.values(dataResources.amplifyDynamoDbTables).forEach((table) => {
table.pointInTimeRecoveryEnabled = false;
console.log(table);
});
Thank you for the answer! In my case I would like to access the DynamoDB table directly in a lambda function, so I need the generated table name!
hey there is currently a github issue open for this
https://github.com/aws-amplify/amplify-category-api/issues/2163
https://github.com/aws-amplify/amplify-category-api/issues/2137
for the table names you may need to utilize a workaround that i could get working but i would suggest testing this in your sandbox
https://github.com/aws-amplify/amplify-category-api/issues/2163#issuecomment-1874354570
How did you install the Amplify CLI? No response If applicable, what version of Node.js are you using? No response Amplify CLI Version Gen2 What operating system are you using? mac Did you make any...
Did it not cause a circular dependency?
hey, it didnt cause the circular dependancy. But wanted to mention we now have updated documentation on accessing the tables here: https://docs.amplify.aws/react/build-a-backend/data/override-resources/
The generated table name like UserProfile-xxjjshsidjt-dev?
yes, for a quick test i set this to a cfn stack output. do note the table name locally would be cfn token if you try to console log it but get resolved on deployment by cfn
const dataResource = backend.data.resources;
new cdk.CfnOutput(backend.data, "dfwddqw", {
value: dataResource.tables["Todo"].tableName,
description: "The name of the table",
});
Thank you so much for your help here. I have 1 last question do you know why I would be getting a circular dependency here? ```import { defineBackend } from "@aws-amplify/backend";
import * as iam from "aws-cdk-lib/aws-iam";
import { auth } from "./auth/resource";
import { data } from "./data/resource";
import { preSignUp } from "./auth/pre-signup/resource";
import { getUsersByEmailOrPhoneFunction } from "./data/getUsersByEmailOrPhone/resource";
import { Table } from "aws-cdk-lib/aws-dynamodb";
import { Function } from "aws-cdk-lib/aws-lambda";
/**
- @see https://docs.amplify.aws/react/build-a-backend/ to add storage, functions, and more
*/
const backend = defineBackend({
auth,
data,
preSignUp,
getUsersByEmailOrPhoneFunction,
});
const getUsersByEmailOrPhoneLambda = backend.getUsersByEmailOrPhoneFunction
.resources.lambda as Function;
getUsersByEmailOrPhoneLambda.addToRolePolicy(
new iam.PolicyStatement({
actions: ["dynamodb:Query"],
resources: ["*"],
})
);
const table = backend.data.resources.tables["UserProfile"] as Table;
getUsersByEmailOrPhoneLambda.addEnvironment(
"USER_PROFILE_TABLE_NAME",
table.tableName
);
backend.data.resources.tables["UserProfile"].grantReadData(
getUsersByEmailOrPhoneLambda
);```
I get it when I try this line getUsersByEmailOrPhoneLambda.addEnvironment( "USER_PROFILE_TABLE_NAME", table.tableName );
ah thanks for the info, did run into this. but noticed you may be able to get away with creating a custom query with a Lambda function and provide the access to the model from the API.
https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/
something as follows
UserProfile: a.model().authorization(allow => [allow.resource(getUserByEmail).to(["read"]))
getUserByEmail: a.query().returns(a.ref("UserProfile")).handler(a.handler.function(getUserByEmail))
unfortunately, I need to directly access the dynamodb functions in my lambda so I am unable to pass the table name due to that circular dependency.
Just wanted to come back and tell you how I was able to work around this. So there is a circular dependency there but I know that the dynamoDB table is created as "SchemaTableName" + backend.data.resources.graphqlApi.apiId + "-NONE" or getUsersByEmailOrPhoneLambda.addEnvironment( "USER_PROFILE_TABLE_NAME", "UserProfile" + backend.data.resources.graphqlApi.apiId + "-NONE" );