Skip to content

Commit 652f2f7

Browse files
committed
feat: Added iamInstanceProfile connection
1 parent 832fc7b commit 652f2f7

5 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/services/ec2/connections.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { ssmManagedInstanceArn } from '../../utils/generateArns'
1919
import { RawAwsElasticBeanstalkEnv } from '../elasticBeanstalkEnvironment/data'
2020
import { RawAwsEksCluster } from '../eksCluster/data'
2121
import { getEksClusterName, getElasticBeanstalkEnvId } from './utils'
22+
import { RawAwsInstanceProfile } from '../iamInstanceProfile/data'
23+
import { globalRegionName } from '../../enums/regions'
2224

2325
/**
2426
* EC2
@@ -48,6 +50,7 @@ export default ({
4850
NetworkInterfaces: instanceNetworkInterfaces = [],
4951
SubnetId: subnetId,
5052
Tags: tags,
53+
IamInstanceProfile: iamInstanceProfile,
5154
} = instance
5255

5356
/**
@@ -293,7 +296,7 @@ export default ({
293296
} = data.find(({ name }) => name === services.elasticBeanstalkEnv)
294297
if (elasticBeanstalkEnvs?.data?.[region]) {
295298
const elasticBeanstalkEnvsInRegion: RawAwsElasticBeanstalkEnv[] =
296-
elasticBeanstalkEnvs.data[region].filter(
299+
elasticBeanstalkEnvs.data[region].filter(
297300
({ EnvironmentId }: RawAwsElasticBeanstalkEnv) =>
298301
elasticBeanstalkEnvId === EnvironmentId
299302
)
@@ -310,6 +313,32 @@ export default ({
310313
}
311314
}
312315

316+
/**
317+
* Find IAM Instance Profiles
318+
* related to this EC2 instance
319+
*/
320+
const iamInstanceProfiles: {
321+
name: string
322+
data: { [property: string]: any[] }
323+
} = data.find(({ name }) => name === services.iamInstanceProfile)
324+
if (iamInstanceProfiles?.data?.[globalRegionName]) {
325+
const dataAtRegion: RawAwsInstanceProfile[] = iamInstanceProfiles.data[
326+
globalRegionName
327+
].filter(instanceProfile => instanceProfile.Arn === iamInstanceProfile?.Arn)
328+
if (!isEmpty(dataAtRegion)) {
329+
for (const iamInstance of dataAtRegion) {
330+
const { InstanceProfileId: instanceProfileId }: RawAwsInstanceProfile = iamInstance
331+
332+
connections.push({
333+
id: instanceProfileId,
334+
resourceType: services.iamInstanceProfile,
335+
relation: 'child',
336+
field: 'iamInstanceProfile',
337+
})
338+
}
339+
}
340+
}
341+
313342
const ec2Result = {
314343
[id]: connections,
315344
}

src/services/ec2/format.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export default ({
4545
fingerprint,
4646
tags: keyPairTags = {},
4747
} = {},
48-
IamInstanceProfile: iamInstanceProfile,
4948
cloudWatchMetricData,
5049
PlatformDetails: platformDetails,
5150
InstanceLifecycle: instanceLifecycle,
@@ -114,7 +113,6 @@ export default ({
114113
sourceDestCheck: sourceDestCheck ? t.yes : t.no,
115114
availabilityZone: placement?.AvailabilityZone || '',
116115
cpuThreadsPerCore: cpuOptions?.ThreadsPerCore || 0,
117-
iamInstanceProfile: iamInstanceProfile?.Arn || '',
118116
deletionProtection: deletionProtection ? t.yes : t.no,
119117
primaryNetworkInterface: networkInterfaceId,
120118
metadataOptions: {

src/services/ec2/schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ type awsEc2 @key(fields: "arn") {
9696
sourceDestCheck: String @search(by: [hash, regexp])
9797
availabilityZone: String @search(by: [hash, regexp])
9898
cpuThreadsPerCore: Int @search
99-
iamInstanceProfile: String @search(by: [hash, regexp]) # TODO: use to make a connection to a iamRole
10099
deletionProtection: String @search(by: [hash, regexp])
101100
dailyCost: awsTotalBillingInfo
102101
primaryNetworkInterface: String @search(by: [hash, regexp])
@@ -122,4 +121,5 @@ type awsEc2 @key(fields: "arn") {
122121
systemsManagerInstance: [awsSystemsManagerInstance] @hasInverse(field: ec2Instance)
123122
eksCluster: [awsEksCluster] @hasInverse(field: ec2Instances)
124123
elasticBeanstalkEnv: [awsElasticBeanstalkEnv] @hasInverse(field: ec2Instances)
124+
iamInstanceProfile: [awsIamInstanceProfile] @hasInverse(field: ec2Instances)
125125
}

src/services/iamInstanceProfile/schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ type awsIamInstanceProfile @key(fields: "id") {
77
createDate: DateTime
88
tags: [awsRawTag]
99
iamRole: [awsIamRole] @hasInverse(field: iamInstanceProfiles)
10+
ec2Instances: [awsEc2] @hasInverse(field: iamInstanceProfile)
1011
}

src/types/generated.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ export type AwsEc2 = {
15181518
emrInstance?: Maybe<Array<Maybe<AwsEmrInstance>>>;
15191519
ephemeralBlockDevices?: Maybe<Array<Maybe<AwsEc2Blockdevice>>>;
15201520
hibernation?: Maybe<Scalars['String']>;
1521-
iamInstanceProfile?: Maybe<Scalars['String']>;
1521+
iamInstanceProfile?: Maybe<Array<Maybe<AwsIamInstanceProfile>>>;
15221522
id: Scalars['String'];
15231523
instanceLifecycle?: Maybe<Scalars['String']>;
15241524
instanceState?: Maybe<Scalars['String']>;
@@ -3110,6 +3110,7 @@ export type AwsIamInstanceProfile = {
31103110
accountId: Scalars['String'];
31113111
arn: Scalars['String'];
31123112
createDate?: Maybe<Scalars['DateTime']>;
3113+
ec2Instances?: Maybe<Array<Maybe<AwsEc2>>>;
31133114
iamRole?: Maybe<Array<Maybe<AwsIamRole>>>;
31143115
id: Scalars['String'];
31153116
name?: Maybe<Scalars['String']>;

0 commit comments

Comments
 (0)