@@ -5,7 +5,7 @@ import isEmpty from 'lodash/isEmpty'
55import { AWSError } from 'aws-sdk/lib/error'
66
77import IAM , {
8- AttachedPolicy ,
8+ AttachedPolicy , GetRoleResponse ,
99 ListAttachedRolePoliciesResponse ,
1010 ListRolePoliciesResponse ,
1111 ListRolesResponse ,
@@ -43,6 +43,35 @@ export interface RawAwsIamRole extends Omit<Role, 'Tags'> {
4343 Tags ?: TagMap
4444}
4545
46+ const roleByRoleName = async (
47+ iam : IAM ,
48+ { RoleName } : Role
49+ ) : Promise < { RoleName : string ; Role : Role } > =>
50+ new Promise ( resolve => {
51+ iam . getRole (
52+ { RoleName } ,
53+ ( err : AWSError , data : GetRoleResponse ) => {
54+ if ( err ) {
55+ errorLog . generateAwsErrorLog ( {
56+ err,
57+ functionName : 'iam:getRole' ,
58+ } )
59+ }
60+
61+ if ( ! isEmpty ( data ) ) {
62+ const { Role} = data
63+
64+ resolve ( {
65+ RoleName,
66+ Role,
67+ } )
68+ }
69+
70+ resolve ( null )
71+ }
72+ )
73+ } )
74+
4675const tagsByRoleName = async (
4776 iam : IAM ,
4877 { RoleName } : Role
@@ -136,6 +165,7 @@ export const listIamRoles = async (
136165 const policiesByRoleNamePromises = [ ]
137166 const tagsByRoleNamePromises = [ ]
138167 const managedPoliciesByRoleNamePromises = [ ]
168+ const roleByRoleNamePromises : Promise < { RoleName : string ; Role : Role } > [ ] = [ ]
139169
140170 iam . listRoles (
141171 { Marker : marker } ,
@@ -155,13 +185,15 @@ export const listIamRoles = async (
155185 managedPoliciesByRoleNamePromises . push (
156186 managedPoliciesByRoleName ( iam , role )
157187 )
188+ roleByRoleNamePromises . push ( roleByRoleName ( iam , role ) )
158189 } )
159190
160191 const tags = await Promise . all ( tagsByRoleNamePromises )
161192 const policies = await Promise . all ( policiesByRoleNamePromises )
162193 const managedPolicies = await Promise . all (
163194 managedPoliciesByRoleNamePromises
164195 )
196+ const detailedRoles = await Promise . all ( roleByRoleNamePromises )
165197
166198 result . push (
167199 ...roles . map (
@@ -173,6 +205,7 @@ export const listIamRoles = async (
173205 ) ,
174206 ...role ,
175207 region : globalRegionName ,
208+ RoleLastUsed : detailedRoles ?. find ( r => r . RoleName === RoleName ) ?. Role . RoleLastUsed ,
176209 Policies :
177210 policies
178211 ?. filter ( p => p ?. RoleName === RoleName )
0 commit comments