aws / aws/aws-cdk

Role.addToPolicy does not exist for imported roles

Open
#24,195 3 comments 1 reaction 0 assignees View on GitHub
@aws-cdk/aws-iam bug p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
1d 19h
Merged PRs (30d)
71

Description

### Describe the bug

I am trying to import the role, however just like #8307 addTopolicy does not exist on imported role.

### Expected Behavior

addToPolicy should have been working on the import role.

### Current Behavior

The method does not exist

### Reproduction Steps

```
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3'
import { Effect, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { IRole } from 'aws-cdk-lib/aws-iam';

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

const statement = new PolicyStatement({
effect: Effect.ALLOW,
actions: ['execute-api:Invoke'],
resources: [
//SOME RESOURCE
]
})
const role_name = 'my_role_for_ec2_testing'
const role = Role.fromRoleName(this, 'Role', role_name)
}

role.addToPolicy(statement)

}
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.44.0

### Framework Version

_No response_

### Node.js Version

16.17.0

### OS

macos

### Language

Typescript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start at the TypeScript IAM role APIs for Role.fromRoleName and IRole, and compare this report with issue #8307. Confirm whether an imported role is intended to support addToPolicy, then add or update the relevant coverage so the reproduction's expected behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
authorization, cloud
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.