-
Notifications
You must be signed in to change notification settings - Fork 1.1k
add page for configuring client library in functions #8167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ashika112
merged 4 commits into
aws-amplify:main
from
josefaidt:add-function-amplify-client-configuration
Apr 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f298bb1
add page for configuring client library in functions
josefaidt e5542ea
Update src/pages/[platform]/build-a-backend/functions/configure-clien…
josefaidt 4a057c7
Merge branch 'main' into add-function-amplify-client-configuration
nadetastic 5ef550a
Update cspell.json
ashika112 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/pages/[platform]/build-a-backend/functions/configure-client-library/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; | ||
|
||
export const meta = { | ||
title: 'Configure client library', | ||
description: | ||
'Learn how to configure the aws-amplify client library in function handlers', | ||
platforms: [ | ||
'android', | ||
'angular', | ||
'flutter', | ||
'javascript', | ||
'nextjs', | ||
'react', | ||
'react-native', | ||
'swift', | ||
'vue' | ||
] | ||
}; | ||
|
||
export function getStaticPaths() { | ||
return getCustomStaticPath(meta.platforms); | ||
} | ||
|
||
export function getStaticProps() { | ||
return { | ||
props: { | ||
meta | ||
} | ||
}; | ||
} | ||
|
||
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`. | ||
|
||
```ts title="amplify/my-function/handler.ts" | ||
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime'; | ||
import { env } from '$amplify/env/my-function'; | ||
|
||
const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig( | ||
env | ||
); | ||
``` | ||
|
||
<Callout warning> | ||
|
||
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. | ||
|
||
</Callout> | ||
|
||
`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. | ||
|
||
```ts title="amplify/my-function/handler.ts" | ||
import { getAmplifyDataClientConfig } from '@aws-amplify/backend/function/runtime'; | ||
// highlight-next-line | ||
import { Amplify } from 'aws-amplify'; | ||
import { env } from '$amplify/env/my-function'; | ||
|
||
const { resourceConfig, libraryOptions } = await getAmplifyDataClientConfig( | ||
env | ||
); | ||
|
||
// highlight-next-line | ||
Amplify.configure(resourceConfig, libraryOptions); | ||
``` | ||
|
||
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). | ||
|
||
## Under the hood | ||
|
||
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. | ||
|
||
```ts title="amplify/my-function/handler.ts" | ||
import { env } from "$amplify/env/my-function"; | ||
|
||
Amplify.configure( | ||
{/* resource configuration */}, | ||
{ | ||
Auth: { | ||
credentialsProvider: { | ||
// instruct the client library to read credentials from the environment | ||
getCredentialsAndIdentityId: async () => ({ | ||
credentials: { | ||
accessKeyId: env.AWS_ACCESS_KEY_ID, | ||
secretAccessKey: env.AWS_SECRET_ACCESS_KEY, | ||
sessionToken: env.AWS_SESSION_TOKEN, | ||
}, | ||
}), | ||
clearCredentialsAndIdentityId: () => { | ||
/* noop */ | ||
}, | ||
}, | ||
}, | ||
} | ||
); | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.