Direkt zum Inhalt

Amplify Gen 2 - Configure client library for custom Functions - AMPLIFY_DATA_DEFAULT_NAME?!

4

Hello together,

as of the official documentation for AWS Amplify Generation 2 (https://docs.amplify.aws/react/build-a-backend/functions/configure-client-library/), I shall be able to connect to Amplify from my custom function with the following code.

`import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime'; import { env } from '$amplify/env/say-hello';

const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(env);`

Unfortunately already for sandbox as well as for actual deployment it tells me:

Argument of type 'LambdaProvidedEnvVars' is not assignable to parameter of type 'DataClientEnv'. Property 'AMPLIFY_DATA_DEFAULT_NAME' is missing in type 'LambdaProvidedEnvVars' but required in type '{ AWS_ACCESS_KEY_ID: string; AWS_SECRET_ACCESS_KEY: string; AWS_SESSION_TOKEN: string; AWS_REGION: string; AMPLIFY_DATA_DEFAULT_NAME: string; }'.ts(2345) get_amplify_clients_configuration.d.ts(7, 5): 'AMPLIFY_DATA_DEFAULT_NAME' is declared here.

Looking into the autogenerated env file, there is no AMPLIFY_DATA_DEFAULT_NAME. And also in the entire documentation, I don't find anything about that.

Experiencing this with "@aws-amplify/backend": "^1.16.1", "@aws-amplify/backend-cli": "^1.7.2", "aws-cdk-lib": "^2.189.1",


in the "get_amplify_clients_configuration.d.ts" a comment states:

Your function needs to be granted resource access on your schema for this to work a.schema(...).authorization((allow) => [a.resource(myFunction)]) ... But that doesn't work.


UPDATE: So while the original documentation misses to mention that you need to grant access to the scheme for the functions and only links in a side note to a wrong article (https://docs.amplify.aws/react/build-a-backend/functions/grant-access-to-other-resources/#using-the-access-property) ...

Through another article I found the solution: As of https://docs.amplify.aws/react/build-a-backend/data/customize-authz/grant-lambda-function-access-to-api/ it would have to be a.schema(...).authorization(allow => [allow.resource(functionWithDataAccess)]);

After authorizing my function that way, I finally got the Amplify-client in my custom function to work.

Dear AWS Amplify Team - please review your documentations... It took me 2 days to figure this simple thing out and searching the internet for the error-message didn't gave any details. I wonder how many potential customers and revenue you loose because of bad documentation.

So I posted this for other people that might run into the same problem.

  • Thank you, you helped me. I was stuck on the same error.

  • Thank you so so much, I have been trying to see what I did wrong for the past hour.

  • This helped me as well. Thank you.

gefragt vor 5 Monaten717 Aufrufe
1 Antwort
2
To configure the Amplify client library for custom functions in AWS Amplify Gen 2, you need to properly grant your function access to the data resources it needs to interact with.

The error you encountered about `AMPLIFY_DATA_DEFAULT_NAME` being missing occurs because your function hasn't been granted access to interact with your Amplify Data resource. When a function is properly authorized to access your data resources, Amplify's backend tooling generates the necessary environment variables, including `AMPLIFY_DATA_DEFAULT_NAME`.

The solution is to grant your function access to your data schema by adding an authorization rule in your schema definition:

```typescript
a.schema(...).authorization(allow => [allow.resource(yourFunctionName)]);
```

This authorization step is crucial because it:
1. Grants the appropriate IAM permissions to your function
2. Ensures the necessary environment variables are generated
3. Allows the `getAmplifyDataClientConfig` function to properly configure the client

Once you've added this authorization, the `getAmplifyDataClientConfig` function will be able to read from the generated `env` object to produce the configuration for the resources your function has access to interact with. This function generates both the resource configuration and specifies how the client library should behave, particularly where it should read credentials from.

After making this change, your original code should work as expected:

```typescript
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime';
import { env } from '$amplify/env/your-function-name';

const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig(env);
```
  

Sources
Configure client library - AWS Amplify Gen 2 Documentation
Environment variables and secrets - AWS Amplify Gen 2 Documentation
Custom functions - React - AWS Amplify Gen 2 Documentation

beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.