Property 'policy' doesn't exist on type 'IKey' (TypeScript, AWS CDK)

I’m trying to access the IKey.policy property to delete the duplicate PolicyStatement from the PolicyDocument. I have been able to access it in the debugger, but not in the transpile. I have also tried deleting the principal from the PolicyStatement, but this has not been successful either.

The following are my AWS, CDK, Constructs, TypeScript, Node and NPM versions:

AWS: aws-cli/2.12.0 Python/3.11.4 Darwin/22.5.0 source/x86_64 prompt/off
CDK: 2.83.1 (build 006b542)
Constructs: 10.1.31
TypeScript: Version 4.3.2
Node: v16.18.1
NPM: 8.19.4

What can I do to access the IKey.policy property in the transpile?

To access the IKey.policy property in the transpile, you need to ensure that you have imported the necessary AWS SDK modules and that your IAM policy is correctly defined.

Here are the steps you can follow:

  1. Import the necessary AWS SDK modules. In your TypeScript file, add the following import statements at the top:

    import { IKey, PolicyStatement } from 'aws-sdk';
    
  2. Make sure you have defined your IAM policy correctly. Ensure that you have created a PolicyStatement object and added it to the PolicyDocument of your IKey object. For example:

    const key: IKey = ...; // Your IKey object
    
    const policyStatement: PolicyStatement = new PolicyStatement();
    // Configure the policy statement as needed
    // ...
    
    key.policy?.document?.removeStatements(policyStatement);
    

    Replace ... with your actual IKey object and configure the PolicyStatement as needed.

  3. Use the removeStatements method of the PolicyDocument to remove the duplicate PolicyStatement from the IKey.policy. This method removes all occurrences of the provided policy statement from the document.

    Note that the removeStatements method modifies the IKey.policy.document in-place, so you don’t need to reassign the value.

By following these steps, you should be able to access the IKey.policy property and remove the duplicate PolicyStatement from the PolicyDocument in the transpile.