Last active
December 8, 2023 21:03
-
-
Save fabidick22/1abb1f1b5d31641f54e25e9a95534273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tabulate import tabulate | |
def get_fargate_spot_cost(region, vcpu_cost=None, gb_cost=None): | |
""" | |
If vcpu_cost and vcpu_cost are 'None', the current value is retrived from AWS | |
""" | |
import requests | |
endpoint = "https://dftu77xade0tc.cloudfront.net/fargate-spot-prices.json" | |
try: | |
if vcpu_cost is not None and gb_cost is not None: | |
return vcpu_cost, gb_cost | |
data = requests.get(endpoint).json() | |
prices = data.get("prices", []) | |
cost_vcpu = cost_gb = None | |
for price in prices: | |
attributes = price.get("attributes", {}) | |
if attributes.get("aws:region") == region: | |
unit, price_value = price.get("unit"), price.get("price", {}).get("USD") | |
cost_vcpu = price_value if unit == "vCPU-Hours" else cost_vcpu | |
cost_gb = price_value if unit == "GB-Hours" else cost_gb | |
return cost_vcpu, cost_gb | |
except Exception as e: | |
print(f"Error retrieving Fargate Spot prices: {str(e)}") | |
return None, None | |
### SECTION TO EDIT ### | |
region = "us-east-1" | |
total_executions = 9322 # Monthly executions | |
average_execution_duration_minutes = 5 # Average execution duration in minutes | |
# Set static values (remove the static values if you want the current value) | |
fargate_spot_vcpu_cost_per_hour, fargate_spot_ram_cost_per_hour = get_fargate_spot_cost(region, 0.014577, 0.00160066) | |
data_transfer_gb_per_month = 100 | |
### SECTION TO EDIT ### | |
# Costs | |
github_actions_cost_per_minute = 0.008 | |
semaphoreci_cost_per_minute = 0.0075 | |
igw_internet_facing_cost_per_gb = 0.09 | |
# Used resources | |
github_actions_vcpu = 2 | |
github_actions_ram = 7 | |
semaphoreci_vcpu = 2 | |
semaphoreci_ram = 4 | |
fargate_spot_vcpu = 2 | |
fargate_spot_ram = 7 | |
# Calculations for GitHub Actions and SemaphoreCI | |
github_actions_monthly_cost = github_actions_cost_per_minute * total_executions * average_execution_duration_minutes | |
semaphoreci_monthly_cost = semaphoreci_cost_per_minute * total_executions * average_execution_duration_minutes | |
# Calculations for Fargate Spot | |
fargate_spot_monthly_cost = (fargate_spot_vcpu_cost_per_hour * fargate_spot_vcpu + fargate_spot_ram_cost_per_hour * fargate_spot_ram) * total_executions * average_execution_duration_minutes / 60 # Convert from minutes to hours | |
fargate_spot_monthly_cost = fargate_spot_monthly_cost + (data_transfer_gb_per_month * igw_internet_facing_cost_per_gb) | |
# Resources used by instance | |
github_actions_resources = f"{github_actions_vcpu} vCPU, {github_actions_ram} GB RAM" | |
semaphoreci_resources = f"{semaphoreci_vcpu} vCPU, {semaphoreci_ram} GB RAM" | |
fargate_spot_resources = f"{fargate_spot_vcpu} vCPU, {fargate_spot_ram} GB RAM" | |
table = [ | |
["Platform", "Monthly Cost", "Nro. of Executions", "Total Time per Execution", "Resources Used"], | |
["GitHub Actions (Standard)", f"${github_actions_monthly_cost:.2f} USD", total_executions, f"{average_execution_duration_minutes} min", github_actions_resources], | |
["SemaphoreCI (e1-standard-2)", f"${semaphoreci_monthly_cost:.2f} USD", total_executions, f"{average_execution_duration_minutes} min", semaphoreci_resources], | |
["Fargate Spot (Custom)", f"${fargate_spot_monthly_cost:.2f} USD", total_executions, f"{average_execution_duration_minutes} min", fargate_spot_resources], | |
] | |
print(tabulate(table, headers="firstrow", tablefmt="github")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment