Skip to content

(aws-dynamodb): addToResourcePolicy has no effect #35062

@colifran

Description

@colifran

Describe the bug

Using addToResourcePolicy for Table does not actually add the resource policy to the DDB table in the synthesized CFN template.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Library Version

No response

Expected Behavior

The resource policy would be applied to the DDB table in the synthesized CFN template.

Current Behavior

The resource policy is not applied to the DDB table in the synthesized CFN template.

Reproduction Steps

Create the following stack:

import * as cdk from 'aws-cdk-lib';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import { PolicyDocument, PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';

export class CdkTableReproStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    new dynamodb.Table(this, 'SimpleTable', {
      tableName: 'simple-table',
      partitionKey: {
        name: 'id',
        type: dynamodb.AttributeType.STRING
      },
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
      resourcePolicy: new PolicyDocument({
        statements: [
          new PolicyStatement({
            actions: [
              'dynamodb:BatchGetItem',
              'dynamodb:DeleteItem',
              'dynamodb:GetItem',
              'dynamodb:Query',
              'dynamodb:Scan',
              'dynamodb:Describe*',
            ],
            resources: ['*'],
          }),
        ],
      }),
    });
  }
}

Run cdk synth and observe the following CFN template:

Resources:
  SimpleTableC6BC762D:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      ResourcePolicy:
        PolicyDocument:
          Statement:
            - Action:
                - dynamodb:BatchGetItem
                - dynamodb:DeleteItem
                - dynamodb:GetItem
                - dynamodb:Query
                - dynamodb:Scan
                - dynamodb:Describe*
              Effect: Allow
              Resource: "*"
          Version: "2012-10-17"
      TableName: simple-table
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: CdkTableReproStack/SimpleTable/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/yXGSwqAIBAA0LO018ncROtuUO1j1AnUUmj6ENHdg1q9p0GrGlSBJ0vropy9gbvf0EaBJ4/uSrhkZ+Ae0Mwk2il9eURHnPfV0iNSdgSBy0MrqBpQRWDv5bqnzS8E3e8LNqEfg2gAAAA=
    Metadata:
      aws:cdk:path: CdkTableReproStack/CDKMetadata/Default
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]

Now update the stack as follows:

import * as cdk from 'aws-cdk-lib';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
import { PolicyStatement } from 'aws-cdk-lib/aws-iam';
import { Construct } from 'constructs';

export class CdkTableReproStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const table = new dynamodb.Table(this, 'SimpleTable', {
      tableName: 'simple-table',
      partitionKey: {
        name: 'id',
        type: dynamodb.AttributeType.STRING
      },
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
      removalPolicy: cdk.RemovalPolicy.DESTROY,
    });

    table.addToResourcePolicy(new PolicyStatement({
      actions: [
        'dynamodb:BatchGetItem',
        'dynamodb:DeleteItem',
        'dynamodb:GetItem',
        'dynamodb:Query',
        'dynamodb:Scan',
        'dynamodb:Describe*',
      ],
      resources: ['*'],
    }));
  }
}

Run cdk synth and observe that the CFN template no longer includes the policy:

Resources:
  SimpleTableC6BC762D:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      TableName: simple-table
    UpdateReplacePolicy: Delete
    DeletionPolicy: Delete
    Metadata:
      aws:cdk:path: CdkTableReproStack/SimpleTable/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Analytics: v2:deflate64:H4sIAAAAAAAA/yXGSwqAIBAA0LO018ncROtuUO1j1AnUUmj6ENHdg1q9p0GrGlSBJ0vropy9gbvf0EaBJ4/uSrhkZ+Ae0Mwk2il9eURHnPfV0iNSdgSBy0MrqBpQRWDv5bqnzS8E3e8LNqEfg2gAAAA=
    Metadata:
      aws:cdk:path: CdkTableReproStack/CDKMetadata/Default
Parameters:
  BootstrapVersion:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /cdk-bootstrap/hnb659fds/version
    Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]

Possible Solution

I think resourcePolicy needs to be lazily evaluated. I did the following locally and it fixed it:

First add this to the constructor:

this.resourcePolicy = props.resourcePolicy;

Update resourcePolicy in CfnTable to:

resourcePolicy: Lazy.any({ produce: () => this.resourcePolicy ? { policyDocument: this.resourcePolicy } : undefined })

Additional Information/Context

No response

AWS CDK Library version (aws-cdk-lib)

[email protected]

AWS CDK CLI version

[email protected]

Node.js Version

v20.19.0

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

@aws-cdk/aws-dynamodbRelated to Amazon DynamoDBbugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions