|
| 1 | +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + title: 'Configure client library', |
| 5 | + description: |
| 6 | + 'Learn how to configure the aws-amplify client library in function handlers', |
| 7 | + platforms: [ |
| 8 | + 'android', |
| 9 | + 'angular', |
| 10 | + 'flutter', |
| 11 | + 'javascript', |
| 12 | + 'nextjs', |
| 13 | + 'react', |
| 14 | + 'react-native', |
| 15 | + 'swift', |
| 16 | + 'vue' |
| 17 | + ] |
| 18 | +}; |
| 19 | + |
| 20 | +export function getStaticPaths() { |
| 21 | + return getCustomStaticPath(meta.platforms); |
| 22 | +} |
| 23 | + |
| 24 | +export function getStaticProps() { |
| 25 | + return { |
| 26 | + props: { |
| 27 | + meta |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +The [`aws-amplify`](https://www.npmjs.com/package/aws-amplify) client library can be configured for use inside function handler files by using the credentials available from the AWS Lambda runtime. To get started, use the `getAmplifyDataClientConfig` from the backend runtime package and pass the generated `env` object to retrieve the preconfigured `resourceConfig` and `libraryOptions`. |
| 33 | + |
| 34 | +```ts title="amplify/my-function/handler.ts" |
| 35 | +import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime'; |
| 36 | +import { env } from '$amplify/env/my-function'; |
| 37 | + |
| 38 | +const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig( |
| 39 | + env |
| 40 | +); |
| 41 | +``` |
| 42 | + |
| 43 | +<Callout warning> |
| 44 | + |
| 45 | +When using `getAmplifyDataClientConfig`, your function requires schema information stored in an Amplify deployed Amazon S3 bucket. This bucket is created during backend deployment and includes necessary access grants for your function. Modifying this bucket outside of the backend deployment process may cause unexpected failures on your function. |
| 46 | + |
| 47 | +</Callout> |
| 48 | + |
| 49 | +`resourceConfig` and `libraryOptions` are returned for you to pass into `Amplify.configure`. This will instruct the client library which resources it can interact with, and where to retrieve AWS credentials to use when signing requests to those resources. |
| 50 | + |
| 51 | +```ts title="amplify/my-function/handler.ts" |
| 52 | +import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime'; |
| 53 | +// highlight-next-line |
| 54 | +import { Amplify } from 'aws-amplify'; |
| 55 | +import { env } from '$amplify/env/my-function'; |
| 56 | + |
| 57 | +const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig( |
| 58 | + env |
| 59 | +); |
| 60 | + |
| 61 | +// highlight-next-line |
| 62 | +Amplify.configure(resourceConfig, libraryOptions); |
| 63 | +``` |
| 64 | + |
| 65 | +The client library will now have access to perform operations against other AWS resources as specified by the function's IAM role. This is handled for you when [granting access to other resources using the `access` property](/[platform]/build-a-backend/functions/grant-access-to-other-resources/#using-the-access-property), however it can also be [extended using CDK](/[platform]/build-a-backend/functions/grant-access-to-other-resources/#using-cdk). |
| 66 | + |
| 67 | +## Under the hood |
| 68 | + |
| 69 | +The `getAmplifyDataClientConfig` function assists with creating the arguments' values to pass to `Amplify.configure`, which reads from the generated `env` object in order to produce configuration for the resources you have granted your function access to interact with. Under the hood this is also generating the configuration that specifies how the client library should behave, namely where the library should read credentials. |
| 70 | + |
| 71 | +```ts title="amplify/my-function/handler.ts" |
| 72 | +import { env } from "$amplify/env/my-function"; |
| 73 | + |
| 74 | +Amplify.configure( |
| 75 | + {/* resource configuration */}, |
| 76 | + { |
| 77 | + Auth: { |
| 78 | + credentialsProvider: { |
| 79 | + // instruct the client library to read credentials from the environment |
| 80 | + getCredentialsAndIdentityId: async () => ({ |
| 81 | + credentials: { |
| 82 | + accessKeyId: env.AWS_ACCESS_KEY_ID, |
| 83 | + secretAccessKey: env.AWS_SECRET_ACCESS_KEY, |
| 84 | + sessionToken: env.AWS_SESSION_TOKEN, |
| 85 | + }, |
| 86 | + }), |
| 87 | + clearCredentialsAndIdentityId: () => { |
| 88 | + /* noop */ |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + } |
| 93 | +); |
| 94 | +``` |
0 commit comments