aws-amplify / amplify-backend

Home to all tools related to Amplify's code-first DX (Gen 2) for building fullstack apps on AWS
Apache License 2.0
150 stars 50 forks source link

[Amplify gen2] Receive Amplify Data parameters as environment variables in a Amplify Function #1715

Open antennix opened 1 month ago

antennix commented 1 month ago

Environment information

System:
  OS: macOS 14.5
  CPU: (8) x64 Apple M2
  Memory: 32.73 MB / 16.00 GB
  Shell: Unknown
Binaries:
  Node: 18.18.0 - ~/.nvm/versions/node/v18.18.0/bin/node
  Yarn: undefined - undefined
  npm: 9.8.1 - ~/.nvm/versions/node/v18.18.0/bin/npm
  pnpm: 8.15.4 - ~/.nvm/versions/node/v18.18.0/bin/pnpm
NPM Packages:
  @aws-amplify/backend: 1.0.4
  @aws-amplify/backend-cli: 1.1.0
  aws-amplify: 6.3.8
  aws-cdk: 2.147.3
  aws-cdk-lib: 2.147.3
  typescript: 5.5.3
AWS environment variables:
  AWS_STS_REGIONAL_ENDPOINTS = regional
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables

Description

Is it possible for a Function to receive the Endpoint of Data, or the name and ARN of Datasources like DynamoDB?

For example, with Storage, you can use the "access" property in the defineStorage function to grant read and write permissions to a Function. https://docs.amplify.aws/react/build-a-backend/functions/grant-access-to-other-resources/

However, a similar approach cannot be used with defineData as it's not possible to specify properties.

It is possible to directly set values for an already created function in backend.ts using CDK, but if you're also using defineFunction for resolvers against Data, I think an error would occur due to circular reference, as the reference order would be resolverFunction → Data → thisFunction.

const l1CloneFunction = backend.cloneFunction.resources.lambda.node
.defaultChild as CfnFunction;

l1CloneFunction.addPropertyOverride(
 "Environment.Variables.TARGET_USER_TABLE",
 backend.data.resources.tables.User.tableName ?? "",
);

Is there a good solution?

edwardfoyle commented 1 month ago

Hi @antennix, you can grant a function access to the Data API by following these docs: https://docs.amplify.aws/react/build-a-backend/data/customize-authz/grant-lambda-function-access-to-api/

We are also actively investigating making the experience of configuring the data client in the context of a lambda function a better experience, so any feedback you have there is welcome!

antennix commented 1 month ago

@edwardfoyle Thank you. I understand the current approach to Data (AppSync). I'm hoping that the process of generating GraphQL code will become simpler.

On the other hand, is there an approach for when we want to access the data source (Dynamo) directly?

For example, executing a daily batch via Function (I believe the scheduling feature will be implemented soon: https://github.com/aws-amplify/amplify-backend/issues/1491), and performing specific processing on DynamoDB records.

We could update records through AppSync, but I don't think we can do so without users who are executing Subscriptions noticing. I want to update silently.

MyNameIsTakenOMG commented 1 month ago

Hi, @antennix , I bumped into the same issue a couple of days ago when trying to pass the dynamoDB table name to my lambda hander as an env variable, and I did exact the same thing as you mentioned in the description section, which caused a circular dependencies issue.

Then, I joined in a discussion in another similar open issue #2554, where we believe this could be some sort of bug. But fortunately, other great devs came up with some workarounds, which I already tested in my case and they worked just fine. And I hope they could work for you too. Good luck!

antennix commented 1 month ago

@MyNameIsTakenOMG Thank you. I think the Lambda custom resource that indirectly retrieves the resource name is a very good idea.

ykethan commented 2 weeks ago

Marking as feature request to provide Data parameters such as table name and arn as environment variables.