question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Attaching Managed Policy to Role failing

See original GitHub issue

Note: for support questions, please first reference our documentation, then use Stackoverflow. This repository’s issues are intended for feature requests and bug reports.

  • I’m submitting a …

    • 🪲 bug report
    • 🚀 feature request
    • 📚 construct library gap
    • ☎️ security issue or vulnerability => Please see policy
    • ❓ support request => Please see note at the top of this template.
  • What is the current behavior? If the current behavior is a 🪲bug🪲: Please provide the steps to reproduce

Attaching a managed policy to a Role.

In 1.1.0 there is a method fromAwsManagedPolicyName. It takes a single parameter: the managed policy name. In previous versions there was a method attachManagedPolicy which took the ARN of the managed policy.

in 1.1.0:

// attach managed policy to role
const managedPolicy = ManagedPolicy.fromAwsManagedPolicyName("MyManagedPolicy")
buildRole.addManagedPolicy(managedPolicy);

I get the following error:

Policy arn:aws:iam::aws:policy/MyManagedPolicy does not exist or is not attachable.
(Service: AmazonIdentityManagement; Status Code: 404; Error Code: NoSuchEntity; Request ID: 7c2ebbde-ac71-11e9-89dd-879d24923f1f)

I can see that the arn for the managed policy is constructed in this method - and is missing the account reference.

In the CDK code:

https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-iam/lib/managed-policy.ts

  public static fromAwsManagedPolicyName(managedPolicyName: string): IManagedPolicy {
    class AwsManagedPolicy implements IManagedPolicy {
      public readonly managedPolicyArn = Lazy.stringValue({
        produce(ctx: IResolveContext) {
          return Stack.of(ctx.scope).formatArn({
            service: "iam",
            region: "", // no region for managed policy
            account: "aws", // the account for a managed policy is 'aws'
            resource: "policy",
            resourceName: managedPolicyName
          });
        }
      });
    }
    return new AwsManagedPolicy();
  }

I can see from the error message and reading the CDK code that the account in the arn is set to aws - not the correct account number:

  • What is the expected behavior (or behavior of feature suggested)?

Using the example above:

managedPolicyArn should be set to

arn:aws:iam::12345678987654:policy/MyManagedPolicy

not

arn:aws:iam::aws:policy/MyManagedPolicy (current behaviour)

In this way the Managed Policy will be correctly identified.

  • What is the motivation / use case for changing the behavior or adding this feature?

To allow managed policies to be assigned to a role. This is a feature we have previously used to manage our codebuild projects.

  • Please tell us about your environment:

    • CDK CLI Version: 1.1.0
    • Module Version: 1.1.0
    • OS: Windows 10 , Ubuntu
    • Language: TypeScript
  • Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. associated pull-request, stackoverflow, gitter, etc)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

12reactions
michael-barkercommented, Aug 11, 2020

I had a similar issue but mine was caused by not adding the service-role/ prefix.

7reactions
ireneaguilar-seatcommented, Jan 25, 2021

I had a similar issue but mine was caused by not adding the service-role/ prefix.

Some managed policy names start with “service-role/”, some start with “job-function/”, and some don’t start with anything. Do include the prefix when constructing this object.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting IAM policies - AWS Identity and Access ...
You can attach an identity-based policy to a principal (or identity), such as an IAM group, user, or role. Identity-based policies include AWS...
Read more >
Attaching a managed policy ARN - Cloudera Documentation
Attaching a managed policy ARN. For security reasons, if you do not want to provide PutRolePolicy permission in your cross account role, which...
Read more >
Top 5 Common AWS IAM Errors you Need to Fix | A Cloud Guru
Verify the IAM policy attached to the user in your development account grants that user permission to the sts:AssumeRole action for the role ......
Read more >
IAM tutorial: Create and attach your first customer managed ...
The policy you create allows an IAM test user to sign in directly to the Amazon Web Services Management Console with read-only permissions....
Read more >
AWS IAM Managed Policies — Use them wisely, or do not use ...
In this case the user can attach (almost) any role, including administrative roles to any EC2 instance! They can also access any EBS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found