aws-cdk: Role.addManagedPolicy does not work for imported roles
Inconsistent Behavior: aws_iam.Role.attachManagedPolicy vs aws_iam.ManagedPolicy.attachToRole
The Scenario
TLDR; aws_iam.Role.attachManagedPolicy does not attach the specified managed policy to the role, aws_iam.ManagedPolicy.attachToRole does.
I have two stacks: one stack deploys roles another deploys an application stack (both are in the same account). The roles stack is deployed first.
In the application stack, I would like to attach a managed policy to a role in the first.
Attempting to use aws_iam.Role.attachManagedPolicy does not create the association but aws_iam.ManagedPolicy.attachToRole does.
Environment
- CDK CLI Version: 1.39.0 (build 5d727c1)
- Module Version: 1.38.0
- Node.js Version: v14.0.0
- OS: macOS Mojave 10.14.6 (18G4032)
- Language: Typescript and Python
Other information
Steps to reproduce
- Deploy one stack with an IAM role
- In a second stack create a managed policy
- In the second stack Import the role using aws_iam.Role.fromRoleArn (importedRole)
- In the second stack attempt to add the managed policy to importedRole via importedRole.attachManagedPolicy
Observed
- cdk synth does not show any associations made between the role and the managed policy
- using ManagedPolicy.attachToRole works however
Expected
- cdk synth (and subsequently) cdk deploy should associate the role and managed policy when Role.attachManagedPolicy is used
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 9
- Comments: 16 (6 by maintainers)
Commits related to this issue
- fix(iam): does not work for imported roles #8307 — committed to stm29/aws-cdk by stm29 2 years ago
- fix(iam): `Role.addManagedPolicy()` does not work for imported roles `IRole` #8307 (#31212) ### Issue #8307 Closes #8307 ### Reason for this change - This addManagedPolicy() just does nothing,... — committed to cpaluch-aws/aws-cdk by stm29 2 years ago
https://github.com/aws/aws-cdk/blob/070f5ecebfba8a3f9b5771b251ee9b584aa89b67/packages/%40aws-cdk/aws-iam/lib/private/imported-role.ts#L70
the function addManagedPolicy for ImportedRole only adds a warning
Not adding managed policy: ${policy.managedPolicyArn} to imported role: ${this.roleName}, is this a missing implementation or an intended behavior?the warning isn’t even really shown to the user, it just prints
Is there any other way to add an AWS Managed Policy to an Imported Role?
This is still wrong in the documentation when importing an existing role.
One can set a
mutableoption when importing the role (by default it’s set totrue) The docs say:Which is untrue. You may only modify the role by adding inline policies. Not managed policies.
The workaround I found is below. It’s not perfect, but it’s pretty good:
Example
Just run into this bug, trying to configure some policies for roles managed outside of cdk app 😢
Could this be resolved by changing addManagedPolicy to take a
ManagedPolicyinstead ofIManagedPolicyas its argument (and then calling the appropriate method on the managed policy to attach it to the role)?I feel like this could fit in with the design guidelines - “It’s okay to enable multiple ways to achieve the same thing, in order to make it more natural for users who come from different mental models”, even though it technically violates [awslint:ref-via-interface]. Also the
attachInlinePolicymethod on imported roles requires aPolicy(opposed to anIPolicy), so this wouldn’t be especially out of place.As a workaround, you can use an AwsCustomResource to attach a managed policy to a role:
We just hit this issue. Especially surprising that even a warning is not getting printed. https://github.com/aws/aws-cdk/blob/master/packages/@aws-cdk/aws-iam/lib/role.ts#L227
It looks like addManagedPolicy is not implemented… https://github.com/aws/aws-cdk/blob/v1.42.1/packages/@aws-cdk/aws-iam/lib/role.ts#L216
Is there a way that this can be clearly stated in documentation?