amplify-backend icon indicating copy to clipboard operation
amplify-backend copied to clipboard

Not possible to configure ddb Lambda trigger if GraphQL allows any function

Open OperationalFallacy opened this issue 1 year ago • 3 comments

Environment information

all latest

Description

Here's a use case for DDB lambda trigger with access to graphql. The lambda will process records and update state in the model with GraphQL mutation (careful with the updates since it can cause circular trigger).

Possible to achieve with this example https://docs.amplify.aws/react/build-a-backend/functions/examples/dynamo-db-stream/

However, it currently not possible to deploy, if model's auth includes at least one function (test1 function here) - then adding event source will break with circular dependency problem.

This is because the functions are added into own nested stack, which creates a dependency on data nested stack, which creates a circular dependency when these stacks referenced for event source.

const backend = defineBackend({
  auth,
  data,
  docUploaderFunction,
  test1,
});

const eventSource = new DynamoEventSource(
  backend.data.resources.tables["Todo"],
  {
    startingPosition: StartingPosition.LATEST,
    batchSize: 5,
  }
);

backend.docUploaderFunction.resources.lambda.addEventSource(eventSource);
amp sandbox
...
The CloudFormation deployment has failed.
Caused By: ❌ Deployment failed: Error [ValidationError]: Circular dependency between resources: [data7552DF31, function1351588B]

I think this is a legit bug.

Related: https://github.com/aws-amplify/amplify-backend/issues/1594

OperationalFallacy avatar Jul 12 '24 20:07 OperationalFallacy

Hey, thank you for reaching out. The issue appear to be similar to https://github.com/aws-amplify/amplify-backend/issues/1552, https://github.com/aws-amplify/amplify-backend/issues/1723.

Refer to the comments providing examples in creating a Lambda event source that mitigates the issue.
https://github.com/aws-amplify/amplify-backend/issues/1552#issuecomment-2138816880 https://github.com/aws-amplify/amplify-backend/issues/1723#issuecomment-2217818725

Marking this as feature request to further evaluate the nested stacks being created.

ykethan avatar Jul 17 '24 16:07 ykethan

Those are the example for external integrations, I think. I got it working as well, no problem there.

The case I'm describing is what Amplify published in the document page linked.

  1. There is a Lambda function managed by Amplify (it sets everything up automatically to access graphql with a client)
  2. Lambda function have permissions to use AppSync, again via .authorization((allow) => [ allow.resource(LambdaFunction)])

This Lambda gets invoked by DDB stream, so it can work with graph client and model types = profit

In other words, I can't work with GraphQL in Lambda functions managed by Amplify, if I want this function invoked by DDB stream.

And even if I use sdk to access tables directly, the event sourcing still breaks, if there is one function with authorization on scheme.

OperationalFallacy avatar Jul 17 '24 17:07 OperationalFallacy

With the latest release, the defineFunction now supports resourceGroupName property which should allow grouping the function with other categories this should help with the breaking the circular dependency between the stacks. Created a sample application providing with similar use case with event source as DDB stream on a function. https://github.com/ykethan/amplify-gen2-samples/tree/main/circular-dependency-rg But do note, setting this `resourceGroupName property will recreate the lambda function.

ykethan avatar Nov 27 '24 17:11 ykethan