Skip to content

feat(iam): support Role.fromLookup() method #33603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Apr 23, 2025
Merged

Conversation

go-to-k
Copy link
Contributor

@go-to-k go-to-k commented Feb 27, 2025

Issue # (if applicable)

Closes #33602.

Reason for this change

There will be many cases where IAM roles will be created outside the CFn stack and used.
Importing actual existing roles from AWS accounts is very convenience. It is also useful to be able to make an error if a role does not exist.

On the other hand, a generic Context Provider for CloudControl API has been added in aws-cdk-cli.

aws/aws-cdk-cli#138

This allows us to implement new context methods.

Description of changes

Add Role.fromLookup method using the new context provider.

Describe any new or updated permissions being added

Description of how you validated changes

Both unit and integ tests.

Checklist


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@aws-cdk-automation aws-cdk-automation requested a review from a team February 27, 2025 07:08
@github-actions github-actions bot added feature-request A feature should be added or improved. p2 distinguished-contributor [Pilot] contributed 50+ PRs to the CDK labels Feb 27, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This review is outdated)

@go-to-k go-to-k marked this pull request as ready for review February 27, 2025 11:44
Copy link

codecov bot commented Mar 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.98%. Comparing base (0923b5e) to head (10846af).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #33603   +/-   ##
=======================================
  Coverage   83.98%   83.98%           
=======================================
  Files         120      120           
  Lines        6976     6976           
  Branches     1178     1178           
=======================================
  Hits         5859     5859           
  Misses       1005     1005           
  Partials      112      112           
Flag Coverage Δ
suite.unit 83.98% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
packages/aws-cdk ∅ <ø> (∅)
packages/aws-cdk-lib/core 83.98% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@aws-cdk-automation aws-cdk-automation dismissed their stale review March 2, 2025 07:52

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Mar 2, 2025
github-merge-queue bot pushed a commit to aws/aws-cdk-cli that referenced this pull request Mar 25, 2025
…PI context provider (#211)

CDK app with CC API Context Provider fails if the resource we want to
get doesn't exist.

```
[Error at /LookupStack] Encountered CC API error while getting resource MyLookupTestRole. Error: ResourceNotFoundException: AWS::IAM::Role Handler returned status FAILED: The role with name MyLookupTestRole cannot be found. (Service: Iam, Status Code: 404, Request ID: xxxx-xxx-xxxx-xxxx) (HandlerErrorCode: NotFound, RequestToken: xxxx-xxx-xxxx-xxxx)
```

Also CC API Context Provider (`CcApiContextProviderPlugin`) cannot
ignore errors even if `ignoreErrorOnMissingContext` is passed in
aws-cdk-lib because the current code in aws-cdk-**cli** doesn't handle
the property.

Sample cdk-lib code (based on my
[PR](aws/aws-cdk#33603) for IAM Role
fromLookup):

```ts
    const response: {[key: string]: any}[] = ContextProvider.getValue(scope, {
      provider: cxschema.ContextProvider.CC_API_PROVIDER,
      props: {
        typeName: 'AWS::IAM::Role',
        exactIdentifier: options.roleName,
        propertiesToReturn: [
          'Arn',
        ],
      } as cxschema.CcApiContextQuery,
      dummyValue: [
        {
          // eslint-disable-next-line @cdklabs/no-literal-partition
          Arn: 'arn:aws:iam::123456789012:role/DUMMY_ARN',
        },
      ],
      ignoreErrorOnMissingContext: options.returnDummyRoleOnMissing, // added
    }).value;
```

However it would be good if the provider can ignore errors and return
any dummy value to cdk library. This allows all resources to be **used
if available, or created if not.**

Actually, SSM and KMS provider can ignore errors:

KMS:
https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/context-providers/keys.ts#L43-L48

SSM:
https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/context-providers/ssm-parameters.ts#L27-L30

For example, in cdk-lib, we can specify
[ignoreErrorOnMissingContext](https://github.com/aws/aws-cdk/blob/v2.182.0/packages/aws-cdk-lib/aws-kms/lib/key.ts#L741-L744)
in the `fromLookup`. The `dummyValue` and `ignoreErrorOnMissingContext`
properties can also be specified in
[GetContextValueOptions](https://github.com/go-to-k/aws-cdk/blob/45a276fd0fc9b7b08f69f7faf3d0091796ab1663/packages/aws-cdk-lib/core/lib/context-provider.ts#L31-L45).

## cli-integ

aws/aws-cdk-cli-testing#50

---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license

---------

Co-authored-by: Rico Hermans <[email protected]>
@github-actions github-actions bot added the effort/medium Medium work item – several days of effort label Apr 3, 2025
Copy link
Contributor

@paulhcsun paulhcsun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @go-to-k, thanks for the contribution! Just one small change and then I'm happy to approve!

@go-to-k
Copy link
Contributor Author

go-to-k commented Apr 23, 2025

@paulhcsun Thanks for your review! I fixed it.

@go-to-k go-to-k requested a review from paulhcsun April 23, 2025 04:16
@paulhcsun
Copy link
Contributor

@Mergifyio update

Thanks for the contribution @go-to-k!!

Copy link
Contributor

mergify bot commented Apr 23, 2025

update

✅ Branch has been successfully updated

Copy link
Contributor

mergify bot commented Apr 23, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 1c5389b
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

Copy link
Contributor

mergify bot commented Apr 23, 2025

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 9e3cbf6 into aws:main Apr 23, 2025
13 of 15 checks passed
Copy link
Contributor

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

iam: support Role.fromLookup() method
3 participants