Skip to content

Commit f298bb1

Browse files
committed
add page for configuring client library in functions
1 parent 874906a commit f298bb1

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

cspell.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,8 @@
16181618
"knowledgebases",
16191619
"rehype",
16201620
"assetlinks",
1621-
"AMPLIFYRULES"
1621+
"AMPLIFYRULES",
1622+
"preconfigured"
16221623
],
16231624
"flagWords": ["hte", "full-stack", "Full-stack", "Full-Stack", "sudo"],
16241625
"patterns": [

src/directory/directory.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ export const directory = {
396396
{
397397
path: 'src/pages/[platform]/build-a-backend/functions/configure-functions/index.mdx'
398398
},
399+
{
400+
path: 'src/pages/[platform]/build-a-backend/functions/configure-client-library/index.mdx'
401+
},
399402
{
400403
path: 'src/pages/[platform]/build-a-backend/functions/scheduling-functions/index.mdx'
401404
},
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 configuring Amplify with `getAmplifyDataClientConfig`, your function consumes schema information from an Amazon S3 bucket created during backend deployment with grants for the access your function need to use it. Any changes to this bucket outside of backend deployment may break 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)