Cost Optimization Framework
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β AWS Cost Optimization Framework β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Visibility Optimization Governance β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Cost Explorerβ β Right-sizing β β Budgets β β
β β Budgets β β Reserved β β Policies β β
β β Cost & Usage β β Spot/On-Demandβ β Tagging β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
β Automation Monitoring Continuous Improvement β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Lambda β β CloudWatch β β Reviews β β
β β Scheduler β β Alarms β β Optimization β β
β β Auto-scaling β β Reports β β Experiments β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q1: How do you implement a FinOps framework on AWS?
Answer:
FinOps Pillars:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FinOps Framework Pillars β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Inform β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Cost visibility & allocation β β
β β β’ Unit economics (cost per transaction/user) β β
β β β’ Benchmarking against industry β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimize β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Rightsizing recommendations β β
β β β’ Reserved Instances / Savings Plans β β
β β β’ Spot Instances for fault-tolerant workloads β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Operate β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Continuous cost monitoring β β
β β β’ Anomaly detection β β
β β β’ Budget accountability β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Cost Allocation Tags:
# Enable cost allocation tags
ce = boto3.client('ce')
ce.create_cost_allocation_tag_status(
Statuses=[
{'TagKey': 'CostCenter', 'Status': 'Active'},
{'TagKey': 'Environment', 'Status': 'Active'},
{'TagKey': 'Team', 'Status': 'Active'}
]
)
# Tag enforcement policy
tag_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RequireCostTags",
"Effect": "Deny",
"Action": [
"ec2:RunInstances",
"rds:CreateDBInstance",
"redshift:CreateCluster"
],
"Resource": "*",
"Condition": {
"Null": {
"aws:RequestTag/CostCenter": "true"
}
}
}
]
}
Q2: How do you analyze and reduce AWS costs?
Answer:
Cost Analysis Architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Analysis & Reduction β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Data Collection Analysis Recommendations β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Cost & UsageβββββββΆβ Cost ββββββΆβ Rightsizing β β
β β Reports β β Explorer β β Recommendationsβ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β S3 β β QuickSight β β Automation β β
β β (Archive) β β Dashboard β β Actions β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Cost Reduction Strategies:
class CostOptimizer:
def __init__(self):
self.ce = boto3.client('ce')
self.ec2 = boto3.client('ec2')
def analyze_monthly_costs(self):
# Get cost breakdown by service
response = self.ce.get_cost_and_usage(
TimePeriod={
'Start': (datetime.now().replace(day=1)).strftime('%Y-%m-%d'),
'End': datetime.now().strftime('%Y-%m-%d')
},
Granularity='MONTHLY',
Metrics=['BlendedCost', 'UsageQuantity'],
GroupBy=[
{'Type': 'DIMENSION', 'Key': 'SERVICE'}
]
)
return response['ResultsByTime']
def get_rightsizing_recommendations(self):
response = self.ce.get_rightsizing_recommendations(
AccountId='123456789',
LookBackPeriodInDays=14,
TermInDays=30
)
return response['RightsizingRecommendations']
def calculate_ri_savings(self):
# Analyze Reserved Instance opportunities
response = self.ce.get_reservation_purchase_recommendation(
AccountId='123456789',
Service='Amazon EC2',
LookbackPeriodInDays=60,
TermInYears='ONE_YEAR',
PaymentOption='ALL_UPFRONT'
)
return response['RecommendationDetails']
Cost Comparison Table:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EC2 Cost Comparison (m5.xlarge) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β On-Demand: $0.192/hr = $139.78/month β
β 1yr Reserved: $0.121/hr = $87.87/month (37% off) β
β 3yr Reserved: $0.077/hr = $56.10/month (59% off) β
β Spot (avg): $0.058/hr = $42.15/month (70% off) β
β β
β Savings Plan Options: β
β Compute SP (1yr): $0.110/hr = $79.99/month (43% off) β
β EC2 SP (1yr): $0.116/hr = $84.41/month (39% off) β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q3: How do you implement auto-scaling for cost optimization?
Answer:
Auto-Scaling Architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Auto-Scaling Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Demand-Based Scaling β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β CloudWatch Metrics β Auto Scaling β EC2 Instances β β
β β CPU > 70% β Scale Out (Add instances) β β
β β CPU < 30% β Scale In (Remove instances) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Schedule-Based Scaling β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Business Hours (9-5) β Full capacity β β
β β Off Hours β Reduced capacity β β
β β Weekends β Minimal capacity β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Predictive Scaling β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β ML-based prediction β Proactive scaling β β
β β Historical patterns β Capacity planning β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
EMR Auto-Scaling:
# Configure EMR auto-scaling
emr = boto3.client('emr')
cluster = emr.run_job_flow(
Name='cost-optimized-cluster',
Instances={
'MasterInstanceType': 'm5.xlarge',
'SlaveInstanceType': 'm5.xlarge',
'InstanceCount': 2,
'Ec2KeyName': 'my-key',
'Placement': {'AvailabilityZone': 'us-east-1a'},
'KeepJobFlowAliveWhenNoSteps': True,
'TerminationProtected': False,
'AutoScalingPolicy': {
'Constraints': {
'MinCapacity': 2,
'MaxCapacity': 10
},
'Rules': [
{
'Name': 'ScaleOutCPU',
'ActionParameters': {
'InstanceType': 'm5.xlarge',
'InstanceCount': 1
},
'Trigger': {
'CloudWatchAlarmDefinition': {
'MetricName': 'CPUUtilization',
'Statistic': 'Average',
'Period': 300,
'Threshold': 70,
'ComparisonOperator': 'GreaterThanThreshold'
}
}
},
{
'Name': 'ScaleInCPU',
'ActionParameters': {
'InstanceCount': -1
},
'Trigger': {
'CloudWatchAlarmDefinition': {
'MetricName': 'CPUUtilization',
'Statistic': 'Average',
'Period': 600,
'Threshold': 30,
'ComparisonOperator': 'LessThanThreshold'
}
}
}
]
}
}
)
Q4: How do you implement spot instances for batch workloads?
Answer:
Spot Instance Strategy:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Spot Instance Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Instance Fleet Configuration β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Master: 1x On-Demand (required) β β
β β Core: Mix of On-Demand + Spot (70/30) β β
β β Task: 100% Spot (maximize savings) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Spot Pricing Strategy β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Set max price = On-Demand price β β
β β β’ Use multiple instance types for diversity β β
β β β’ Implement interruption handling β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Cost Comparison β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β On-Demand: $0.192/hr β β
β β Spot: $0.058/hr (70% savings) β β
β β Monthly: $139 β $42 (per instance) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
EMR Spot Implementation:
# Create cost-optimized EMR cluster
emr = boto3.client('emr')
cluster = emr.run_job_flow(
Name='spot-optimized-cluster',
Instances={
'MasterInstanceType': 'm5.xlarge', # On-Demand for stability
'MasterInstanceCount': 1,
'InstanceFleets': [
{
'Name': 'CoreFleet',
'InstanceFleetType': 'CORE',
'TargetOnDemandCapacity': 2,
'TargetSpotCapacity': 8,
'InstanceTypeConfigs': [
{
'InstanceType': 'm5.xlarge',
'WeightedCapacity': 1,
'SpotPrice': 0.192
},
{
'InstanceType': 'm5a.xlarge',
'WeightedCapacity': 1,
'SpotPrice': 0.186
},
{
'InstanceType': 'm4.xlarge',
'WeightedCapacity': 1,
'SpotPrice': 0.175
}
]
},
{
'Name': 'TaskFleet',
'InstanceFleetType': 'TASK',
'TargetOnDemandCapacity': 0,
'TargetSpotCapacity': 10,
'InstanceTypeConfigs': [
{
'InstanceType': 'm5.xlarge',
'WeightedCapacity': 1
},
{
'InstanceType': 'm5.2xlarge',
'WeightedCapacity': 2
}
]
}
]
}
)
Q5: How do you optimize storage costs?
Answer:
Storage Optimization Strategies:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Storage Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β S3 Storage Classes β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Standard: $0.023/GB (frequent access) β β
β β Standard-IA: $0.0125/GB (infrequent) β β
β β One Zone-IA: $0.01/GB (re-creatable) β β
β β Glacier Instant: $0.004/GB (archive, ms access) β β
β β Glacier Flexible: $0.0036/GB (archive, min access) β β
β β Deep Archive: $0.00099/GB (long-term archive) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Lifecycle Policies β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Day 1: Standard β Day 30: Standard-IA β β
β β Day 90: Glacier β Day 365: Deep Archive β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Compression β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Parquet + Snappy: 60-70% size reduction β β
β β Gzip: 70-80% size reduction (CPU intensive) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Lifecycle Policy Implementation:
s3 = boto3.client('s3')
# Create lifecycle policy
lifecycle_config = {
'Rules': [
{
'ID': 'OptimizeDataLakeStorage',
'Status': 'Enabled',
'Filter': {
'And': {
'Prefix': 'raw/',
'Tags': [
{'Key': 'DataTier', 'Value': 'hot'}
]
}
},
'Transitions': [
{
'Days': 30,
'StorageClass': 'STANDARD_IA'
},
{
'Days': 90,
'StorageClass': 'GLACIER'
},
{
'Days': 365,
'StorageClass': 'DEEP_ARCHIVE'
}
],
'Expiration': {
'Days': 2555 # 7 years
}
}
]
}
s3.put_bucket_lifecycle_configuration(
Bucket='data-lake-bucket',
LifecycleConfiguration=lifecycle_config
)
Q6: How do you implement cost anomaly detection?
Answer:
Cost Anomaly Detection Architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Anomaly Detection β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Data Sources Analysis Response β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Cost & UsageβββββββΆβ Cost AnomalyββββββΆβ SNS Alerts β β
β β Reports β β Detection β β Lambda β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β CloudWatch β β ML Model β β Remediation β β
β β Metrics β β (Historical)β β Actions β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
AWS Cost Anomaly Detection:
# Create cost anomaly detector
ce = boto3.client('ce')
ce.create_cost_anomaly_detector(
AnomalyDetector={
'DimensionKey': 'SERVICE',
'MatchOptions': ['EQUALS'],
'Values': ['Amazon Elastic Compute Cloud - Compute']
},
MonitorFrequency='DAILY'
)
# Create anomaly monitor
ce.create_cost_monitor(
Monitor={
'MonitorType': 'DIMENSIONAL',
'DimensionKey': 'SERVICE',
'MatchOptions': ['EQUALS'],
'Values': ['Amazon Elastic Compute Cloud - Compute']
}
)
# Subscribe to alerts
ce.create_cost_anomaly_subscription(
Subscription={
'AlertSubscription': {
'Channel': 'EMAIL',
'AddrESS': 'finops@example.com'
},
'MonitorArnList': ['arn:aws:ce::123456789:anomaly-detector/12345678'],
'Subscriber': {
'Type': 'EMAIL',
'Address': 'finops@example.com'
}
}
)
Q7: How do you implement tagging strategy for cost allocation?
Answer:
Tagging Strategy:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Allocation Tagging Strategy β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Required Tags β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ CostCenter: Department/Business Unit β β
β β β’ Environment: Production/Dev/Test β β
β β β’ Team: Ownership team β β
β β β’ Project: Project name β β
β β β’ Owner: Individual responsible β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optional Tags β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Application: Application name β β
β β β’ Tier: Data tier (hot/warm/cold) β β
β β β’ Compliance: Regulatory requirement β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Tag Enforcement β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Service Control Policies (SCPs) β β
β β β’ IAM policies β β
β β β’ Config rules β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Tag Enforcement via SCP:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "RequireCostCenterTag",
"Effect": "Deny",
"Action": [
"ec2:RunInstances",
"rds:CreateDBInstance",
"redshift:CreateCluster",
"s3:CreateBucket"
],
"Resource": "*",
"Condition": {
"StringNotEquals": {
"aws:RequestTag/CostCenter": [
"Engineering",
"Marketing",
"Sales",
"Finance"
]
}
}
}
]
}
Q8: How do you calculate cost per transaction?
Answer:
Unit Economics Implementation:
class UnitEconomics:
def __init__(self):
self.ce = boto3.client('ce')
def calculate_cost_per_transaction(self):
# Get total cost
cost_response = self.ce.get_cost_and_usage(
TimePeriod={
'Start': (datetime.now().replace(day=1)).strftime('%Y-%m-%d'),
'End': datetime.now().strftime('%Y-%m-%d')
},
Granularity='MONTHLY',
Metrics=['BlendedCost']
)
total_cost = float(cost_response['ResultsByTime'][0]['Total']['BlendedCost']['Amount'])
# Get transaction count (from application metrics)
transactions = self.get_transaction_count()
cost_per_transaction = total_cost / transactions if transactions > 0 else 0
return {
'total_cost': total_cost,
'transactions': transactions,
'cost_per_transaction': cost_per_transaction,
'period': datetime.now().strftime('%Y-%m')
}
def get_transaction_count(self):
# Query CloudWatch or application metrics
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.get_metric_statistics(
Namespace='Application/Transactions',
MetricName='TransactionCount',
StartTime=datetime.now().replace(day=1),
EndTime=datetime.now(),
Period=86400 * 30,
Statistics=['Sum']
)
return sum(datapoint['Sum'] for datapoint in response['Datapoints'])
Cost Dashboard:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Unit Economics Dashboard β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Monthly Costs Transactions Cost/Transaction β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β $15,234 β β 2,456,789 β β $0.0062 β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
β Cost by Service Cost by Team β
β βββββββββββββββββββββββ βββββββββββββββββββββββ β
β β EC2: 35% β β Engineering: 45% β β
β β RDS: 25% β β Data: 30% β β
β β S3: 15% β β Analytics: 15% β β
β β Lambda: 10% β β DevOps: 10% β β
β β Other: 15% β β β β
β βββββββββββββββββββββββ βββββββββββββββββββββββ β
β β
β Trends β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Cost per transaction: $0.0062 β $0.0058 (β6.5%) β β
β β Total cost: $15,234 β $14,892 (β2.2%) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q9: How do you implement budget controls?
Answer:
Budget Control Architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Budget Control Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Budget Definition Monitoring Enforcement β
β ββββββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Monthly Budget βββββΆβ CloudWatch ββββΆβ SCP/Quotas β β
β β Service Budget β β Alarms β β Auto-shutdownβ β
β β Team Budget β β Alerts β β β β
β ββββββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
β Thresholds β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 50%: Informational alert β β
β β 70%: Warning alert β β
β β 90%: Critical alert (auto-actions) β β
β β 100%: Quota enforcement β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Budget Implementation:
# Create budget
budgets = boto3.client('budgets')
budgets.create_budget(
AccountId='123456789',
Budget={
'BudgetName': 'Monthly-Data-Engineering',
'BudgetLimit': {
'Amount': '10000',
'Unit': 'USD'
},
'TimeUnit': 'MONTHLY',
'BudgetType': 'COST',
'CostFilters': {
'TagKeyValue': [
'user:CostCenter$DataEngineering'
]
}
},
NotificationsWithSubscribers=[
{
'Notification': {
'NotificationType': 'ACTUAL',
'ComparisonOperator': 'GREATER_THAN',
'Threshold': 70,
'ThresholdType': 'PERCENTAGE'
},
'Subscribers': [
{
'SubscriptionType': 'EMAIL',
'Address': 'finops@example.com'
}
]
}
]
)
Q10: How do you optimize data transfer costs?
Answer:
Data Transfer Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Transfer Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Transfer Types & Costs β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β S3 β Internet: $0.09/GB β β
β β S3 β Same Region: $0.01/GB β β
β β S3 β Cross-Region: $0.02/GB β β
β β EC2 β Internet: $0.09/GB β β
β β EC2 β Same AZ: Free β β
β β EC2 β Cross-AZ: $0.01/GB β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimization Strategies β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Use VPC endpoints for S3/DynamoDB β β
β β β’ Keep processing in same region β β
β β β’ Use CloudFront for internet delivery β β
β β β’ Compress data before transfer β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
VPC Endpoint for Cost Reduction:
# Create VPC endpoint (saves $0.08/GB)
ec2 = boto3.client('ec2')
ec2.create_vpc_endpoint(
VpcId='vpc-12345678',
ServiceName='com.amazonaws.us-east-1.s3',
RouteTableIds=['rtb-12345678'],
VpcEndpointType='Gateway'
)
# Cost savings calculation
def calculate_endpoint_savings():
monthly_gb = 10000 # 10TB transfer
without_endpoint = monthly_gb * 0.09 # $900
with_endpoint = monthly_gb * 0.01 # $100
return {
'monthly_savings': without_endpoint - with_endpoint,
'annual_savings': (without_endpoint - with_endpoint) * 12,
'savings_percentage': 89
}
Q11: How do you implement cost optimization for Redshift?
Answer:
Redshift Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Redshift Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Instance Strategy β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Provisioned (Reserved): Predictable workloads β β
β β Serverless: Variable workloads β β
β β Auto-suspend: Dev/test environments β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Cost Comparison (ra3.xlplus) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β On-Demand: $0.25/hr = $180/month β β
β β 1yr Reserved: $0.16/hr = $115/month (36% savings) β β
β β 3yr Reserved: $0.10/hr = $72/month (60% savings) β β
β β Serverless: $0.375/RPU-hr (variable) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Auto-Suspend Strategy β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Business Hours (9-6): Running β β
β β Off Hours (6-9am, weekends): Suspended β β
β β Savings: 60-70% β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Auto-Suspend Lambda:
def lambda_handler(event, context):
redshift = boto3.client('redshift')
# Check current hour (UTC)
current_hour = datetime.now().hour
# Business hours: 9 AM - 6 PM UTC
business_hours = range(9, 18)
clusters = redshift.describe_clusters()['Clusters']
for cluster in clusters:
cluster_id = cluster['ClusterIdentifier']
status = cluster['ClusterStatus']
if current_hour not in business_hours and status == 'available':
# Suspend cluster
redshift.pause_cluster(ClusterIdentifier=cluster_id)
elif current_hour in business_hours and status == 'paused':
# Resume cluster
redshift.resume_cluster(ClusterIdentifier=cluster_id)
Q12: How do you implement cost reporting and dashboards?
Answer:
Cost Reporting Architecture:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Reporting Architecture β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Data Collection Visualization Distribution β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Cost & UsageβββββββΆβ QuickSight ββββββΆβ Email β β
β β Reports β β Dashboard β β Reports β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β S3 β β CloudWatch β β Slack β β
β β (Archive) β β Metrics β β Notificationsβ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
QuickSight Dashboard:
# Create QuickSight dashboard
quicksight = boto3.client('quicksight')
# Create data source from Cost Explorer
response = quicksight.create_data_source(
AwsAccountId='123456789',
DataSourceId='cost-data-source',
Name='Cost Explorer Data',
Type='ATHENA',
Parameters={
'Athena': {
'WorkGroup': 'cost-analysis'
}
}
)
# Create dashboard
dashboard = quicksight.create_dashboard(
AwsAccountId='123456789',
DashboardId='cost-dashboard',
Name='Cost Optimization Dashboard',
SourceEntity={
'SourceTemplate': {
'DataSetReferences': [
{
'DataSetArn': 'arn:aws:quicksight:us-east-1:123456789:dataset/cost-data',
'DataSetPlaceholder': 'CostData'
}
],
'Arn': 'arn:aws:quicksight:us-east-1:123456789:template/cost-template'
}
}
)
Q13: How do you optimize Lambda costs?
Answer:
Lambda Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Lambda Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Pricing Model β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Requests: $0.20 per 1M requests β β
β β Duration: $0.0000166667 per GB-second β β
β β Free tier: 1M requests + 400,000 GB-seconds/month β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimization Strategies β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Right-size memory (CPU scales with memory) β β
β β Optimize code execution time β β
β β Use provisioned concurrency for steady workloads β β
β β Batch operations to reduce invocations β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Cost Comparison β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β 1M requests @ 100ms, 128MB: ~$2.08/month β β
β β 1M requests @ 100ms, 256MB: ~$4.17/month β β
β β 1M requests @ 50ms, 128MB: ~$1.04/month (50% less) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Lambda Right-Sizing:
import boto3
import json
cloudwatch = boto3.client('cloudwatch')
def analyze_lambda_performance(function_name):
# Get duration metrics
response = cloudwatch.get_metric_statistics(
Namespace='AWS/Lambda',
MetricName='Duration',
Dimensions=[{'Name': 'FunctionName', 'Value': function_name}],
StartTime=datetime.now() - timedelta(days=30),
EndTime=datetime.now(),
Period=86400,
Statistics=['Average', 'Maximum', 'p99']
)
avg_duration = response['Datapoints'][0]['Average']
max_duration = response['Datapoints'][0]['Maximum']
# Recommend memory based on duration
if avg_duration < 50:
recommended_memory = 128
elif avg_duration < 100:
recommended_memory = 256
elif avg_duration < 200:
recommended_memory = 512
else:
recommended_memory = 1024
return {
'function': function_name,
'avg_duration': avg_duration,
'max_duration': max_duration,
'recommended_memory': recommended_memory,
'cost_savings': calculate_savings(function_name, recommended_memory)
}
Q14: How do you implement cost anomaly detection and response?
Answer:
Anomaly Detection and Response:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Anomaly Detection & Response β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Detection Classification Response β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β Cost AnomalyβββββΆβ Severity βββββββΆβ Auto-Stop β β
β β Detection β β Classificationβ β Scale Down β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β β β
β βΌ βΌ βΌ β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β CloudWatch β β ML Model β β Notify β β
β β Metrics β β (Patterns) β β FinOps Team β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Automated Response Lambda:
def lambda_handler(event, context):
# Parse anomaly alert
anomaly = json.loads(event['Records'][0]['Sns']['Message'])
severity = anomaly['severity']
service = anomaly['service']
amount = anomaly['amount']
# Classification and response
if severity > 80: # Critical
# Immediate action
if service == 'EC2':
# Stop non-critical instances
stop_non_critical_instances()
elif service == 'RDS':
# Pause dev/test instances
pause_dev_instances()
notify_finops_team(anomaly, 'CRITICAL')
elif severity > 60: # High
# Scale down
if service == 'EC2':
scale_down_instances()
notify_finops_team(anomaly, 'HIGH')
elif severity > 40: # Medium
# Investigate
create_investigation_ticket(anomaly)
else: # Low
# Log for review
log_anomaly(anomaly)
Q15: How do you implement cost optimization for S3?
Answer:
S3 Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β S3 Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Storage Classes β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Standard: $0.023/GB-month β β
β β Intelligent-Tiering: $0.023-0.0025/GB-month β β
β β Standard-IA: $0.0125/GB-month β β
β β One Zone-IA: $0.01/GB-month β β
β β Glacier: $0.0036-0.00099/GB-month β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Request Costs β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β PUT/COPY/POST: $0.005/1,000 requests β β
β β GET/SELECT: $0.0004/1,000 requests β β
β β Lifecycle transitions: $0.01/1,000 requests β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimization Strategies β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Intelligent-Tiering for unknown access patterns β β
β β β’ Lifecycle policies for automatic tiering β β
β β β’ S3 Batch Operations for bulk transitions β β
β β β’ S3 Inventory for cleanup β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Intelligent-Tiering Configuration:
# Enable Intelligent-Tiering
s3 = boto3.client('s3')
s3.put_bucket_intelligent_tiering_configuration(
Bucket='data-lake-bucket',
Id='full-bucket',
IntelligentTieringConfiguration={
'Status': 'Enabled',
'Filter': {'Prefix': ''},
'Tierings': [
{
'AccessTier': 'ARCHIVE_ACCESS',
'Days': 90
},
{
'AccessTier': 'DEEP_ARCHIVE_ACCESS',
'Days': 180
}
]
}
)
# Monitor tiering savings
def calculate_intelligent_tiering_savings():
# Before: All Standard
before_cost = 1000 * 0.023 # $23/GB-month
# After: Intelligent-Tiering (assuming 60% moves to IA, 30% to Archive)
after_cost = (1000 * 0.4 * 0.023) + (1000 * 0.6 * 0.0125) # $14.95/GB-month
return {
'monthly_savings': before_cost - after_cost,
'savings_percentage': 35
}
Q16: How do you implement FinOps culture in an organization?
Answer:
FinOps Culture Implementation:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FinOps Culture Framework β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Education Accountability Governance β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Training β β Team Budgets β β Policies β β
β β Workshops β β Cost Owners β β Standards β β
β β Best Practicesβ β KPIs β β Reviews β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
β Visibility Optimization Continuous Improv. β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β Dashboards β β Recommendationsβ β Monthly Reviewsβ β
β β Reports β β Experiments β β Optimization β β
β β Alerts β β Automation β β Roadmap β β
β ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
FinOps Maturity Model:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β FinOps Maturity Levels β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Level 1: Crawl (Basic) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Basic cost visibility β β
β β β’ Manual optimization β β
β β β’ Ad-hoc reporting β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Level 2: Walk (Intermediate) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Automated alerts β β
β β β’ Rightsizing recommendations β β
β β β’ Tag enforcement β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Level 3: Run (Advanced) β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Full unit economics β β
β β β’ Predictive analytics β β
β β β’ Automated optimization β β
β β β’ Continuous improvement β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q17: How do you optimize EMR costs?
Answer:
EMR Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β EMR Cost Optimization Strategies β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Instance Selection β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Graviton instances: 20% cheaper β β
β β β’ Spot instances: 60-70% cheaper β β
β β β’ Right-size based on workload β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Cluster Management β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Auto-scaling: Match capacity to demand β β
β β β’ Auto-terminate: Shut down when idle β β
β β β’ Use EMR Serverless for variable workloads β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Cost Comparison β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β m5.xlarge On-Demand: $0.192/hr β β
β β m6g.xlarge On-Demand: $0.154/hr (20% savings) β β
β β m5.xlarge Spot: $0.058/hr (70% savings) β β
β β m6g.xlarge Spot: $0.046/hr (76% savings) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
EMR Cost Optimizer:
class EMRCostOptimizer:
def __init__(self):
self.emr = boto3.client('emr')
def optimize_cluster(self, cluster_id):
# Get cluster details
response = self.emr.describe_cluster(ClusterId=cluster_id)
cluster = response['Cluster']
# Analyze utilization
utilization = self.analyze_utilization(cluster_id)
recommendations = []
# Right-sizing recommendation
if utilization['avg_cpu'] < 30:
recommendations.append({
'type': 'RIGHT_SIZE',
'current': cluster['Ec2InstanceAttributes']['Ec2InstanceType'],
'recommended': 'm5.large',
'savings': 50
})
# Spot recommendation
if cluster['Ec2InstanceAttributes']['RequestedInstanceType'] != 'spot':
recommendations.append({
'type': 'USE_SPOT',
'current': 'On-Demand',
'recommended': 'Spot',
'savings': 70
})
# Graviton recommendation
if 'm5' in cluster['Ec2InstanceAttributes']['Ec2InstanceType']:
recommendations.append({
'type': 'USE_GRAVITON',
'current': 'm5.xlarge',
'recommended': 'm6g.xlarge',
'savings': 20
})
return recommendations
Q18: How do you implement cost controls for development environments?
Answer:
Dev Environment Cost Controls:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Development Environment Cost Controls β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Time-Based Controls β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Auto-suspend outside business hours β β
β β β’ Auto-terminate on weekends β β
β β β’ Maximum runtime limits β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Resource Limits β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Instance size restrictions β β
β β β’ Maximum instance count β β
β β β’ Storage quotas β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Monitoring & Alerts β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Daily cost alerts β β
β β β’ Budget thresholds β β
β β β’ Anomaly detection β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Dev Environment Scheduler:
def lambda_handler(event, context):
# Auto-suspend dev resources
ec2 = boto3.client('ec2')
rds = boto3.client('rds')
current_hour = datetime.now().hour
is_weekday = datetime.now().weekday() < 5
# Business hours: 9 AM - 7 PM UTC, weekdays only
business_hours = is_weekday and 9 <= current_hour <= 19
if not business_hours:
# Stop dev EC2 instances
instances = ec2.describe_instances(
Filters=[{'Name': 'tag:Environment', 'Values': ['development']}]
)
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
if instance['State']['Name'] == 'running':
ec2.stop_instances(InstanceIds=[instance['InstanceId']])
# Pause dev RDS instances
db_instances = rds.describe_db_instances()
for db in db_instances['DBInstances']:
if db['DBInstanceIdentifier'].startswith('dev-'):
if db['DBInstanceStatus'] == 'available':
rds.stop_db_instance(DBInstanceIdentifier=db['DBInstanceIdentifier'])
Q19: How do you calculate ROI for cloud cost optimization?
Answer:
ROI Calculation Framework:
class CloudROI:
def calculate_roi(self, optimization_data):
# Cost savings
monthly_savings = optimization_data['monthly_savings']
annual_savings = monthly_savings * 12
# Implementation costs
implementation_cost = optimization_data['implementation_cost']
ongoing_cost = optimization_data['ongoing_cost']
# Time to value
time_to_value_months = optimization_data['time_to_value']
# ROI calculation
total_benefit = annual_savings
total_cost = implementation_cost + (ongoing_cost * 12)
roi = ((total_benefit - total_cost) / total_cost) * 100
# Payback period
payback_months = implementation_cost / monthly_savings
return {
'annual_savings': annual_savings,
'implementation_cost': implementation_cost,
'annual_ongoing_cost': ongoing_cost * 12,
'roi_percentage': roi,
'payback_months': payback_months,
'npv_3_year': self.calculate_npv(annual_savings, ongoing_cost, implementation_cost)
}
def calculate_npv(self, annual_savings, annual_cost, initial_cost, discount_rate=0.1):
npv = -initial_cost
for year in range(1, 4):
cash_flow = annual_savings - annual_cost
npv += cash_flow / ((1 + discount_rate) ** year)
return npv
ROI Dashboard:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Optimization ROI Dashboard β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Annual Savings Implementation Cost ROI β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β $125,000 β β $15,000 β β 733% β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β
β Payback Period NPV (3 Year) β
β βββββββββββββββ βββββββββββββββ β
β β 1.4 months β β $298,000 β β
β βββββββββββββββ βββββββββββββββ β
β β
β Optimization Breakdown β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Reserved Instances: $45,000 (36%) β β
β β Spot Instances: $35,000 (28%) β β
β β Rightsizing: $25,000 (20%) β β
β β Storage Tiering: $20,000 (16%) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q20: How do you implement cost governance policies?
Answer:
Cost Governance Framework:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Cost Governance Framework β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Policies Enforcement Monitoring β
β ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ β
β β Spending Limits β β SCPs β β Budget Alerts β β
β β Resource Limits β β IAM Policies β β Cost Reports β β
β β Tag Requirements β β Config Rules β β Anomaly Detect β β
β ββββββββββββββββββββ ββββββββββββββββββββ ββββββββββββββββββ β
β β
β Approval Workflows β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Large purchase approval β β
β β β’ New service approval β β
β β β’ Reserved Instance purchases β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Service Control Policy for Cost Control:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyExpensiveInstances",
"Effect": "Deny",
"Action": "ec2:RunInstances",
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {
"ForAnyValue:StringNotLike": {
"ec2:InstanceType": [
"t3.*",
"t2.*",
"m5.*",
"m4.*",
"c5.*",
"c4.*",
"r5.*",
"r4.*"
]
}
}
},
{
"Sid": "DenyHighStorage",
"Effect": "Deny",
"Action": "ec2:RunInstances",
"Resource": "arn:aws:ec2:*:*:volume/*",
"Condition": {
"NumericGreaterThan": {
"ebs:volumeSize": "500"
}
}
}
]
}
Q21: How do you optimize data processing costs?
Answer:
Data Processing Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Data Processing Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Glue Optimization β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Right-size DPUs (start with 10, scale as needed) β β
β β β’ Use bookmarking for incremental processing β β
β β β’ Optimize partitions (128MB-1GB per file) β β
β β β’ Use Python Shell for simple jobs (0.0625 DPU) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β EMR Optimization β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Use Spot instances for task nodes β β
β β β’ Auto-scaling based on workload β β
β β β’ Right-size instance types β β
β β β’ Use EMR Serverless for variable workloads β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Athena Optimization β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Partition data for query pruning β β
β β β’ Use columnar formats (Parquet, ORC) β β
β β β’ Enable result caching β β
β β β’ Use reserved capacity for predictable queries β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q22: How do you implement cost allocation for multi-tenant systems?
Answer:
Multi-Tenant Cost Allocation:
class TenantCostAllocator:
def __init__(self):
self.ce = boto3.client('ce')
def allocate_costs(self, tenant_id, period):
# Get costs by tenant tag
response = self.ce.get_cost_and_usage(
TimePeriod={
'Start': period['start'],
'End': period['end']
},
Granularity='MONTHLY',
Metrics=['BlendedCost'],
Filter={
'Tags': {
'Key': 'TenantId',
'Values': [tenant_id]
}
}
)
total_cost = float(response['ResultsByTime'][0]['Total']['BlendedCost']['Amount'])
# Get tenant usage metrics
usage = self.get_tenant_usage(tenant_id, period)
# Calculate cost per unit
cost_per_query = total_cost / usage['total_queries'] if usage['total_queries'] > 0 else 0
cost_per_gb = total_cost / usage['data_processed_gb'] if usage['data_processed_gb'] > 0 else 0
return {
'tenant_id': tenant_id,
'period': period,
'total_cost': total_cost,
'usage': usage,
'cost_per_query': cost_per_query,
'cost_per_gb': cost_per_gb
}
Q23: How do you optimize network costs?
Answer:
Network Cost Optimization:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Network Cost Optimization β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Data Transfer Costs β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Internet β S3: $0.09/GB β β
β β S3 β Internet: $0.09/GB β β
β β Same Region (AZ to AZ): $0.01/GB β β
β β Cross-Region: $0.02/GB β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimization Strategies β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ VPC Endpoints: Free data transfer to S3/DynamoDB β β
β β β’ CloudFront: $0.085/GB (vs $0.09 direct) β β
β β β’ Same-region processing: Minimize cross-region β β
β β β’ Data compression: Reduce transfer volume β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Q24: How do you implement cost optimization automation?
Answer:
Cost Optimization Automation:
class CostOptimizationAutomation:
def __init__(self):
self.ec2 = boto3.client('ec2')
self.ce = boto3.client('ce')
def auto_terminate_idle_instances(self):
# Find idle instances (CPU < 5% for 7 days)
cloudwatch = boto3.client('cloudwatch')
instances = self.ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
for reservation in instances['Reservations']:
for instance in reservation['Instances']:
instance_id = instance['InstanceId']
# Check CPU utilization
response = cloudwatch.get_metric_statistics(
Namespace='AWS/EC2',
MetricName='CPUUtilization',
Dimensions=[{'Name': 'InstanceId', 'Value': instance_id}],
StartTime=datetime.now() - timedelta(days=7),
EndTime=datetime.now(),
Period=86400,
Statistics=['Average']
)
if response['Datapoints']:
avg_cpu = sum(dp['Average'] for dp in response['Datapoints']) / len(response['Datapoints'])
if avg_cpu < 5:
# Terminate idle instance
self.ec2.stop_instances(InstanceIds=[instance_id])
print(f"Stopped idle instance {instance_id} (CPU: {avg_cpu:.2f}%)")
def auto_snapshot_volumes(self):
# Snapshot volumes before termination
volumes = self.ec2.describe_volumes(
Filters=[{'Name': 'status', 'Values': ['available']}]
)
for volume in volumes['Volumes']:
# Check age
launch_time = volume['CreateTime']
age_days = (datetime.now(launch_time.tzinfo) - launch_time).days
if age_days > 30:
# Create snapshot
self.ec2.create_snapshot(
VolumeId=volume['VolumeId'],
Description=f"Auto-snapshot after {age_days} days"
)
Q25: How do you present cost optimization to leadership?
Answer:
Executive Cost Report:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Executive Cost Report β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β Current State β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Monthly Spend: $45,234 β β
β β Budget: $50,000 β β
β β Variance: -9.5% (Under Budget) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Optimization Achievements β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Reserved Instances: Saved $12,450/month β β
β β Spot Instances: Saved $8,230/month β β
β β Rightsizing: Saved $5,670/month β β
β β Storage Tiering: Saved $3,890/month β β
β β Total Savings: $30,240/month (40%) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β ROI Metrics β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Annual Savings: $362,880 β β
β β Implementation Cost: $25,000 β β
β β ROI: 1,351% β β
β β Payback Period: < 1 month β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β Recommendations β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β β’ Migrate to Graviton instances (additional 20% savings)β β
β β β’ Implement auto-scaling (additional 15% savings) β β
β β β’ Use S3 Intelligent-Tiering (additional 10% savings) β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Summary
Mastering AWS cost optimization requires understanding:
- Pricing Models: On-Demand, Reserved, Spot, Savings Plans
- Optimization Strategies: Rightsizing, auto-scaling, storage tiering
- FinOps Practices: Cost visibility, accountability, governance
- Automation: Auto-termination, auto-scaling, budget controls
- Reporting: Dashboards, ROI calculations, executive reporting
These concepts form the foundation for building cost-effective, efficient data systems on AWS.