Skip to content

Commit

Permalink
CloudFormation: AWS::IAM::Role now supports RoleId (#7442)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblommers committed Mar 8, 2024
1 parent 06bfd7f commit b2ff3d9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion moto/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,13 +838,15 @@ def physical_resource_id(self) -> str:

@classmethod
def has_cfn_attr(cls, attr: str) -> bool:
return attr in ["Arn"]
return attr in ["Arn", "RoleId"]

def get_cfn_attribute(self, attribute_name: str) -> str:
from moto.cloudformation.exceptions import UnformattedGetAttTemplateException

if attribute_name == "Arn":
return self.arn
if attribute_name == "RoleId":
return self.id
raise UnformattedGetAttTemplateException()

def get_tags(self) -> List[Dict[str, str]]:
Expand Down
17 changes: 16 additions & 1 deletion tests/test_iam/test_iam_cloudformation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
- ec2.amazonaws.com
Action:
- 'sts:AssumeRole'
Outputs:
RootRole:
Value: !Ref RootRole
RoleARN:
Value: {"Fn::GetAtt": ["RootRole", "Arn"]}
RoleID:
Value: {"Fn::GetAtt": ["RootRole", "RoleId"]}
"""


Expand Down Expand Up @@ -1417,8 +1424,16 @@ def test_iam_cloudformation_create_role():
role = [res for res in resources if res["ResourceType"] == "AWS::IAM::Role"][0]
assert role["LogicalResourceId"] == "RootRole"

outputs = cf_client.describe_stacks(StackName=stack_name)["Stacks"][0]["Outputs"]
outputs = {o["OutputKey"]: o["OutputValue"] for o in outputs}

iam_client = boto3.client("iam", region_name="us-east-1")
assert len(iam_client.list_roles()["Roles"]) == 1
roles = iam_client.list_roles()["Roles"]
assert len(roles) == 1

assert roles[0]["RoleName"] == [v for k, v in outputs.items() if k == "RootRole"][0]
assert roles[0]["Arn"] == [v for k, v in outputs.items() if k == "RoleARN"][0]
assert roles[0]["RoleId"] == [v for k, v in outputs.items() if k == "RoleID"][0]

cf_client.delete_stack(StackName=stack_name)

Expand Down

0 comments on commit b2ff3d9

Please sign in to comment.