Sub topic 6
Granting permissions with grant methods
This chapter assumes you have defined an S3 bucket and a Lambda function in a CDK stack, and that you know basic IAM terms: role, policy, and principal.
Why grant methods exist
A Lambda function runs with an execution role. By default that role can write logs and do nothing else. To read a bucket, the role needs an IAM policy that names the bucket's ARN and lists the allowed actions.
Writing that policy by hand is error prone. You have to copy the ARN, remember to add both the bucket ARN and the /* object ARN, and pick the right action names. Grant methods do this for you. They are methods on L2 constructs that add the right statements to the right principal's policy.
The basic pattern
The call has the form resource.grantX(principal). The resource owns the data. The principal is the grantee, which is any construct that has an IAM identity, such as a Lambda function or a role.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as lambda from 'aws-cdk-lib/aws-lambda';
export class AppStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'UploadsBucket', { versioned: true });
const fn = new lambda.Function(this, 'Processor', {
runtime: lambda.Runtime.NODEJS_20_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
});
bucket.grantRead(fn);
}
}The single grantRead line adds a policy to the function's execution role. The policy allows read actions such as s3:GetObject*, s3:GetBucket*, and s3:List* on the bucket and on its objects. If the bucket uses a customer-managed KMS key, the grant also adds the key permissions.
In Python the method is bucket.grant_read(fn). The behavior is the same.
Common grant methods
| Method | What it allows |
|---|---|
| `grantRead` | Read objects and list the bucket |
| `grantWrite` | Write and delete objects |
| `grantReadWrite` | Both of the above |
| `grantPut` | Put objects only |
| `grantInvoke` (on a function) | Let another principal call the function |
Other services follow the same naming. Examples are table.grantReadData(fn) for DynamoDB and queue.grantSendMessages(fn) for SQS. Check the construct's API reference for the exact list.
What the grant produces
The grant becomes a AWS::IAM::Policy resource in the synthesized template, attached to the function's role. Run cdk diff before deploying to see the new statements. CDK shows security-sensitive IAM changes and asks for confirmation on deploy.
Why prefer grants over raw policies
- They use least privilege by default, scoped to the specific resource.
- They stay correct when the resource is renamed, because CDK uses a reference and not a typed ARN.
- They handle related details, such as KMS key access, that are easy to forget.
When no grant method exists
If a construct lacks a grant for the action you need, add a statement directly with fn.addToRolePolicy. Scope it to specific resources and avoid * where possible.
import * as iam from 'aws-cdk-lib/aws-iam';
fn.addToRolePolicy(new iam.PolicyStatement({
actions: ['s3:GetObjectTagging'],
resources: [bucket.arnForObjects('*')],
}));Exercise
You call bucket.grantRead(fn) but later your function needs to delete objects. Which single change is the most appropriate, and why?
Show answer
Replace it with bucket.grantReadWrite(fn), or add bucket.grantDelete(fn) if you want to keep access narrow. Either way you use a grant method, so CDK generates a policy scoped to this bucket and no hand-written ARNs are needed.