Skip to content

Instantly share code, notes, and snippets.

View Dirga36's full-sized avatar
🤔

Fu'ad Husnan Dirga36

🤔
View GitHub Profile
package terraform.finops
deny[msg] {
resource := input.resource_changes[_]
resource.type == "aws_instance"
allowed_types := {"t3.micro", "t3.small", "t3.medium"}
not allowed_types[resource.change.after.instance_type]
msg := sprintf(
"Instance type %v is not in the approved list for this environment",
import boto3
from datetime import datetime, timedelta
def find_idle_instances(cpu_threshold=5.0, days=7):
ec2 = boto3.client("ec2")
cloudwatch = boto3.client("cloudwatch")
idle_instances = []
instances = ec2.describe_instances(
Filters=[{"Name": "instance-state-name", "Values": ["running"]}]
import boto3
REQUIRED_TAGS = {"team", "environment", "project"}
def lambda_handler(event, context):
ec2 = boto3.client("ec2")
detail = event["detail"]
if detail["eventName"] != "RunInstances":
return
from sklearn.cluster import KMeans
import pandas as pd
def classify_workload_patterns(usage_df, n_clusters=4):
"""
Cluster storage volumes by access frequency, size, and
read/write ratio to recommend appropriate storage tiers.
"""
features = usage_df[["avg_daily_accesses", "size_gb", "read_write_ratio"]]
import numpy as np
from statsmodels.tsa.holtwinters import ExponentialSmoothing
def forecast_storage_demand(historical_usage, periods_ahead=24):
"""
Forecast storage utilization for the next N hours using
Holt-Winters exponential smoothing.
"""
model = ExponentialSmoothing(
historical_usage,
import time
import random
import boto3
from botocore.exceptions import ClientError
s3 = boto3.client("s3")
def upload_with_retry(local_path: str, bucket: str, s3_key: str, max_retries: int = 5) -> bool:
for attempt in range(max_retries):
try:
import boto3
import json
s3 = boto3.client("s3")
def lambda_handler(event, context):
for record in event["Records"]:
bucket = record["s3"]["bucket"]["name"]
key = record["s3"]["object"]["key"]
import hashlib
import boto3
from pathlib import Path
s3 = boto3.client("s3")
def file_hash(path: Path) -> str:
return hashlib.md5(path.read_bytes()).hexdigest()
def sync_directory(local_dir: str, bucket: str, prefix: str = "") -> None:
import boto3
from pathlib import Path
s3 = boto3.client("s3")
def upload_file(local_path: str, bucket: str, s3_key: str) -> None:
"""Upload a single file to S3, preserving content type where possible."""
path = Path(local_path)
if not path.exists():
raise FileNotFoundError(f"No such file: {local_path}")
pip install boto3
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"
export AWS_DEFAULT_REGION="us-east-1"