Skip to content

Instantly share code, notes, and snippets.

@thiagozs
Created March 15, 2023 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagozs/019a25fbcbd987209ef1d25e10d0dde9 to your computer and use it in GitHub Desktop.
Save thiagozs/019a25fbcbd987209ef1d25e10d0dde9 to your computer and use it in GitHub Desktop.
AWS lambda calculator
package main
import (
"flag"
"fmt"
)
type Pricing struct {
Region string
PerRequestPrice float64 // in USD per 1 million requests
PerMemoryPrice float64 // in USD per GB-second
}
var pricingByRegion = map[string]Pricing{
"us-east-1": {
Region: "US East (N. Virginia)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"us-east-2": {
Region: "US East (Ohio)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"us-west-1": {
Region: "US West (N. California)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"us-west-2": {
Region: "US West (Oregon)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"af-south-1": {
Region: "Africa (Cape Town)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001833,
},
"ap-east-1": {
Region: "Asia Pacific (Hong Kong)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00002333,
},
"ap-south-1": {
Region: "Asia Pacific (Mumbai)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"ap-northeast-3": {
Region: "Asia Pacific (Osaka-Local)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00002,
},
"ap-northeast-2": {
Region: "Asia Pacific (Seoul)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"ap-southeast-1": {
Region: "Asia Pacific (Singapore)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"ap-southeast-2": {
Region: "Asia Pacific (Sydney)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"ap-northeast-1": {
Region: "Asia Pacific (Tokyo)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"ca-central-1": {
Region: "Canada (Central)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"eu-central-1": {
Region: "EU (Frankfurt)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"eu-west-1": {
Region: "EU (Ireland)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"eu-west-2": {
Region: "EU (London)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"eu-south-1": {
Region: "EU (Milan)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00002,
},
"eu-west-3": {
Region: "EU (Paris)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"eu-north-1": {
Region: "EU (Stockholm)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
"me-south-1": {
Region: "Middle East (Bahrain)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00002,
},
"sa-east-1": {
Region: "South America (São Paulo)",
PerRequestPrice: 0.20,
PerMemoryPrice: 0.00001667,
},
}
func calculateLambdaCost(region string, memory int, requests int, executionTime int) (float64, error) {
pricing, ok := pricingByRegion[region]
if !ok {
return 0.0, fmt.Errorf("unknown region %s", region)
}
gbSec := float64(requests) * float64(memory) / 1024.0
if executionTime > 1 {
gbSec = gbSec * (float64(executionTime) / 1000.0) // convert to seconds
}
computeCharges := gbSec * pricing.PerMemoryPrice
requestCharges := float64(requests) / 1000000.0 * pricing.PerRequestPrice
totalCharges := computeCharges + requestCharges
return totalCharges, nil
}
var (
region string
memory int
requests int
exectime int
)
func main() {
flag.StringVar(&region, "rg", "", "Region of AWS run - us-west-2")
flag.IntVar(&memory, "mm", 0, "Memory allocated - in MegaByte")
flag.IntVar(&requests, "rq", 0, "Total of Requests - times/month")
flag.IntVar(&exectime, "ex", 0, "Execution durationn - time in milliseconds")
flag.Parse()
if region == "" {
region = "us-west-2"
}
if memory == 0 {
memory = 256
}
if requests == 0 {
requests = 10000
}
if exectime == 0 {
exectime = 1000
}
costs, err := calculateLambdaCost(region, memory, requests, exectime)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Allocated Memory: %d MB\n", memory)
fmt.Printf("No of invocations (request): %d\n", requests)
fmt.Printf("Execution duration: %d milliseconds\n", exectime)
fmt.Printf("TotalCharges: $%.3f\n", costs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment