Skip to content

Role.addManagedPolicy does not work for imported roles #8307

@its-mirus-lu

Description

@its-mirus-lu

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

  1. Deploy one stack with an IAM role
  2. In a second stack create a managed policy
  3. In the second stack Import the role using aws_iam.Role.fromRoleArn (importedRole)
  4. In the second stack attempt to add the managed policy to importedRole via importedRole.attachManagedPolicy
Observed
  1. cdk synth does not show any associations made between the role and the managed policy
  2. using ManagedPolicy.attachToRole works however
Expected
  1. cdk synth (and subsequently) cdk deploy should associate the role and managed policy when Role.attachManagedPolicy is used

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-iamRelated to AWS Identity and Access ManagementbugThis issue is a bug.docs/inlineRelated to inline documentation of the API ReferencedocumentationThis is a problem with documentation.effort/smallSmall work item – less than a day of effortp2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions