prefer-grants-property
✅ recommended を使用した場合、このルールが有効になります。
従来、アクセス制御を持つリソースを表す L2 Construct では grant* メソッド(例: topic.grantPublish(role))が使用されていましたが、Grants クラスという新しいパターンが導入され、利便性の高い grants プロパティが追加されました。
grant* メソッドは現在も利用可能ですが、以下の引用 (PR#35782) のように、現在は非推奨であ流ため、grants プロパティを使用することが推奨されています。
The grantPublish etc methods on the L2 are now no longer recommended (though they will not be deprecated immediately to not disrupt existing code too much).
このルールは、新しい Grants クラスパターンをサポートする AWS CDK L2 Construct において、grant* メソッドではなく grants プロパティを使用することを強制します
🔧 使用方法
ts
// eslint.config.mjs
export default defineConfig([
{
// ... some configs
rules: {
"awscdk/prefer-grants-property": "error",
},
},
]);✅ 正しい例
ts
import { Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { Topic } from "aws-cdk-lib/aws-sns";
const role = new Role(stack, "MyRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
});
const topic = new Topic(stack, "MyTopic");
// ✅ grants プロパティを使用する
topic.grants.subscribe(role);
topic.grants.publish(role);❌ 不正な例
ts
import { Role, ServicePrincipal } from "aws-cdk-lib/aws-iam";
import { Topic } from "aws-cdk-lib/aws-sns";
const role = new Role(stack, "MyRole", {
assumedBy: new ServicePrincipal("lambda.amazonaws.com"),
});
const topic = new Topic(stack, "MyTopic");
// ❌ grants プロパティが利用可能な場合は grant* メソッドの使用を避ける
topic.grantSubscribe(role);
topic.grantPublish(role);