In an AWS Amplify backed application, I can define two data models Inventory and Order. Then Amplify generates a GraphQL API and underlying DynamoDB tables for these models.
Then I can create a function to handle new orders. What it needs to do is check if there's enough inventory to complete the order, and if there is, update both inventory and order.
In this case, I would like to have the function read the respective table names from environment variables. That way I can create multiple Amplify stacks and the different functions will be configured to use the tables that belong to their stack.
I create the function like this
// amplify/functions/resource.ts
export const dynamoDbDataHandlerFunction = defineFunction(
(scope) =>
new Function(scope, "dynamoDB-data-handler-function", {
handler: Handler.FROM_IMAGE,
runtime: Runtime.FROM_IMAGE,
timeout: Duration.minutes(2), // default is 3 seconds
code: Code.fromEcrImage(
Repository.fromRepositoryArn(...),
{tagOrDigest: "v1.2.3"}
),
memorySize: 256,
environment: {
// environment variables
}
}),
{
resourceGroupName: "data"
}
);
However, at this point, and in this file (amplify/functions/resource.ts) I don't see a way to pull in the backend construct (defined in amplify/backend.ts) where I could read the DynamoDB table names.
// amplify/backend.ts
const backend = defineBackend({
auth,
data,
dynamoDbDataHandlerFunction,
});
const inventoryTable = backend.data.resources.tables["Inventory"];
const orderTable = backend.data.resources.tables["Order"];
// at this point I can access inventoryTable.tableName or orderTable.tableName
Is it possible to wire the different resources together?