Skip to content

Commit 6cf3065

Browse files
authored
follow-up to #7987, update remaining override examples with updated ergonomics (#7988)
* follow-up to #7987, update remaining override examples with updated ergonomics * Update src/pages/[platform]/build-a-backend/add-aws-services/overriding-resources/index.mdx
1 parent 7354c81 commit 6cf3065

File tree

1 file changed

+25
-30
lines changed
  • src/pages/[platform]/build-a-backend/add-aws-services/overriding-resources

1 file changed

+25
-30
lines changed

src/pages/[platform]/build-a-backend/add-aws-services/overriding-resources/index.mdx

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,50 +57,47 @@ The `backend` object exposes a `resources` property with objects for each of the
5757
For example, here is how you can access the Cognito user pool that is created by `defineAuth` and set a custom removal policy on the resource.
5858

5959
```ts title="amplify/backend.ts"
60+
import { RemovalPolicy } from 'aws-cdk-lib';
6061
import { defineBackend } from '@aws-amplify/backend';
6162
import { auth } from './auth/resource';
62-
import { UserPool } from 'aws-cdk-lib/aws-cognito';
63-
import { RemovalPolicy } from 'aws-cdk-lib';
6463

6564
const backend = defineBackend({
6665
auth
6766
});
6867

69-
const userPool = backend.auth.resources.userPool as UserPool;
68+
const userPool = backend.auth.resources.userPool;
7069
userPool.applyRemovalPolicy(RemovalPolicy.RETAIN_ON_UPDATE_OR_DELETE);
7170
```
7271

7372
Most L1 and L2 AWS CDK constructs that are used by the `define*` functions are accessible in this way.
7473

7574
## Example - Grant access permissions between resources
7675

77-
Consider the case that we want to grant a function created by `defineFunction` access to call the Cognito user pool created by `defineAuth`. This can be accomplished with the following overrides.
76+
Consider the case that we want to grant a function created by `defineFunction` access to call the Cognito user pool created by `defineAuth`. For most cases it is recommended to use the [`access` property on `defineAuth`](/[platform]/build-a-backend/auth/grant-access-to-auth-resources/), however for permissions not exposed by this property, access can be accomplished with the following overrides.
7877

7978
```ts title="amplify/backend.ts"
8079
import { defineBackend } from '@aws-amplify/backend';
8180
import { auth } from './auth/resource';
8281
import { data } from './data/resource';
83-
import { demoFunction } from './functions/demo-function/resource';
84-
import { UserPool } from 'aws-cdk-lib/aws-cognito';
85-
import { Function } from 'aws-cdk-lib/aws-lambda';
82+
import { authAuditorFunction } from './functions/auth-auditor-function/resource';
8683

8784
const backend = defineBackend({
8885
auth,
8986
data,
90-
demoFunction
87+
authAuditorFunction,
9188
});
9289

93-
const userPool = backend.auth.resources.userPool as UserPool;
94-
const lambdaFunction = backend.demoFunction.resources.lambda as Function;
90+
const userPool = backend.auth.resources.userPool;
91+
const lambdaFunction = backend.authAuditorFunction.resources.lambda;
9592

96-
// grant the lambdaFunction read access to users
97-
userPool.grant(lambdaFunction, 'cognito:GetUser', 'cognito:ListUsers');
93+
// grant the lambdaFunction access to list auth events for a particular user
94+
userPool.grant(lambdaFunction, 'cognito:AdminListUserAuthEvents');
9895

9996
// pass the Lambda the UserPool ID so that the Lambda can use it to make SDK calls
100-
lambdaFunction.addEnvironment('USER_POOL_ID', userPool.userPoolId);
97+
backend.authAuditorFunction.addEnvironment('USER_POOL_ID', userPool.userPoolId);
10198
```
10299

103-
## Example - Mutate synthesized CloudFormation
100+
## Example - Modify L1 CDK Constructs
104101

105102
It's possible to reach all the way down to the raw CloudFormation to mutate properties using `addPropertyOverride` on an AWS CDK construct. To edit the password policies of the Cognito user pool in `defineAuth`, you can use the following code.
106103

@@ -109,23 +106,21 @@ import { defineBackend } from '@aws-amplify/backend';
109106
import { auth } from './auth/resource';
110107

111108
const backend = defineBackend({
112-
auth
109+
auth,
113110
});
114-
115-
// override user pool password policies
116-
backend.auth.resources.cfnResources.cfnUserPool.addPropertyOverride(
117-
'Policies',
118-
{
119-
PasswordPolicy: {
120-
MinimumLength: 10,
121-
RequireLowercase: true,
122-
RequireNumbers: true,
123-
RequireSymbols: true,
124-
RequireUppercase: true,
125-
TemporaryPasswordValidityDays: 20
126-
}
127-
}
128-
);
111+
// extract L1 CfnUserPool resources
112+
const { cfnUserPool } = backend.auth.resources.cfnResources;
113+
// modify cfnUserPool policies directly
114+
cfnUserPool.policies = {
115+
passwordPolicy: {
116+
minimumLength: 10,
117+
requireLowercase: true,
118+
requireNumbers: true,
119+
requireSymbols: true,
120+
requireUppercase: true,
121+
temporaryPasswordValidityDays: 20,
122+
},
123+
};
129124
```
130125

131126
Note the usage of `auth.resources.cfnResources`. This property exposes [L1 CDK constructs](https://docs.aws.amazon.com/cdk/v2/guide/constructs.html#constructs_l1_using) that map one-to-one with the underlying CloudFormation properties.

0 commit comments

Comments
 (0)