@@ -16,6 +16,12 @@ import { RawAwsSubnet } from '../subnet/data'
1616import { RawAwsEcsContainer } from '../ecsContainer/data'
1717import { RawAwsSystemsManagerInstance } from '../systemsManagerInstance/data'
1818import { ssmManagedInstanceArn } from '../../utils/generateArns'
19+ import { RawAwsElasticBeanstalkEnv } from '../elasticBeanstalkEnvironment/data'
20+ import { RawAwsEksCluster } from '../eksCluster/data'
21+ import { getEksClusterName , getElasticBeanstalkEnvId } from './utils'
22+ import { RawAwsInstanceProfile } from '../iamInstanceProfile/data'
23+ import { globalRegionName } from '../../enums/regions'
24+ import { RawAwsIamRole } from '../iamRole/data'
1925
2026/**
2127 * EC2
@@ -35,6 +41,7 @@ export default ({
3541 KeyPairName ?: string
3642 Tags ?: TagList
3743 IamInstanceProfile : IamInstanceProfile
44+ IamRolesArn ?: string [ ]
3845 }
3946 region : string
4047} ) : { [ key : string ] : ServiceConnection [ ] } => {
@@ -44,6 +51,9 @@ export default ({
4451 SecurityGroups : instanceSecurityGroups = [ ] ,
4552 NetworkInterfaces : instanceNetworkInterfaces = [ ] ,
4653 SubnetId : subnetId ,
54+ Tags : tags ,
55+ IamInstanceProfile : iamInstanceProfile ,
56+ IamRolesArn : rolesArn ,
4757 } = instance
4858
4959 /**
@@ -189,17 +199,37 @@ export default ({
189199 id : subnet . SubnetId ,
190200 resourceType : services . subnet ,
191201 relation : 'child' ,
192- field : 'subnet ' ,
202+ field : 'subnets ' ,
193203 } )
194204 }
195205 }
196206 }
197207
198208 /**
199- * Find EKS
209+ * Find EKS cluster
200210 * related to this EC2
201211 */
202- // TODO: Implement when eks service is ready
212+ const eksClusterName = getEksClusterName ( tags )
213+ const eksClusters : {
214+ name : string
215+ data : { [ property : string ] : any [ ] }
216+ } = data . find ( ( { name } ) => name === services . eksCluster )
217+ if ( eksClusters ?. data ?. [ region ] ) {
218+ const eksClustersInRegion : RawAwsEksCluster [ ] = eksClusters . data [
219+ region
220+ ] . filter ( ( { name } : RawAwsEksCluster ) => name === eksClusterName )
221+
222+ if ( ! isEmpty ( eksClustersInRegion ) ) {
223+ for ( const eksCluster of eksClustersInRegion ) {
224+ connections . push ( {
225+ id : eksCluster . arn ,
226+ resourceType : services . eksCluster ,
227+ relation : 'child' ,
228+ field : 'eksCluster' ,
229+ } )
230+ }
231+ }
232+ }
203233
204234 /**
205235 * Find ECS Container
@@ -210,9 +240,9 @@ export default ({
210240 data : { [ property : string ] : any [ ] }
211241 } = data . find ( ( { name } ) => name === services . ecsContainer )
212242 if ( ecsContainers ?. data ?. [ region ] ) {
213- const containersInRegion : RawAwsEcsContainer [ ] = ecsContainers . data [ region ] . filter (
214- ( { ec2InstanceId } ) => ec2InstanceId === id
215- )
243+ const containersInRegion : RawAwsEcsContainer [ ] = ecsContainers . data [
244+ region
245+ ] . filter ( ( { ec2InstanceId } ) => ec2InstanceId === id )
216246
217247 if ( ! isEmpty ( containersInRegion ) ) {
218248 for ( const container of containersInRegion ) {
@@ -230,18 +260,24 @@ export default ({
230260 * Find SSM managed instances
231261 * related to this EC2 instance
232262 */
233- const instances : {
263+ const instances : {
234264 name : string
235265 data : { [ property : string ] : any [ ] }
236266 } = data . find ( ( { name } ) => name === services . systemsManagerInstance )
237267 if ( instances ?. data ?. [ region ] ) {
238- const dataInRegion : RawAwsSystemsManagerInstance [ ] = instances . data [ region ] . filter (
268+ const dataInRegion : RawAwsSystemsManagerInstance [ ] = instances . data [
269+ region
270+ ] . filter (
239271 ( { InstanceId } : RawAwsSystemsManagerInstance ) => InstanceId === id
240272 )
241273
242274 if ( ! isEmpty ( dataInRegion ) ) {
243275 for ( const ssmInstance of dataInRegion ) {
244- const arn = ssmManagedInstanceArn ( { region, account, name : ssmInstance . InstanceId } )
276+ const arn = ssmManagedInstanceArn ( {
277+ region,
278+ account,
279+ name : ssmInstance . InstanceId ,
280+ } )
245281 connections . push ( {
246282 id : arn ,
247283 resourceType : services . systemsManagerInstance ,
@@ -254,9 +290,82 @@ export default ({
254290
255291 /**
256292 * Find Elastic Beanstalk
257- * related to this EC2 loadbalancer
293+ * related to this EC2 instance
294+ */
295+ const elasticBeanstalkEnvId = getElasticBeanstalkEnvId ( tags )
296+ const elasticBeanstalkEnvs : {
297+ name : string
298+ data : { [ property : string ] : any [ ] }
299+ } = data . find ( ( { name } ) => name === services . elasticBeanstalkEnv )
300+ if ( elasticBeanstalkEnvs ?. data ?. [ region ] ) {
301+ const elasticBeanstalkEnvsInRegion : RawAwsElasticBeanstalkEnv [ ] =
302+ elasticBeanstalkEnvs . data [ region ] . filter (
303+ ( { EnvironmentId } : RawAwsElasticBeanstalkEnv ) =>
304+ elasticBeanstalkEnvId === EnvironmentId
305+ )
306+
307+ if ( ! isEmpty ( elasticBeanstalkEnvsInRegion ) ) {
308+ for ( const elasticBeanstalkEnv of elasticBeanstalkEnvsInRegion ) {
309+ connections . push ( {
310+ id : elasticBeanstalkEnv . EnvironmentId ,
311+ resourceType : services . elasticBeanstalkEnv ,
312+ relation : 'child' ,
313+ field : 'elasticBeanstalkEnv' ,
314+ } )
315+ }
316+ }
317+ }
318+
319+ /**
320+ * Find IAM Instance Profiles
321+ * related to this EC2 instance
322+ */
323+ const iamInstanceProfiles : {
324+ name : string
325+ data : { [ property : string ] : any [ ] }
326+ } = data . find ( ( { name } ) => name === services . iamInstanceProfile )
327+ if ( iamInstanceProfiles ?. data ?. [ globalRegionName ] ) {
328+ const dataAtRegion : RawAwsInstanceProfile [ ] = iamInstanceProfiles . data [
329+ globalRegionName
330+ ] . filter ( instanceProfile => instanceProfile . Arn === iamInstanceProfile ?. Arn )
331+ if ( ! isEmpty ( dataAtRegion ) ) {
332+ for ( const iamInstance of dataAtRegion ) {
333+ const { InstanceProfileId : instanceProfileId } : RawAwsInstanceProfile = iamInstance
334+
335+ connections . push ( {
336+ id : instanceProfileId ,
337+ resourceType : services . iamInstanceProfile ,
338+ relation : 'child' ,
339+ field : 'iamInstanceProfile' ,
340+ } )
341+ }
342+ }
343+ }
344+
345+ /**
346+ * Find IAM Roles
347+ * related to this EC2 instance
258348 */
259- // TODO: Implement when eb service is ready
349+ const roles : { name : string ; data : { [ property : string ] : any [ ] } } =
350+ data . find ( ( { name } ) => name === services . iamRole )
351+
352+ if ( roles ?. data ?. [ globalRegionName ] ) {
353+ const dataAtRegion : RawAwsIamRole [ ] = roles . data [ globalRegionName ] . filter (
354+ ( { Arn } : RawAwsIamRole ) => rolesArn ?. includes ( Arn )
355+ )
356+ if ( ! isEmpty ( dataAtRegion ) ) {
357+ for ( const iamRole of dataAtRegion ) {
358+ const { Arn : arn } :RawAwsIamRole = iamRole
359+
360+ connections . push ( {
361+ id : arn ,
362+ resourceType : services . iamRole ,
363+ relation : 'child' ,
364+ field : 'iamRole' ,
365+ } )
366+ }
367+ }
368+ }
260369
261370 const ec2Result = {
262371 [ id ] : connections ,
0 commit comments