Skip to content

Query EKS price from AWS Pricing API #783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/cmd/lib_cluster_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,14 @@ func getClusterUpdateConfig(cachedClusterConfig clusterconfig.Config, awsCreds A
}

func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds AWSCredentials, awsClient *aws.Client) {
eksPrice := aws.EKSPrices[*clusterConfig.Region]
operatorInstancePrice := aws.InstanceMetadatas[*clusterConfig.Region]["t3.medium"].Price
operatorEBSPrice := aws.EBSMetadatas[*clusterConfig.Region].Price * 20 / 30 / 24
elbPrice := aws.ELBMetadatas[*clusterConfig.Region].Price
natPrice := aws.NATMetadatas[*clusterConfig.Region].Price
apiInstancePrice := aws.InstanceMetadatas[*clusterConfig.Region][*clusterConfig.InstanceType].Price
apiEBSPrice := aws.EBSMetadatas[*clusterConfig.Region].Price * float64(clusterConfig.InstanceVolumeSize) / 30 / 24
fixedPrice := 0.20 + operatorInstancePrice + operatorEBSPrice + 2*elbPrice + natPrice
fixedPrice := eksPrice + operatorInstancePrice + operatorEBSPrice + 2*elbPrice + natPrice
totalMinPrice := fixedPrice + float64(*clusterConfig.MinInstances)*(apiInstancePrice+apiEBSPrice)
totalMaxPrice := fixedPrice + float64(*clusterConfig.MaxInstances)*(apiInstancePrice+apiEBSPrice)

Expand All @@ -301,7 +302,7 @@ func confirmInstallClusterConfig(clusterConfig *clusterconfig.Config, awsCreds A
}

rows := [][]interface{}{}
rows = append(rows, []interface{}{"1 eks cluster", "$0.20"})
rows = append(rows, []interface{}{"1 eks cluster", s.DollarsMaxPrecision(eksPrice)})
rows = append(rows, []interface{}{"1 20gb ebs volume for the operator", s.DollarsAndTenthsOfCents(operatorEBSPrice)})
rows = append(rows, []interface{}{"1 t3.medium for the operator", s.DollarsMaxPrecision(operatorInstancePrice)})
rows = append(rows, []interface{}{"1 nat gateway", s.DollarsMaxPrecision(natPrice)})
Expand Down
47 changes: 45 additions & 2 deletions pkg/lib/aws/gen_resource_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@

OUTPUT_FILE_NAME = "resource_metadata.go"

PRICING_ENDPOINT_TEMPLATE = (
EC2_PRICING_ENDPOINT_TEMPLATE = (
"https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/{}/index.json"
)

EKS_PRICING_ENDPOINT_TEMPLATE = (
"https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEKS/current/{}/index.json"
)


def get_instance_metadatas(pricing):
instance_mapping = {}
Expand Down Expand Up @@ -136,6 +140,29 @@ def get_ebs_metadata(pricing):
return {"price": float(price)}


def get_eks_price(region):
response = requests.get(EKS_PRICING_ENDPOINT_TEMPLATE.format(region))
pricing = response.json()

for product_id, product in pricing["products"].items():
if product.get("attributes") is None:
continue
if product.get("productFamily") != "Compute":
continue
if product["attributes"].get("servicecode") != "AmazonEKS":
continue
if product["attributes"].get("operation") != "CreateOperation":
continue
if not product["attributes"].get("usagetype", "").endswith("-AmazonEKS-Hours:perCluster"):
continue

price_dimensions = list(pricing["terms"]["OnDemand"][product["sku"]].values())[0][
"priceDimensions"
]
price = list(price_dimensions.values())[0]["pricePerUnit"]["USD"]
return float(price)


file_template = Template(
"""/*
Copyright 2020 Cortex Labs, Inc.
Expand Down Expand Up @@ -204,6 +231,11 @@ def get_ebs_metadata(pricing):
var EBSMetadatas = map[string]EBSMetadata{
${ebs_region_map}
}

// region -> EKS price
var EKSPrices = map[string]float64{
${eks_region_map}
}
"""
)

Expand Down Expand Up @@ -234,23 +266,30 @@ def get_ebs_metadata(pricing):
"""
)

eks_region_map_template = Template(
""""${region}": ${price},
"""
)


def main():
instance_region_map_str = ""
elb_region_map_str = ""
nat_region_map_str = ""
ebs_region_map_str = ""
eks_region_map_str = ""

for i, region in enumerate(sorted(REGIONS), start=1):
print("generating region {}/{} ({})...".format(i, len(REGIONS), region))

response = requests.get(PRICING_ENDPOINT_TEMPLATE.format(region))
response = requests.get(EC2_PRICING_ENDPOINT_TEMPLATE.format(region))
pricing = response.json()

instance_metadatas = get_instance_metadatas(pricing)
elb_metadata = get_elb_metadata(pricing)
nat_metadata = get_nat_metadata(pricing)
ebs_metadata = get_ebs_metadata(pricing)
eks_price = get_eks_price(region)

instance_metadatas_str = ""

Expand Down Expand Up @@ -279,13 +318,17 @@ def main():
ebs_region_map_str += ebs_region_map_template.substitute(
{"region": region, "price": ebs_metadata["price"]}
)
eks_region_map_str += eks_region_map_template.substitute(
{"region": region, "price": eks_price}
)

file_str = file_template.substitute(
{
"instance_region_map": instance_region_map_str,
"elb_region_map": elb_region_map_str,
"nat_region_map": nat_region_map_str,
"ebs_region_map": ebs_region_map_str,
"eks_region_map": eks_region_map_str,
}
)

Expand Down
25 changes: 23 additions & 2 deletions pkg/lib/aws/resource_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,7 +1070,7 @@ var InstanceMetadatas = map[string]map[string]InstanceMetadata{
"g4dn.4xlarge": {Region: "ap-southeast-2", Type: "g4dn.4xlarge", Memory: kresource.MustParse("65536Mi"), CPU: kresource.MustParse("16"), GPU: 1, Price: 1.566},
"g4dn.8xlarge": {Region: "ap-southeast-2", Type: "g4dn.8xlarge", Memory: kresource.MustParse("131072Mi"), CPU: kresource.MustParse("32"), GPU: 1, Price: 2.83},
"g4dn.xlarge": {Region: "ap-southeast-2", Type: "g4dn.xlarge", Memory: kresource.MustParse("16384Mi"), CPU: kresource.MustParse("4"), GPU: 1, Price: 0.684},
"hs1.8xlarge": {Region: "ap-southeast-2", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("17"), GPU: 0, Price: 5.57},
"hs1.8xlarge": {Region: "ap-southeast-2", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 5.57},
"i2.2xlarge": {Region: "ap-southeast-2", Type: "i2.2xlarge", Memory: kresource.MustParse("62464Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 2.035},
"i2.4xlarge": {Region: "ap-southeast-2", Type: "i2.4xlarge", Memory: kresource.MustParse("124928Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 4.07},
"i2.8xlarge": {Region: "ap-southeast-2", Type: "i2.8xlarge", Memory: kresource.MustParse("249856Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 8.14},
Expand Down Expand Up @@ -1789,7 +1789,7 @@ var InstanceMetadatas = map[string]map[string]InstanceMetadata{
"h1.2xlarge": {Region: "eu-west-1", Type: "h1.2xlarge", Memory: kresource.MustParse("32768Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 0.519},
"h1.4xlarge": {Region: "eu-west-1", Type: "h1.4xlarge", Memory: kresource.MustParse("65536Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 1.038},
"h1.8xlarge": {Region: "eu-west-1", Type: "h1.8xlarge", Memory: kresource.MustParse("131072Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 2.076},
"hs1.8xlarge": {Region: "eu-west-1", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("17"), GPU: 0, Price: 4.9},
"hs1.8xlarge": {Region: "eu-west-1", Type: "hs1.8xlarge", Memory: kresource.MustParse("119808Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 4.9},
"i2.2xlarge": {Region: "eu-west-1", Type: "i2.2xlarge", Memory: kresource.MustParse("62464Mi"), CPU: kresource.MustParse("8"), GPU: 0, Price: 1.876},
"i2.4xlarge": {Region: "eu-west-1", Type: "i2.4xlarge", Memory: kresource.MustParse("124928Mi"), CPU: kresource.MustParse("16"), GPU: 0, Price: 3.751},
"i2.8xlarge": {Region: "eu-west-1", Type: "i2.8xlarge", Memory: kresource.MustParse("249856Mi"), CPU: kresource.MustParse("32"), GPU: 0, Price: 7.502},
Expand Down Expand Up @@ -3367,3 +3367,24 @@ var EBSMetadatas = map[string]EBSMetadata{
"us-east-2": {Region: "us-east-2", Price: 0.1},
"us-west-2": {Region: "us-west-2", Price: 0.1},
}

// region -> EKS price
var EKSPrices = map[string]float64{
"ap-east-1": 0.1,
"ap-northeast-1": 0.1,
"ap-northeast-2": 0.1,
"ap-south-1": 0.1,
"ap-southeast-1": 0.1,
"ap-southeast-2": 0.1,
"ca-central-1": 0.1,
"eu-central-1": 0.1,
"eu-north-1": 0.1,
"eu-west-1": 0.1,
"eu-west-2": 0.1,
"eu-west-3": 0.1,
"me-south-1": 0.1,
"sa-east-1": 0.1,
"us-east-1": 0.1,
"us-east-2": 0.1,
"us-west-2": 0.1,
}