From ac26af6668b0fff1776ba2692e69b39972735866 Mon Sep 17 00:00:00 2001 From: Amariah Kamau <110414493+AmariahAK@users.noreply.github.com> Date: Fri, 31 Jul 2026 09:04:08 +0300 Subject: [PATCH] docs(s3,kms): replace object-literal IResourceWithPolicyV2 example with named class The object-literal pattern with inline methods in the forResource() return value does not translate across jsii/rosetta to non-TypeScript languages (Ruby, Python, Java, C#, Go). Replace it with a proper MyResourceWithPolicy class that implements IResourceWithPolicyV2, matching the existing CfnBucketWithPolicy and CfnKeyWithPolicy patterns in default-traits.ts. closes #38452 Co-authored-by: atlarix-agent --- packages/aws-cdk-lib/aws-kms/README.md | 31 +++++++++++++++++--------- packages/aws-cdk-lib/aws-s3/README.md | 31 +++++++++++++++++--------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/aws-cdk-lib/aws-kms/README.md b/packages/aws-cdk-lib/aws-kms/README.md index 624c44744e745..11f6150c598e7 100644 --- a/packages/aws-cdk-lib/aws-kms/README.md +++ b/packages/aws-cdk-lib/aws-kms/README.md @@ -293,20 +293,31 @@ for the `AWS::KMS::Key` CloudFormation type: ```ts nofixture import { CfnResource } from 'aws-cdk-lib'; -import { IResourcePolicyFactory, IResourceWithPolicyV2, PolicyStatement, ResourceWithPolicies } from 'aws-cdk-lib/aws-iam'; -import { Construct, IConstruct } from 'constructs'; +import { AddToResourcePolicyResult, IResourcePolicyFactory, IResourceWithPolicyV2, PolicyStatement, ResourceWithPolicies } from 'aws-cdk-lib/aws-iam'; +import { ResourceEnvironment } from 'aws-cdk-lib/interfaces'; +import { Construct } from 'constructs'; declare const scope: Construct; + +class MyResourceWithPolicy implements IResourceWithPolicyV2 { + public readonly env: ResourceEnvironment; + private readonly resource: CfnResource; + + constructor(resource: CfnResource) { + this.resource = resource; + this.env = resource.env; + } + + public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult { + // custom implementation to add the statement to the resource policy + return { statementAdded: true, policyDependable: this.resource }; + } +} + class MyFactory implements IResourcePolicyFactory { - forResource(resource: CfnResource): IResourceWithPolicyV2 { - return { - env: resource.env, - addToResourcePolicy(statement: PolicyStatement) { - // custom implementation to add the statement to the resource policy - return { statementAdded: true, policyDependable: resource }; - } - } + public forResource(resource: CfnResource): IResourceWithPolicyV2 { + return new MyResourceWithPolicy(resource); } } diff --git a/packages/aws-cdk-lib/aws-s3/README.md b/packages/aws-cdk-lib/aws-s3/README.md index 516f03a4241e1..5fde02b4b5cb1 100644 --- a/packages/aws-cdk-lib/aws-s3/README.md +++ b/packages/aws-cdk-lib/aws-s3/README.md @@ -194,19 +194,30 @@ for the `AWS::S3::Bucket` CloudFormation type: ```ts nofixture import { CfnResource } from 'aws-cdk-lib'; -import { IResourcePolicyFactory, IResourceWithPolicyV2, PolicyStatement, ResourceWithPolicies } from 'aws-cdk-lib/aws-iam'; -import { Construct, IConstruct } from 'constructs'; +import { AddToResourcePolicyResult, IResourcePolicyFactory, IResourceWithPolicyV2, PolicyStatement, ResourceWithPolicies } from 'aws-cdk-lib/aws-iam'; +import { ResourceEnvironment } from 'aws-cdk-lib/interfaces'; +import { Construct } from 'constructs'; declare const scope: Construct; + +class MyResourceWithPolicy implements IResourceWithPolicyV2 { + public readonly env: ResourceEnvironment; + private readonly resource: CfnResource; + + constructor(resource: CfnResource) { + this.resource = resource; + this.env = resource.env; + } + + public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult { + // custom implementation to add the statement to the resource policy + return { statementAdded: true, policyDependable: this.resource }; + } +} + class MyFactory implements IResourcePolicyFactory { - forResource(resource: CfnResource): IResourceWithPolicyV2 { - return { - env: resource.env, - addToResourcePolicy(statement: PolicyStatement) { - // custom implementation to add the statement to the resource policy - return { statementAdded: true, policyDependable: resource }; - } - } + public forResource(resource: CfnResource): IResourceWithPolicyV2 { + return new MyResourceWithPolicy(resource); } }