Skip to content

Commit e657200

Browse files
josefaidtykethannadetasticashika112
authored
add page for configuring client library in functions (#8167)
* add page for configuring client library in functions * Update src/pages/[platform]/build-a-backend/functions/configure-client-library/index.mdx Co-authored-by: Kethan sai <[email protected]> * Update cspell.json --------- Co-authored-by: Kethan sai <[email protected]> Co-authored-by: Dan Kiuna <[email protected]> Co-authored-by: ashika112 <[email protected]>
1 parent 65385b2 commit e657200

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,7 @@
16231623
"rehype",
16241624
"assetlinks",
16251625
"AMPLIFYRULES",
1626+
"preconfigured",
16261627
"manylinux",
16271628
"GOARCH",
16281629
"norpc"

src/directory/directory.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ export const directory = {
408408
{
409409
path: 'src/pages/[platform]/build-a-backend/functions/configure-functions/index.mdx'
410410
},
411+
{
412+
path: 'src/pages/[platform]/build-a-backend/functions/configure-client-library/index.mdx'
413+
},
411414
{
412415
path: 'src/pages/[platform]/build-a-backend/functions/scheduling-functions/index.mdx'
413416
},
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)