Skip to content

Commit 6a8aa3f

Browse files
committed
use existing auth resources
1 parent 0e7d09e commit 6a8aa3f

File tree

1 file changed

+50
-2
lines changed
  • src/pages/[platform]/build-a-backend/auth/use-existing-cognito-resources

1 file changed

+50
-2
lines changed

src/pages/[platform]/build-a-backend/auth/use-existing-cognito-resources/index.mdx

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,60 @@ Configuring the mobile client libraries directly is not supported, however you c
112112

113113
## Use auth resources with an Amplify backend
114114

115-
<Callout warning>
115+
If you have created Amazon Cognito resources outside of the context of your Amplify app such as creating resources through the AWS Console or consuming resources created by a separate team, you can use `referenceAuth` to reference the existing resources.
116+
117+
```ts title="amplify/auth/resource.ts"
118+
import { referenceAuth } from '@aws-amplify/backend';
119+
120+
export const auth = referenceAuth({
121+
userPoolId: 'us-east-1_xxxx',
122+
identityPoolId: 'us-east-1:b57b7c3b-9c95-43e4-9266-xxxx',
123+
authRoleArn: 'arn:aws:iam::xxxx:role/amplify-xxxx-mai-amplifyAuthauthenticatedU-xxxx',
124+
unauthRoleArn: 'arn:aws:iam::xxxx:role/amplify-xxxx-mai-amplifyAuthunauthenticate-xxxx',
125+
userPoolClientId: 'xxxx',
126+
});
127+
```
128+
129+
<Callout info>
116130

117-
**Warning:** Amplify resources do not support including auth configurations by referencing with CDK. We are currently working to improve this experience by providing first-class support for referencing existing auth resources. [View the RFC for `referenceAuth` for more details](https://github.com/aws-amplify/amplify-backend/issues/1548)
131+
Referenced resources cannot be modified. IAM policies specific to your Amplify application will be appended to your authenticated and unauthenticated roles.
118132

119133
</Callout>
120134

135+
You can also use the [`access` property](/[platform]/build-a-backend/auth/grant-access-to-auth-resources/) to grant permissions to your auth resource from other Amplify backend resources. For example, if you have a function that needs to retrieve details about a user:
136+
137+
```ts title="amplify/auth/resource.ts"
138+
import { referenceAuth } from '@aws-amplify/backend';
139+
import { getUser } from "../functions/get-user/resource";
140+
141+
export const auth = referenceAuth({
142+
userPoolId: 'us-east-1_xxxx',
143+
identityPoolId: 'us-east-1:b57b7c3b-9c95-43e4-9266-xxxx',
144+
authRoleArn: 'arn:aws:iam::xxxx:role/amplify-xxxx-mai-amplifyAuthauthenticatedU-xxxx',
145+
unauthRoleArn: 'arn:aws:iam::xxxx:role/amplify-xxxx-mai-amplifyAuthunauthenticate-xxxx',
146+
userPoolClientId: 'xxxx',
147+
access: (allow) => [
148+
allow.resource(getUser).to(["getUser"]),
149+
],
150+
});
151+
```
152+
153+
In a team setting you may want to reference a different set of auth resources depending on the deployment context. For instance if you have a `staging` branch that should reuse resources from a separate "staging" environment compared to a `production` branch that should reuse resources from the separate "production" environment. In this case we recommend using environment variables.
154+
155+
```ts title="amplify/auth/resource.ts"
156+
import { referenceAuth } from '@aws-amplify/backend';
157+
158+
export const auth = referenceAuth({
159+
userPoolId: process.env.MY_USER_POOL_ID,
160+
identityPoolId: process.env.MY_IDENTITY_POOL_ID,
161+
authRoleArn: process.env.MY_AUTH_ROLE_ARN,
162+
unauthRoleArn: process.env.MY_UNAUTH_ROLE_ARN,
163+
userPoolClientId: process.env.MY_USER_POOL_CLIENT_ID,
164+
});
165+
```
166+
167+
Environment variables must be configured separately on your machine for sandbox deployments and Amplify console for branch deployments.
168+
121169
## Next steps
122170

123171
- [Learn how to connect your frontend](/[platform]/build-a-backend/auth/connect-your-frontend/)

0 commit comments

Comments
 (0)