Skip to content

About

Cloud-native FinOps platform for infrastructure cost analytics, monitoring, and optimization using GCP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Infrastructure as Code for Infrastructure Cost Optimization with Cloud Billing API and Cloud Functions

Problem

Organizations struggle with managing cloud costs effectively as their Google Cloud infrastructure scales, often experiencing unexpected billing spikes, unused resources driving up expenses, and lack of real-time visibility into spending patterns. Without automated cost monitoring and optimization mechanisms, businesses face budget overruns and inefficient resource allocation that directly impacts their bottom line and operational efficiency.

Solution

Build an intelligent cost optimization system that leverages Cloud Billing API for real-time spending data, Cloud Functions for serverless automation, and BigQuery for advanced analytics to create a comprehensive FinOps solution. This architecture provides automated budget monitoring, cost anomaly detection, and intelligent resource optimization recommendations while maintaining operational reliability and performance.

Architecture Diagram

graph TB
    subgraph "Cost Data Sources"
        BILLING[Cloud Billing API]
        MONITORING[Cloud Monitoring]
        EXPORT[Billing Export]
    end
    
    subgraph "Data Processing"
        BQ[BigQuery Dataset]
        FUNCTION1[Cost Analysis Function]
        FUNCTION2[Anomaly Detection Function]
        FUNCTION3[Optimization Function]
    end
    
    subgraph "Automation & Alerts"
        PUBSUB[Pub/Sub Topic]
        BUDGET[Budget Alerts]
        SCHEDULER[Cloud Scheduler]
    end
    
    subgraph "Monitoring & Reporting"
        DASHBOARD[Cloud Monitoring Dashboard]
        ALERTS[Alert Policies]
        REPORTS[Cost Reports]
    end
    
    BILLING-->BQ
    MONITORING-->BQ
    EXPORT-->BQ
    
    BQ-->FUNCTION1
    BQ-->FUNCTION2
    FUNCTION1-->FUNCTION3
    
    BUDGET-->PUBSUB
    PUBSUB-->FUNCTION2
    SCHEDULER-->FUNCTION1
    
    FUNCTION2-->ALERTS
    FUNCTION3-->DASHBOARD
    FUNCTION1-->REPORTS
    
    style BQ fill:#4285F4
    style FUNCTION1 fill:#FF9900
    style FUNCTION2 fill:#FF9900
    style FUNCTION3 fill:#FF9900
    style PUBSUB fill:#34A853
Loading

Prerequisites

  • Google Cloud CLI (gcloud) installed and configured
  • Active Google Cloud project with billing enabled
  • Appropriate IAM permissions:
    • Billing Account Administrator or Billing Account Costs Manager
    • Cloud Functions Developer
    • BigQuery Admin
    • Pub/Sub Admin
    • Cloud Scheduler Admin
    • Project IAM Admin
  • Basic understanding of FinOps principles and cloud cost management
  • Estimated cost: $15-25/month for BigQuery storage, Cloud Functions execution, and monitoring resources

Quick Start

Using Infrastructure Manager

cd infrastructure-manager/

# Set required environment variables
export PROJECT_ID="your-project-id"
export BILLING_ACCOUNT_ID="your-billing-account-id"
export REGION="us-central1"

# Deploy infrastructure
gcloud infra-manager deployments apply \
    projects/${PROJECT_ID}/locations/${REGION}/deployments/cost-optimization \
    --service-account=${PROJECT_ID}@${PROJECT_ID}.iam.gserviceaccount.com \
    --local-source="." \
    --inputs-file="main.yaml"

Using Terraform

cd terraform/

# Initialize Terraform
terraform init

# Copy and customize variables
cp terraform.tfvars.example terraform.tfvars
# Edit terraform.tfvars with your specific values

# Plan deployment
terraform plan

# Apply infrastructure
terraform apply

Using Bash Scripts

# Make scripts executable
chmod +x scripts/deploy.sh scripts/destroy.sh

# Set required environment variables
export PROJECT_ID="your-project-id"
export BILLING_ACCOUNT_ID="your-billing-account-id"
export REGION="us-central1"

# Deploy infrastructure
./scripts/deploy.sh

Configuration

Required Variables

Variable Description Default Required
project_id Google Cloud Project ID - Yes
billing_account_id Billing Account ID for budget creation - Yes
region Google Cloud region for resources us-central1 No
budget_amount Monthly budget amount in USD 1000 No
dataset_location BigQuery dataset location US No

Optional Customizations

  • Budget Thresholds: Modify budget alert thresholds (default: 50%, 90%, 100%)
  • Function Memory: Adjust Cloud Function memory allocation (default: 512MB)
  • Function Timeout: Modify function execution timeout (default: 540s)
  • Schedule Frequency: Change cost analysis scheduling (default: daily at 9 AM)
  • Anomaly Detection Sensitivity: Adjust statistical thresholds for anomaly detection

Architecture Components

This infrastructure deploys:

Data Infrastructure

  • BigQuery Dataset: Central data warehouse for billing and cost analytics
  • Cost Anomalies Table: Structured storage for anomaly detection results
  • Billing Export Configuration: Automated data pipeline from Cloud Billing API

Compute Resources

  • Cost Analysis Function: Serverless function for trend analysis and recommendations
  • Anomaly Detection Function: Machine learning-based spending anomaly detection
  • Optimization Function: Resource utilization analysis and optimization recommendations

Event-Driven Architecture

  • Pub/Sub Topic: Message queue for cost optimization events and alerts
  • Budget Configuration: Automated spending thresholds with Pub/Sub notifications
  • Cloud Scheduler Jobs: Automated execution of cost analysis workflows

Monitoring & Alerting

  • Budget Alerts: Multi-tier spending notifications (50%, 90%, 100%)
  • Cost Anomaly Tracking: Statistical analysis for unusual spending patterns
  • Optimization Recommendations: Automated resource rightsizing suggestions

Validation & Testing

After deployment, validate the infrastructure:

1. Verify BigQuery Resources

# Check dataset creation
bq ls ${PROJECT_ID}:cost_optimization_*

# Verify table schema
bq show ${PROJECT_ID}:cost_optimization_*/cost_anomalies

2. Test Cloud Functions

# Get function URLs
gcloud functions list --filter="name:cost-opt-*"

# Test cost analysis function
COST_ANALYSIS_URL=$(gcloud functions describe cost-opt-*-cost-analysis \
    --format="value(httpsTrigger.url)")
curl -X GET ${COST_ANALYSIS_URL}

3. Verify Pub/Sub Configuration

# List topics and subscriptions
gcloud pubsub topics list --filter="name:cost-optimization-alerts"
gcloud pubsub subscriptions list --filter="topic:cost-optimization-alerts"

# Test message publishing
gcloud pubsub topics publish cost-optimization-alerts \
    --message='{"test": "cost optimization system"}'

4. Check Scheduled Jobs

# Verify scheduler configuration
gcloud scheduler jobs list --filter="name:cost-*-scheduler"

# Check job execution history
gcloud scheduler jobs describe cost-analysis-scheduler

Cleanup

Using Infrastructure Manager

# Delete deployment
gcloud infra-manager deployments delete \
    projects/${PROJECT_ID}/locations/${REGION}/deployments/cost-optimization

Using Terraform

cd terraform/
terraform destroy

Using Bash Scripts

./scripts/destroy.sh

Security Considerations

IAM and Access Control

  • Functions use least-privilege service accounts
  • BigQuery dataset access restricted to cost optimization workflows
  • Pub/Sub topics secured with appropriate IAM bindings
  • Budget notifications limited to authorized recipients

Data Protection

  • Billing data encrypted at rest in BigQuery
  • Function environment variables secured
  • Network traffic encrypted in transit
  • Audit logging enabled for all cost-related activities

Budget Controls

  • Multiple alert thresholds prevent budget overruns
  • Pub/Sub integration enables automated responses
  • Cost anomaly detection provides early warning system
  • Resource optimization recommendations reduce unnecessary spending

Monitoring and Observability

Cloud Monitoring Integration

  • Function execution metrics and error rates
  • BigQuery query performance and costs
  • Pub/Sub message throughput and latency
  • Budget alert delivery status

Logging and Debugging

  • Structured logging in all Cloud Functions
  • BigQuery query execution logs
  • Scheduler job execution history
  • Pub/Sub message delivery tracking

Performance Optimization

  • Function cold start minimization
  • BigQuery query optimization
  • Efficient data partitioning strategies
  • Cost-effective storage class selection

Cost Optimization Features

Automated Analysis

  • Daily Cost Trends: Automatic analysis of spending patterns across all services
  • Anomaly Detection: Statistical algorithms identify unusual spending spikes
  • Optimization Recommendations: Resource rightsizing and scheduling suggestions
  • Budget Monitoring: Proactive alerts at customizable spending thresholds

Machine Learning Capabilities

  • Predictive Analytics: Forecast future costs based on historical patterns
  • Usage Pattern Analysis: Identify underutilized resources for optimization
  • Seasonal Trend Detection: Adapt to business cycle variations
  • Cost Attribution: Granular analysis by project, service, and resource labels

Automation Workflows

  • Scheduled Analysis: Regular cost review without manual intervention
  • Event-Driven Alerts: Immediate response to budget threshold violations
  • Optimization Triggers: Automated recommendations based on usage patterns
  • Remediation Integration: Foundation for automated cost optimization actions

About

Cloud-native FinOps platform for infrastructure cost analytics, monitoring, and optimization using GCP.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages