Skip to content

src

core special

analyze

Analyze

Entrypoint for Analyze command

__init__(self, provider, service, region, sdk=<module 'boto3' from '/home/runner/.cache/pypoetry/virtualenvs/costreduce-IwU1RX7U-py3.8/lib/python3.8/site-packages/boto3/__init__.py'>) special

init function

Parameters:

Name Type Description Default
provider

Cloud Provider (aws)

required
service

Cloud Service (ec2,cloudwatch)

required
region

Cloud Provider region

required
Source code in costreduce/core/analyze.py
16
17
18
19
20
21
22
23
24
25
26
27
def __init__(self, provider, service, region, sdk=boto3):
    """
    init function
    Arguments:
        provider: Cloud Provider (aws)
        service: Cloud Service (ec2,cloudwatch)
        region: Cloud Provider region
    """
    self.provider = provider
    self.service = service
    self.region = region
    self.sdk = sdk
aws(self)

Entrypoint for AWS provider

Source code in costreduce/core/analyze.py
29
30
31
32
33
34
35
36
37
38
39
40
41
def aws(self):
    """
    Entrypoint for AWS provider
    """
    if self.service == "ec2":
        return Ec2Analyze(self.sdk, self.region).analyze()
    elif self.service == "s3":
        return S3Analyze(self.sdk, self.region).analyze()
    elif self.service == "cloudwatch":
        return CloudwatchAnalyze(self.sdk, self.region).analyze()
    else:
        logger.info("Service " + str(self.service) + " don't allow!")
        sys.exit(1)

providers special

aws

account_id()

Get current account id

Source code in costreduce/core/providers/aws.py
 8
 9
10
11
12
def account_id():
    """ Get current account id """
    response = boto3.client("sts").get_caller_identity().get("Account")
    logger.debug("Account ID: " + response)
    return response

services special

aws special

cloudwatch
Cloudwatch

Class for all CloudWatch services

__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/cloudwatch.py
39
40
41
42
43
44
45
46
47
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.region = region
    self.client_logs = sdk.client("logs", region_name=region)
CloudwatchAnalyze

Class for Analyze all EC2 Services

__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/cloudwatch.py
10
11
12
13
14
15
16
17
18
19
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.sdk = sdk
    self.region = region
    self.cloudwatch = Cloudwatch(self.sdk, self.region)
analyze(self)

Function for Analyze all services.

Returns:

Type Description

Table for all check

Source code in costreduce/core/services/aws/cloudwatch.py
21
22
23
24
25
26
27
28
29
30
31
32
33
def analyze(self):
    """
    Function for Analyze all services.
    Returns:
        Table for all check
    """
    analyze = list()

    logger.info("Check for CloudWatch Logs")
    analyze.append(self.cloudwatch.get_log_groups())

    logger.debug("Result of analyze : " + str(analyze))
    return analyze
ec2
ApplicationLoadBalancer

Class for all ALBv2 services

__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/ec2.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.sdk = sdk
    self.region = region
    self.client = sdk.client("elbv2", region_name=region)
ComputeOptimizer
__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/ec2.py
145
146
147
148
149
150
151
152
153
154
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.region = region
    self.client = sdk.client("compute-optimizer", region_name=region)
    self.account_id = account_id()
is_active(self)

Check Compute Optimizer is active

Source code in costreduce/core/services/aws/ec2.py
156
157
158
159
160
161
162
163
def is_active(self):
    """Check Compute Optimizer is active"""
    response = self.client.get_enrollment_status()
    if response["status"] != "Active":
        logger.error("Compute Optimizer enrollment status is " + response["status"])
        return False
    else:
        return True
Ec2

Class for all EC2 services

__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/ec2.py
52
53
54
55
56
57
58
59
60
61
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.sdk = sdk
    self.region = region
    self.client_ec2 = sdk.client("ec2", region_name=region)
Ec2Analyze

Class for Analyze all EC2 Services

__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/ec2.py
11
12
13
14
15
16
17
18
19
20
21
22
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.sdk = sdk
    self.region = region
    self.compute_optimizer = ComputeOptimizer(self.sdk, self.region)
    self.application_load_balancer = ApplicationLoadBalancer(self.sdk, self.region)
    self.ec2 = Ec2(self.sdk, self.region)
analyze(self)

Function for Analyze all services.

Returns:

Type Description

Table for all check

Source code in costreduce/core/services/aws/ec2.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def analyze(self):
    """
    Function for Analyze all services.
    Returns:
        Table for all check
    """
    analyze = list()
    # Compute Optimizer
    logger.info("Check Compute Optimizer")
    if self.compute_optimizer.is_active():
        analyze.append(self.compute_optimizer.ec2_recommendations())
        analyze.append(self.compute_optimizer.auto_scaling_group_recommendations())
    # EIP
    logger.info("Check for EIP")
    analyze.append(self.ec2.eip_is_not_attached())
    # ALB
    logger.info("Check for ALB")
    analyze.append(self.application_load_balancer.alb_listener_one_rule())
    # EBS
    logger.info("Check for EBS")
    analyze.append(self.ec2.ebs_is_not_attached())
    logger.debug("Result of analyze : " + str(analyze))
    return analyze
s3
S3
__init__(self, sdk, region) special

init function

Parameters:

Name Type Description Default
sdk

import boto3 globaly

required
region

Cloud Provider region

required
Source code in costreduce/core/services/aws/s3.py
24
25
26
27
28
29
30
31
32
33
def __init__(self, sdk, region):
    """
    init function
    Arguments:
        sdk: import boto3 globaly
        region: Cloud Provider region
    """
    self.sdk = sdk
    self.region = region
    self.client_s3 = sdk.client("s3", region_name=region)