Skip to content

Conversation

nogueiraanderson
Copy link
Contributor

@nogueiraanderson nogueiraanderson commented Oct 6, 2025

Cost Calculation Fix for EC2 Instance Reporting

Purpose

Lambda function that runs nightly to email instance owners about their running staging EC2 instances, including uptime and estimated costs, to remind them to shut down unused resources.

Before

# Wrong: sums all prices regardless of duration
for price in priceHistory['SpotPriceHistory']:
    totalCost += float(price['SpotPrice'])

After

# Right: multiplies price by hours in effect
for i, entry in enumerate(sorted_prices):
    hours = (period_end - period_start).total_seconds() / 3600
    totalCost += hours * price

Additional fixes: Spot/on-demand detection, Linux-only pricing, Python 3 compatibility, enhanced email formatting.

- Remove separate cloud/aws-functions/email_running_instances.py file
- Keep Lambda code inline in IaC/StagingStack.yml for simpler deployment
- Remove AWS CLI example command from template header
- Fix YAML formatting: proper comment spacing and remove trailing whitespaces
- Fix email_running_instances Lambda: accurate cost calculation for spot/on-demand instances
- Calculate time-weighted costs based on actual spot price history
- Add richer email format with instance type, lifecycle, and region/AZ info
- Fix Python 3 compatibility issues with filter() function
- Clean up YAML formatting: fix comment spacing and remove trailing whitespaces
@nogueiraanderson nogueiraanderson changed the title Fix Lambda function for accurate cost calculation and richer emails Fix EC2 cost calculation in staging notification Lambda Oct 6, 2025
- Add AWS Pricing API integration for accurate on-demand pricing
- Remove inaccurate spot × 2 fallback estimation
- Add pricing:GetProducts IAM permission to Lambda role
- Keep describe_spot_price_history for spot instances (API doesn't provide spot prices)
- Limit region support to US and Europe only
- Fix YAML formatting: remove trailing whitespaces and extra spaces before comments
- Update email recipient list
def lambda_handler(event, context):
fullReportEmail = ['[email protected]', '[email protected]']
fullReportEmail = ['[email protected]', '[email protected]', '[email protected]']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fullReportEmail = ['alexander.tymchuk@percona.com', '[email protected]', '[email protected]']
fullReportEmail = ['alexander.demidoff@percona.com', '[email protected]', '[email protected]']

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, I don't remember when I got an email )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants