-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1245 from RoadieHQ/sc-18900-update-aws-providers
Update AWS providers
- Loading branch information
Showing
12 changed files
with
251 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@roadiehq/catalog-backend-module-aws': major | ||
--- | ||
|
||
Breaking change on entity output level, no code changes needed for most use cases. | ||
|
||
Updating entities provided by DDB and Lambda providers to be of kind Resource instead of a Component. | ||
|
||
Adding an additional EntityProvider to create entities from EC2 instances. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
plugins/backend/catalog-backend-module-aws/src/providers/AWSEC2Provider.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright 2024 Larder Software Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { ResourceEntity } from '@backstage/catalog-model'; | ||
import { EC2 } from '@aws-sdk/client-ec2'; | ||
import * as winston from 'winston'; | ||
import { Config } from '@backstage/config'; | ||
import { AWSEntityProvider } from './AWSEntityProvider'; | ||
import { ANNOTATION_AWS_EC2_INSTANCE_ID } from '../annotations'; | ||
|
||
/** | ||
* Provides entities from AWS Elastic Compute Cloud. | ||
*/ | ||
export class AWSEC2Provider extends AWSEntityProvider { | ||
static fromConfig(config: Config, options: { logger: winston.Logger }) { | ||
const accountId = config.getString('accountId'); | ||
const roleArn = config.getString('roleArn'); | ||
const externalId = config.getOptionalString('externalId'); | ||
const region = config.getString('region'); | ||
|
||
return new AWSEC2Provider( | ||
{ accountId, roleArn, externalId, region }, | ||
options, | ||
); | ||
} | ||
|
||
getProviderName(): string { | ||
return `aws-ec2-provider-${this.accountId}`; | ||
} | ||
|
||
async run(): Promise<void> { | ||
if (!this.connection) { | ||
throw new Error('Not initialized'); | ||
} | ||
|
||
this.logger.info(`Providing ec2 resources from aws: ${this.accountId}`); | ||
const ec2Resources: ResourceEntity[] = []; | ||
|
||
const credentials = this.getCredentials(); | ||
const ec2 = new EC2({ credentials, region: this.region }); | ||
|
||
const defaultAnnotations = this.buildDefaultAnnotations(); | ||
|
||
const instances = await ec2.describeInstances({ | ||
Filters: [{ Name: 'instance-state-name', Values: ['running'] }], | ||
}); | ||
|
||
for (const reservation of instances.Reservations || []) { | ||
if (reservation.Instances) { | ||
for (const instance of reservation.Instances) { | ||
const instanceId = instance.InstanceId; | ||
const resource: ResourceEntity = { | ||
kind: 'Resource', | ||
apiVersion: 'backstage.io/v1beta1', | ||
metadata: { | ||
annotations: { | ||
...(await defaultAnnotations), | ||
[ANNOTATION_AWS_EC2_INSTANCE_ID]: instanceId ?? 'unknown', | ||
}, | ||
labels: instance.Tags?.reduce( | ||
(acc: Record<string, string>, tag) => { | ||
if (tag.Key && tag.Value) { | ||
acc[tag.Key] = tag.Value; | ||
} | ||
return acc; | ||
}, | ||
{}, | ||
), | ||
name: | ||
instanceId ?? | ||
`${reservation.ReservationId}-instance-${instance.InstanceId}`, | ||
title: | ||
instance?.Tags?.find( | ||
tag => tag.Key === 'Name' || tag.Key === 'name', | ||
)?.Value ?? undefined, | ||
instancePlatform: instance.Platform, | ||
instanceType: instance.InstanceType, | ||
monitoringState: instance.Monitoring?.State, | ||
instancePlacement: instance.Placement?.AvailabilityZone, | ||
amountOfBlockDevices: instance.BlockDeviceMappings?.length ?? 0, | ||
instanceCpuCores: instance.CpuOptions?.CoreCount, | ||
instanceCpuThreadsPerCode: instance.CpuOptions?.ThreadsPerCore, | ||
reservationId: reservation.ReservationId, | ||
}, | ||
spec: { | ||
owner: 'unknown', | ||
type: 'ec2-instance', | ||
}, | ||
}; | ||
|
||
ec2Resources.push(resource); | ||
} | ||
} | ||
} | ||
|
||
await this.connection.applyMutation({ | ||
type: 'full', | ||
entities: ec2Resources.map(entity => ({ | ||
entity, | ||
locationKey: this.getProviderName(), | ||
})), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.