Skip to content

Commit d2ab938

Browse files
authored
chore: remove references to discouraged grant* methods in examples. (#36784)
In the examples, there are some references to `grant*` methods in L2s, that are now discouraged (though not formally deprecated). Replace these examples with the preferred way, which is using the `grants` attribute of the construct. Some surrounding text also had to be updated to match the examples. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 7e8528a commit d2ab938

File tree

7 files changed

+33
-30
lines changed

7 files changed

+33
-30
lines changed

CONTRIBUTING.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -988,37 +988,37 @@ finalized, will be added to the AWS CDK with a specific suffix: `BetaX`. APIs
988988
with the preview suffix will never be removed, instead they will be deprecated
989989
and replaced by either the stable version (without the suffix), or by a newer
990990
preview version. For example, assume we add the method
991-
`grantAwesomePowerBeta1`:
991+
`addSecondaryResourceBeta1()` to a class:
992992

993993
```ts
994994
/**
995-
* This method grants awesome powers
995+
* This method adds a secondary resource to the main one
996996
*/
997-
grantAwesomePowerBeta1();
997+
addSecondaryResourceBeta1(res: SomeResource);
998998
```
999999

10001000
Times goes by, we get feedback that this method will actually be much better
1001-
if it accepts a `Principal`. Since adding a required property is a breaking
1002-
change, we will add `grantAwesomePowerBeta2()` and deprecate
1003-
`grantAwesomePowerBeta1`:
1001+
if it accepts an additional required `options` parameter. Since adding a required
1002+
parameter to a method is a breaking change, we will add `addSecondaryResourceBeta2()`
1003+
and deprecate `addSecondaryResourceBeta1`:
10041004

10051005
```ts
10061006
/**
1007-
* This method grants awesome powers to the given principal
1007+
* This method adds a secondary resource, with more options
10081008
*
10091009
* @param grantee The principal to grant powers to
10101010
*/
1011-
grantAwesomePowerBeta2(grantee: iam.IGrantable)
1011+
addSecondaryResourceBeta2(res: SomeResource, options: SecondaryResourceOptions);
10121012

10131013
/**
1014-
* This method grants awesome powers
1015-
* @deprecated use grantAwesomePowerBeta2
1014+
* This method adds a secondary resource to the main one
1015+
* @deprecated use addSecondaryResourceBeta1
10161016
*/
1017-
grantAwesomePowerBeta1()
1017+
addSecondaryResourceBeta1(res: SomeResource);
10181018
```
10191019

10201020
When we decide it's time to graduate the API, the latest preview version will
1021-
be deprecated and the final version - `grantAwesomePower` will be added.
1021+
be deprecated and the final version - `addSecondaryResource` will be added.
10221022

10231023
## Documentation
10241024

packages/@aws-cdk/aws-elasticache-alpha/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ declare const role: iam.Role;
397397

398398
// grant "elasticache:Connect" action permissions to role
399399
user.grantConnect(role);
400-
serverlessCache.grantConnect(role);
400+
serverlessCache.grants.connect(role);
401401
```
402402

403403
### Import an existing user and user group

packages/aws-cdk-lib/aws-ec2/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ const vpc = new ec2.Vpc(this, 'TheVPC', {
245245
const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', { vpc });
246246
securityGroup.addEgressRule(ec2.Peer.anyIpv4(), ec2.Port.tcp(443));
247247
for (const gateway of provider.gatewayInstances) {
248-
bucket.grantWrite(gateway);
248+
bucket.grants.write(gateway);
249249
gateway.addSecurityGroup(securityGroup);
250250
}
251251
```

packages/aws-cdk-lib/aws-iam/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,29 @@ Managed policies can be attached using `xxx.addManagedPolicy(ManagedPolicy.fromA
3232

3333
## Granting permissions to resources
3434

35-
Many of the AWS CDK resources have `grant*` methods that allow you to grant other resources access to that resource. As an example, the following code gives a Lambda function write permissions (Put, Update, Delete) to a DynamoDB table.
35+
Many of the AWS CDK resources have grant methods (accessible via the `grants` attribute) that allow you to grant other
36+
resources access to that resource. As an example, the following code gives a Lambda function write permissions
37+
(Put, Update, Delete) to a DynamoDB table.
3638

3739
```ts
3840
declare const fn: lambda.Function;
3941
declare const table: dynamodb.Table;
4042

41-
table.grantWriteData(fn);
43+
table.grants.writeData(fn);
4244
```
4345

44-
The more generic `grant` method allows you to give specific permissions to a resource:
46+
The more generic `actions` method allows you to give specific permissions to a resource:
4547

4648
```ts
4749
declare const fn: lambda.Function;
4850
declare const table: dynamodb.Table;
4951

50-
table.grant(fn, 'dynamodb:PutItem');
52+
table.grants.actions(fn, 'dynamodb:PutItem');
5153
```
5254

53-
The `grant*` methods accept an `IGrantable` object. This interface is implemented by IAM principal resources (groups, users and roles), policies, managed policies and resources that assume a role such as a Lambda function, EC2 instance or a Codebuild project.
55+
The grant methods accept an `IGrantable` object. This interface is implemented by IAM principal resources (groups, users and roles), policies, managed policies and resources that assume a role such as a Lambda function, EC2 instance or a Codebuild project.
5456

55-
You can find which `grant*` methods exist for a resource in the [AWS CDK API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html).
57+
You can find which grant methods exist for a resource in the [AWS CDK API Reference](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html).
5658

5759
## Roles
5860

@@ -70,8 +72,8 @@ automatically if you associate the construct with other constructs from the
7072
AWS Construct Library (for example, if you tell an *AWS CodePipeline* to trigger
7173
an *AWS Lambda Function*, the Pipeline's Role will automatically get
7274
`lambda:InvokeFunction` permissions on that particular Lambda Function),
73-
or if you explicitly grant permissions using `grant` functions (see the
74-
previous section).
75+
or if you explicitly grant permissions using the public methods in the
76+
`RoleGrants` class (see the previous section).
7577

7678
### Opting out of automatic permissions management
7779

@@ -186,7 +188,7 @@ const fn = new lambda.Function(this, 'MyLambda', {
186188
});
187189

188190
const bucket = new s3.Bucket(this, 'Bucket');
189-
bucket.grantRead(fn);
191+
bucket.grants.read(fn);
190192
```
191193

192194
The following report will be generated.
@@ -445,7 +447,8 @@ new iam.Role(this, 'Role', {
445447

446448
### Granting a principal permission to assume a role
447449

448-
A principal can be granted permission to assume a role using `grantAssumeRole`.
450+
A principal can be granted permission to assume a role using `assumeRole` from the `RoleGrants` class.
451+
For convenience, an instance of this class is available via the `grants` attribute on the `Role` class.
449452

450453
Note that this does not apply to service principals or account principals as they must be added to the role trust policy via `assumeRolePolicy`.
451454

@@ -455,7 +458,7 @@ const role = new iam.Role(this, 'role', {
455458
assumedBy: new iam.AccountPrincipal(this.account)
456459
});
457460

458-
role.grantAssumeRole(user);
461+
role.grants.assumeRole(user);
459462
```
460463

461464
### Granting service and account principals permission to assume a role

packages/aws-cdk-lib/aws-logs/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ Or more conveniently, write permissions to the log group can be granted as follo
6868

6969
```ts
7070
const logGroup = new logs.LogGroup(this, 'LogGroup');
71-
logGroup.grantWrite(new iam.ServicePrincipal('es.amazonaws.com'));
71+
logGroup.grants.write(new iam.ServicePrincipal('es.amazonaws.com'));
7272
```
7373

7474
Similarly, read permissions can be granted to the log group as follows.
7575

7676
```ts
7777
const logGroup = new logs.LogGroup(this, 'LogGroup');
78-
logGroup.grantRead(new iam.ServicePrincipal('es.amazonaws.com'));
78+
logGroup.grants.read(new iam.ServicePrincipal('es.amazonaws.com'));
7979
```
8080

8181
Be aware that any ARNs or tokenized values passed to the resource policy will be converted into AWS Account IDs.

packages/aws-cdk-lib/aws-sns/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,14 @@ myTopic.addSubscription(new subscriptions.SqsSubscription(queue));
6161
Note that subscriptions of queues in different accounts need to be manually confirmed by
6262
reading the initial message from the queue and visiting the link found in it.
6363

64-
The `grantSubscribe` method adds a policy statement to the topic's resource policy, allowing the specified principal to perform the `sns:Subscribe` action.
64+
The `topic.grants.subscribe` method adds a policy statement to the topic's resource policy, allowing the specified principal to perform the `sns:Subscribe` action.
6565
It's useful when you want to allow entities, such as another AWS account or resources created later, to subscribe to the topic at their own pace, separating permission granting from the actual subscription process.
6666

6767
```ts
6868
declare const accountPrincipal: iam.AccountPrincipal;
6969
const myTopic = new sns.Topic(this, 'MyTopic');
7070

71-
myTopic.grantSubscribe(accountPrincipal);
71+
myTopic.grants.subscribe(accountPrincipal);
7272
```
7373

7474
### Filter policy

packages/aws-cdk-lib/pipelines/ORIGINAL_API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ The Action can also be used as a Grantable after having been added to a Pipeline
628628
const action = new pipelines.ShellScriptAction({ /* ... */ });
629629
pipeline.addStage('Test').addActions(action);
630630

631-
bucket.grantRead(action);
631+
bucket.grants.read(action);
632632
```
633633

634634
#### Additional files from the source repository

0 commit comments

Comments
 (0)