# Real-time Pricing

MechCloud displays cost information directly in the plan output before any infrastructure changes are applied. This enables cost-aware infrastructure decisions during development rather than discovering costs after deployment.

### Overview

When generating an execution plan, MechCloud calculates and displays comprehensive cost information for AWS resources that have associated costs.

### Supported Billing Models

| Model                | How it appears in the plan                    | Examples                                               |
| -------------------- | --------------------------------------------- | ------------------------------------------------------ |
| Usage-based (hourly) | Hourly rate + max monthly total (744 hours)   | EC2, RDS, Lambda duration, NAT Gateway                 |
| Storage (GB-month)   | Rate + total when size is known               | EBS, S3, RDS storage, DynamoDB storage                 |
| Consumption-based    | Rate only (actual usage unknown at plan time) | Lambda requests, DynamoDB on-demand, data transfer out |

#### Usage-Based Charges (Price + Cost)

For resources with **predictable usage patterns**, MechCloud shows both price rates and calculated costs:

* EC2 instance hours
* RDS database hours
* EBS volume storage (GB-Mo)
* Load balancer hours
* NAT Gateway hours

Example: An EC2 `t3.nano` instance shows `price: $0.00520/Hrs, monthly: $3.87`

**EC2 Spot Pricing**: For all on-demand EC2 instances, MechCloud displays both on-demand and spot pricing in the plan output. This helps users evaluate potential cost savings by using spot instances when suitable for their workloads.

Example: `vm1 (action: create, price: $0.00520/Hrs, monthly: $3.87, spot-price: $0.00160/Hrs, spot-monthly: $1.19)`

#### Consumption-Based Charges (Price Only)

For resources with **variable consumption**, MechCloud shows pricing rates but cannot calculate costs without actual usage data:

* Data transfer (GB)
* DynamoDB requests (Million-WRU, Million-RRU)
* S3 requests
* Lambda invocations
* API Gateway requests
* NAT Gateway data processing (GB)

**Example**: DynamoDB write request units pricing shown as `Price (Write request units - price: $0.625/Million-WRU)`

### Cost Change Tracking

MechCloud displays cost change percentages for all resource actions, helping you understand the financial impact of infrastructure changes.

#### Create Actions

When creating new resources, the cost change is **+100%** (from $0 to the new cost):

```
vm1 (action: create, price: $0.00520/Hrs, monthly: $3.87, change: +100%)
```

#### Update Actions

When modifying existing resources, the change percentage reflects the actual cost difference.

**Example: Downsizing an instance from t3.medium to t3.small**

```
vm1 (action: update, price: $0.0208/Hrs, monthly: $15.19, change: -50%)
```

The -50% change indicates a 50% cost reduction from the previous `t3.medium` configuration.

**Example: Increasing DynamoDB write capacity from 25 to 35 WCU**

```
provisioned_table (action: update)
=> Price (Write capacity units)
  => Tier 1 (First 25 WCU - price: $0.00/WCU-Hrs, quantity: 25, monthly: $0.00, change: 0%)
  => Tier 2 (Beyond 25 WCU - price: $0.00065/WCU-Hrs, quantity: 10, monthly: $4.84, change: +100%)
```

The +100% change shows new costs since previously all 25 WCU were in the free tier.

#### Delete Actions

When deleting resources, the cost change is **-100%** (from current cost to $0):

```
vm1 (action: delete, price: $0.00/Hrs, monthly: $0.00, change: -100%)
```

### Plan Output Examples

#### EC2 Instance (Usage-Based Charges)

EC2 instances have predictable usage patterns, so MechCloud calculates and displays actual hourly and monthly costs.

**Creating an on-demand EC2 instance:**

```yaml
resources:
  - type: aws_ec2_launch_template
    name: template1
    props:
      launch_template_name: "template1"
      launch_template_data:
        instance_type: "t3.nano"
        image_id: "ami-0bc691261a82b32bc"

  - type: aws_ec2_instance
    name: vm1
    props:
      launch_template:
        launch_template_id: "ref:template1"
        version: "1"
```

**Plan output:**

```
template1 (action: create)
vm1 (action: create, price: $0.00520/Hrs, monthly: $3.87, spot-price: $0.00160/Hrs, spot-monthly: $1.19, change: +100%)
```

The plan shows:

* **On-demand price**: `$0.00520/Hrs` - calculated hourly cost for t3.nano instance
* **On-demand monthly**: `$3.87` - estimated monthly cost (hourly × 730 hours)
* **Spot price**: `$0.00160/Hrs` - spot instance hourly cost for comparison
* **Spot monthly**: `$1.19` - estimated monthly spot cost
* **Change**: `+100%` - new resource, going from $0 to $3.87/month

**Key Feature**: MechCloud displays spot pricing alongside on-demand pricing for all EC2 instances. This encourages users to evaluate spot instances when suitable for their workloads, potentially saving up to 70-90% on compute costs.

**Creating a spot EC2 instance:**

```yaml
resources:
  - type: aws_ec2_launch_template
    name: template1
    props:
      launch_template_name: "template1"
      launch_template_data:
        instance_type: "t3.nano"
        image_id: "ami-0bc691261a82b32bc"
        instance_market_options:
          market_type: "spot"

  - type: aws_ec2_instance
    name: spot_vm1
    props:
      launch_template:
        launch_template_id: "ref:template1"
        version: "1"
```

**Plan output:**

```
template1 (action: create)
spot_vm1 (action: create, spot-price: $0.00160/Hrs, spot-monthly: $1.19, change: +100%)
```

For spot instances, only spot pricing is shown since the instance will use spot capacity. The +100% change indicates a new resource.

#### DynamoDB Table (Mixed: Usage-Based + Consumption-Based)

DynamoDB provisioned capacity tables demonstrate both pricing types - calculated costs for provisioned capacity (usage-based) and pricing rates for storage (consumption-based).

**Creating a DynamoDB table (Standard class, provisioned capacity):**

```yaml
resources:
  - name: provisioned_standard
    type: aws_dynamodb_table
    props:
      table_name: "test-provisioned"
      billing_mode: "PROVISIONED"
      provisioned_throughput:
        read_capacity_units: 30
        write_capacity_units: 35
      attribute_definitions:
        - attribute_name: "id"
          attribute_type: "S"
      key_schema:
        - attribute_name: "id"
          key_type: "HASH"
```

**Plan output:**

```
provisioned_standard (action: create, change: +100%)
=> Price (Write capacity units)
  => Tier 1 (First 25 WCU - price: $0.00/WCU-Hrs, quantity: 25, monthly: $0.00, change: 0%)
  => Tier 2 (Beyond 25 WCU - price: $0.00065/WCU-Hrs, quantity: 10, monthly: $4.84, change: +100%)
=> Price (Read capacity units)
  => Tier 1 (First 25 RCU - price: $0.00/RCU-Hrs, quantity: 25, monthly: $0.00, change: 0%)
  => Tier 2 (Beyond 25 RCU - price: $0.00013/RCU-Hrs, quantity: 5, monthly: $0.48, change: +100%)
=> Price (Storage (standard))
  => Tier 1 (First 25 GB - price: $0.00/GB-Mo)
  => Tier 2 (Beyond 25 GB - price: $0.25/GB-Mo)
```

The plan shows:

* **Usage-based (calculated costs)**: Provisioned read/write capacity units with tiered pricing
  * First 25 WCU/RCU are free
  * Additional capacity shows `quantity` and calculated `monthly` cost
  * Example: 10 WCU beyond free tier = `$4.84/month`
* **Consumption-based (pricing only)**: Storage shows pricing rates (`$0.25/GB-Mo`) without calculated costs since actual storage volume is unknown at provisioning time

**Creating a DynamoDB table (Infrequent Access class):**

```yaml
resources:
  - name: provisioned_standard_ia
    type: aws_dynamodb_table
    props:
      table_name: "test-provisioned"
      billing_mode: "PROVISIONED"
      table_class: "STANDARD_INFREQUENT_ACCESS"
      provisioned_throughput:
        read_capacity_units: 30
        write_capacity_units: 35
      attribute_definitions:
        - attribute_name: "id"
          attribute_type: "S"
      key_schema:
        - attribute_name: "id"
          key_type: "HASH"
```

**Plan output:**

```
provisioned_standard_ia (action: create, change: +100%)
=> Price (Write capacity units (IA) - price: $0.00081/WCU-Hrs, quantity: 35, monthly: $21.09, change: +100%)
=> Price (Read capacity units (IA) - price: $0.00016/RCU-Hrs, quantity: 30, monthly: $3.57, change: +100%)
=> Price (Storage (IA) - price: $0.10/GB-Mo)
```

The Infrequent Access class shows:

* **Higher capacity costs**: No free tier, all capacity units are charged
  * 35 WCU = `$21.09/month` (calculated)
  * 30 RCU = `$3.57/month` (calculated)
* **Lower storage costs**: `$0.10/GB-Mo` vs `$0.25/GB-Mo` for Standard class (pricing only)

### Supported Resources

MechCloud provides real-time pricing for the following AWS resource types:

| Offering    | Resource Type                   |
| ----------- | ------------------------------- |
| EC2         | Instance (Virtual Machine)      |
|             | Volume (EBS)                    |
|             | Elastic IP                      |
|             | NAT Gateway                     |
|             | VPN Connection                  |
|             | Transit Gateway                 |
| ECS         | Service (Fargate)               |
| EKS         | Cluster                         |
| ElastiCache | Cache Cluster                   |
|             | Replication Group               |
| ELB         | Load Balancer (ALB / NLB / GLB) |
| Lambda      | Capacity Provider               |
| Lightsail   | Instance                        |
| MSK         | Managed Kafka Cluster           |
| RDS         | DB Instance                     |
|             | DB Cluster (Aurora)             |
| DynamoDB    | Table                           |
| S3          | Bucket                          |
| Route53     | Hosted Zone                     |

For detailed pricing examples, see [Examples (Resources With Cost)](https://docs.mechcloud.io/cloud-computing/stateless-iac/aws/examples-resources-with-cost).

### Regional Pricing

Costs vary by AWS region. MechCloud automatically applies region-specific pricing.

Example - `t3.medium` pricing:

```yaml
# us-east-1
instance_type: "t3.medium"  # $0.0416/Hr, $30.37/month

# ap-south-1
instance_type: "t3.medium"  # $0.0448/Hr, $32.70/month
```

### Using Pricing Data

#### Compare Configurations

```yaml
# Option A - Compute optimized
instance_type: "c6i.2xlarge"  # $0.34/Hr, $248.20/month

# Option B - General purpose
instance_type: "m6i.2xlarge"  # $0.384/Hr, $280.32/month
```

#### Track Environment Costs

When generating plans for different Resource Contexts:

```
Development environment (context: app-dev)
Monthly cost: $125.43

Production environment (context: app-prod)
Monthly cost: $892.15
```

#### Document Cost Decisions

```yaml
resources:
  - type: aws_ec2_instance
    name: api-server
    props:
      # c6i.xlarge for consistent CPU performance
      # Cost: +$18.25/month vs t3.xlarge
      # Justified by API latency requirements
      instance_type: "c6i.xlarge"
```

### Example Workflow

This example demonstrates how real-time pricing helps optimize infrastructure costs during development.

#### Initial Configuration

```yaml
resources:
  - type: aws_ec2_instance
    name: web-server
    props:
      instance_type: "t3.large"
      
  - type: aws_rds_db_instance
    name: postgres-db
    props:
      instance_class: "db.t3.medium"
      allocated_storage: 100
```

When you generate a plan, MechCloud shows costs for each resource. You can then adjust instance types and storage to optimize costs before deploying.

#### Cost Optimization

By reviewing the pricing information in the plan:

1. **Resize compute**: Change `t3.large` to `t3.medium` to reduce EC2 costs
2. **Adjust storage**: Reduce RDS storage from 100GB to 50GB if appropriate
3. **Consider spot**: Evaluate spot instances for non-critical workloads

Each configuration change generates a new plan showing updated costs, enabling iterative optimization before any resources are created.

### Best Practices

**Include cost justification in code comments** - Document why specific instance types or sizes were chosen.

**Compare costs during code review** - Review cost deltas alongside configuration changes.

**Track cost trends over time** - Save plan outputs to monitor infrastructure cost evolution.

**Use Resource Context for cost isolation** - Separate cost tracking by environment (dev, staging, prod).

**Monitor actual vs estimated costs** - Compare MechCloud estimates against actual cloud bills to refine understanding.

### Integration with Stateless IaC

Real-time pricing works seamlessly with MechCloud's stateless architecture:

**Fresh pricing every plan** - No stale data from state files

**Accurate cost deltas** - Costs compared against actual current state

**Environment isolation** - Resource Context enables per-environment cost tracking

### Next Steps

* [Examples (Resources With Cost)](https://docs.mechcloud.io/cloud-computing/stateless-iac/aws/examples-resources-with-cost)
* [Authoring Desired State](https://docs.mechcloud.io/cloud-computing/stateless-iac/aws/authoring-desired-state)
