Skip to content

Commit 9e3cbf6

Browse files
authored
feat(iam): support Role.fromLookup() method (#33603)
### 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 - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent e7a6e14 commit 9e3cbf6

13 files changed

+507
-1
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/LookupRoleStack.assets.json

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"Resources": {
3+
"HelloPolicyD59007DF": {
4+
"Type": "AWS::IAM::Policy",
5+
"Properties": {
6+
"PolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "ec2:*",
10+
"Effect": "Allow",
11+
"Resource": "*"
12+
}
13+
],
14+
"Version": "2012-10-17"
15+
},
16+
"PolicyName": "Default",
17+
"Roles": [
18+
"MyLookupTestRole"
19+
]
20+
}
21+
}
22+
},
23+
"Outputs": {
24+
"LookupRoleName": {
25+
"Value": "MyLookupTestRole"
26+
}
27+
},
28+
"Parameters": {
29+
"BootstrapVersion": {
30+
"Type": "AWS::SSM::Parameter::Value<String>",
31+
"Default": "/cdk-bootstrap/hnb659fds/version",
32+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
33+
}
34+
},
35+
"Rules": {
36+
"CheckBootstrapVersion": {
37+
"Assertions": [
38+
{
39+
"Assert": {
40+
"Fn::Not": [
41+
{
42+
"Fn::Contains": [
43+
[
44+
"1",
45+
"2",
46+
"3",
47+
"4",
48+
"5"
49+
],
50+
{
51+
"Ref": "BootstrapVersion"
52+
}
53+
]
54+
}
55+
]
56+
},
57+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
58+
}
59+
]
60+
}
61+
}
62+
}

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/cdk.out

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/integ.json

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/integiamrolefromlookupDefaultTestDeployAssert63955306.assets.json

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/integiamrolefromlookupDefaultTestDeployAssert63955306.template.json

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/manifest.json

+171
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-iam/test/integ.role-from-lookup.js.snapshot/tree.json

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { App, CfnOutput, Stack } from 'aws-cdk-lib';
2+
import { IntegTest } from '@aws-cdk/integ-tests-alpha';
3+
import { Policy, PolicyStatement, Role } from 'aws-cdk-lib/aws-iam';
4+
5+
const roleName = 'MyLookupTestRole';
6+
7+
const app = new App();
8+
9+
const stack = new Stack(app, 'LookupRoleStack', {
10+
env: {
11+
account: process.env.CDK_INTEG_ACCOUNT ?? process.env.CDK_DEFAULT_ACCOUNT,
12+
region: process.env.CDK_INTEG_REGION ?? process.env.CDK_DEFAULT_REGION,
13+
},
14+
});
15+
16+
const lookupRole = Role.fromLookup(stack, 'LookupRole', {
17+
roleName,
18+
});
19+
20+
const policy = new Policy(stack, 'HelloPolicy', { policyName: 'Default' });
21+
policy.addStatements(new PolicyStatement({ actions: ['ec2:*'], resources: ['*'] }));
22+
policy.attachToRole(lookupRole);
23+
24+
new CfnOutput(stack, 'LookupRoleName', { value: lookupRole.roleName });
25+
26+
new IntegTest(app, 'integ-iam-role-from-lookup', {
27+
enableLookups: true,
28+
stackUpdateWorkflow: false,
29+
testCases: [stack],
30+
// create the role before the test and delete it after
31+
hooks: {
32+
preDeploy: [`aws iam create-role --role-name ${roleName} --assume-role-policy-document file://policy-document.json`],
33+
postDestroy: [`aws iam delete-role --role-name ${roleName}`],
34+
},
35+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Principal": {
7+
"Service": "sqs.amazonaws.com"
8+
},
9+
"Action": "sts:AssumeRole"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)