Pricing

This modules allows to estimate your cloud infrastructure cost changes based on input from Infrastructure as Code (IaC) tools. This ensures that you are aware of cost changes before they occur.

It exposes the nops_sdk.pricing.CloudCost class, which can be used to estimate cost impact of a IaC changeset. See the documentation of the class for more.

Pricing

class nops_sdk.pricing.pricing.CloudCost(aws_region, spec)

Bases: object

Compute AWS Infrastructure cost change from changes to Infrastructure as Code files!

Currently only Terraform is supported. Supported AWS resources are specified in cloud_infrastructure.enums.AWSProductFamily

Parameters
  • aws_region (Union[str, AWSRegion]) – The AWS region where resources are.

  • spec (list[dict[str, str]]) – a list of operations

Return type

None

Examples

>>> from nops_sdk.pricing import CloudCost
>>> from nops_sdk.cloud_infrastructure.enums import AWSRegion
>>> from nops_sdk.cloud_infrastructure.cloud_operation import Periodicity
>>> spec = [
        {
            "new_data": {"instance_type": "t2.micro"},
            "old_data": None,
            "operation_type": "create",
            "resource_type": "ec2",
            "ami": "ami-0269f532"
        },
        {
            "new_data": None,
            "old_data": {
                "instance_class": "db.t2.micro",
                "engine": "oracle-ee",
                "license_model": "bring-your-own-license",
                "multi_az": True
            },
            "operation_type": "delete",
            "resource_type": "rds",
        },
        {
            "new_data": {"instance_type": "t2.nano", "ami": "ami-00bb6f60"},
            "old_data": {"instance_type": "t2.micro", "ami": "ami-0269f532"},
            "operation_type": "update",
            "resource_type": "ec2"
        },
        {
            'id': None,
            'resource_type': 'aws_eks_cluster',
            'operation_type': 'create',
            'old_data': None,
            'new_data': {
                'name': 'devopsthehardway-cluster',
            }
        },
        {
            'id': None,
            'resource_type': 'aws_eks_node_group',
            'operation_type': 'create',
            'old_data': None,
            'new_data': {
                'cluster_name': 'devopsthehardway-cluster',
                'instance_types': ['t3.xlarge'],
                'node_group_name': 'devopsthehardway-workernodes',
                'scaling_config': [
                    {
                    'desired_size': 1,
                    'max_size': 1,
                    'min_size': 1
                    }
                ],
            }
        }
    ]
>>> cloud_cost = CloudCost(aws_region=AWSRegion('us-west-2'), spec=spec)
>>> cloud_cost.load_prices()
After you load the prices, you can compute and output prices for any supported `Peridocity` at no significant cost.
>>> cloud_cost.compute_cost_effects(period=Periodicity('monthly'))
>>> cloud_cost.output_report()
Create t2.micro EC2 instance with a monthly cost impact of 8.35
Delete db.t2.micro RDS instance with a monthly cost impact of -9.79
Update t2.micro EC2 instance to t2.nano EC2 instance with a monthly cost impact of -4.18
Create AWS EKS cluster devopsthehardway-cluster with a monthly cost impact of 72.00
Create AWS EKS NodeGroup cluster devopsthehardway-workernodes on cluster devopsthehardway-cluster with a monthly cost impact of 119.81
__init__(aws_region, spec)
Parameters
Return type

None

compute_cost_effects(period=Periodicity.MONTHLY)

Given a period, compute the cost effect. Makes sense only after prices have been loaded via CloudCost.load_prices.

Parameters

period (nops_sdk.cloud_infrastructure.enums.Periodicity) – defaults to monthly

Returns

None

load_prices()

Load price data from AWS and nOps API and populate cost fields in Resources.

output_report()

Print out the reports of the operations.