UNPKG

@cloud-carbon-footprint/aws

Version:

The core logic to get cloud usage data and estimate energy and carbon emissions from Amazon Web Services.

104 lines 6.15 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const client_athena_1 = require("@aws-sdk/client-athena"); const client_cloudwatch_1 = require("@aws-sdk/client-cloudwatch"); const client_cloudwatch_logs_1 = require("@aws-sdk/client-cloudwatch-logs"); const client_cost_explorer_1 = require("@aws-sdk/client-cost-explorer"); const client_glue_1 = require("@aws-sdk/client-glue"); const client_s3_1 = require("@aws-sdk/client-s3"); const core_1 = require("@cloud-carbon-footprint/core"); const common_1 = require("@cloud-carbon-footprint/common"); const lib_1 = require("../lib"); const AWSCredentialsProvider_1 = __importDefault(require("./AWSCredentialsProvider")); const domain_1 = require("../domain"); const Recommendations_1 = require("../lib/Recommendations"); class AWSAccount extends core_1.CloudProviderAccount { id; name; regions; credentials; constructor(id, name, regions) { super(); this.id = id; this.name = name; this.regions = regions; this.credentials = AWSCredentialsProvider_1.default.create(id); } async getDataForRegions(startDate, endDate, grouping) { const results = []; for (const regionId of this.regions) { const regionEstimates = await Promise.all(await this.getDataForRegion(regionId, startDate, endDate, grouping)); results.push(regionEstimates); } return results.flat(); } getDataForRegion(regionId, startDate, endDate, grouping) { const awsServices = this.getServices(regionId); const awsConstants = { minWatts: domain_1.AWS_CLOUD_CONSTANTS.MIN_WATTS_AVG, maxWatts: domain_1.AWS_CLOUD_CONSTANTS.MAX_WATTS_AVG, powerUsageEffectiveness: domain_1.AWS_CLOUD_CONSTANTS.getPUE(), }; const region = new core_1.Region(regionId, awsServices, domain_1.AWS_EMISSIONS_FACTORS_METRIC_TON_PER_KWH, awsConstants); return this.getRegionData((0, common_1.configLoader)().AWS.NAME, region, startDate, endDate, grouping); } getServices(regionId) { return (0, common_1.configLoader)().AWS.CURRENT_SERVICES.map(({ key }) => { return this.getService(key, regionId, this.credentials); }); } async getDataForRecommendations(recommendationTarget) { const serviceWrapper = this.createServiceWrapper(this.getClientConfig((0, common_1.configLoader)().AWS.ATHENA_REGION, this.credentials)); return await Recommendations_1.Recommendations.getRecommendations(recommendationTarget, serviceWrapper); } async getDataFromCostAndUsageReports(startDate, endDate, grouping) { const costAndUsageReportsService = new lib_1.CostAndUsageReports(new core_1.ComputeEstimator(), new core_1.StorageEstimator(domain_1.AWS_CLOUD_CONSTANTS.SSDCOEFFICIENT), new core_1.StorageEstimator(domain_1.AWS_CLOUD_CONSTANTS.HDDCOEFFICIENT), new core_1.NetworkingEstimator(domain_1.AWS_CLOUD_CONSTANTS.NETWORKING_COEFFICIENT), new core_1.MemoryEstimator(domain_1.AWS_CLOUD_CONSTANTS.MEMORY_COEFFICIENT), new core_1.UnknownEstimator(domain_1.AWS_CLOUD_CONSTANTS.ESTIMATE_UNKNOWN_USAGE_BY), new core_1.EmbodiedEmissionsEstimator(domain_1.AWS_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN), this.createServiceWrapper(this.getClientConfig((0, common_1.configLoader)().AWS.ATHENA_REGION, this.credentials))); return await costAndUsageReportsService.getEstimates(startDate, endDate, grouping); } static async getCostAndUsageReportsDataFromInputData(inputData) { const costAndUsageReportsService = new lib_1.CostAndUsageReports(new core_1.ComputeEstimator(), new core_1.StorageEstimator(domain_1.AWS_CLOUD_CONSTANTS.SSDCOEFFICIENT), new core_1.StorageEstimator(domain_1.AWS_CLOUD_CONSTANTS.HDDCOEFFICIENT), new core_1.NetworkingEstimator(domain_1.AWS_CLOUD_CONSTANTS.NETWORKING_COEFFICIENT), new core_1.MemoryEstimator(domain_1.AWS_CLOUD_CONSTANTS.MEMORY_COEFFICIENT), new core_1.UnknownEstimator(domain_1.AWS_CLOUD_CONSTANTS.ESTIMATE_UNKNOWN_USAGE_BY), new core_1.EmbodiedEmissionsEstimator(domain_1.AWS_CLOUD_CONSTANTS.SERVER_EXPECTED_LIFESPAN)); return await costAndUsageReportsService.getEstimatesFromInputData(inputData); } getService(key, region, credentials) { if (this.services[key] === undefined) throw new Error('Unsupported service: ' + key); const options = this.getClientConfig(region, credentials); return this.services[key](options); } getClientConfig(region, credentials) { return { region, credentials }; } createServiceWrapper(options) { return new lib_1.ServiceWrapper(new client_cloudwatch_1.CloudWatchClient(options), new client_cloudwatch_logs_1.CloudWatchLogsClient(options), new client_cost_explorer_1.CostExplorerClient({ region: (0, common_1.configLoader)().AWS.IS_AWS_GLOBAL ? 'us-east-1' : 'cn-northwest-1', credentials: options.credentials, }), new client_s3_1.S3Client(options), new client_athena_1.AthenaClient(options), new client_glue_1.GlueClient(options)); } services = { ebs: (options) => { return new lib_1.EBS(this.createServiceWrapper(options)); }, s3: (options) => { return new lib_1.S3(this.createServiceWrapper(options)); }, ec2: (options) => { return new lib_1.EC2(this.createServiceWrapper(options)); }, elasticache: (options) => { return new lib_1.ElastiCache(this.createServiceWrapper(options)); }, rds: (options) => { return new lib_1.RDS(new lib_1.RDSComputeService(this.createServiceWrapper(options)), new lib_1.RDSStorage(this.createServiceWrapper(options))); }, lambda: (options) => { return new lib_1.Lambda(120000, 1000, this.createServiceWrapper(options)); }, }; } exports.default = AWSAccount; //# sourceMappingURL=AWSAccount.js.map