Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Last active July 18, 2017 00:40
Show Gist options
  • Save MarkTiedemann/63aed7dd68d9bfff4615d3dfa772cbc2 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/63aed7dd68d9bfff4615d3dfa772cbc2 to your computer and use it in GitHub Desktop.
Simple Azure Functions Cost Calculator

Live Demo: https://repl.it/J6L1/0

// --------- PARAM ---------

const invocations = _`100_000_000`
const avgRuntimeInSec = 0.5
const avgMemoryInMb = 256

// --------- CALC ---------

const RUNTIME_COSTS_PER_GB_SEC = _`0.000_014`
const FREE_RUNTIME_GB_SECS = _`400_000`

const consumptionInSec = invocations * avgRuntimeInSec
const consumptionInGbPerSec = avgMemoryInMb / 1024 * consumptionInSec
const reducedConsumptionInGbPerSec = consumptionInGbPerSec - FREE_RUNTIME_GB_SECS

const consumptionCosts = consumptionInGbPerSec * RUNTIME_COSTS_PER_GB_SEC
const reducedConsumptionCosts = reducedConsumptionInGbPerSec * RUNTIME_COSTS_PER_GB_SEC

log('consumptionCosts', consumptionCosts, 0)
log('reducedConsumptionCosts', reducedConsumptionCosts, 1)

const INVOCATION_COSTS_PER_MIL = 0.169
const FREE_INVOCATIONS = _`1_000_000`

const reducedInvocations = invocations - FREE_INVOCATIONS

const invocationCosts = invocations / _`1_000_000` * INVOCATION_COSTS_PER_MIL
const reducedInvocationsCosts = reducedInvocations / _`1_000_000` * INVOCATION_COSTS_PER_MIL

log('invocationCosts', invocationCosts, 0)
log('reducedInvocationCosts', reducedInvocationsCosts, 1)

const totalCosts = consumptionCosts + invocationCosts
const reducedTotalCosts = reducedConsumptionCosts + reducedInvocationsCosts

log('totalCosts', totalCosts, 0)
log('reducedTotalCosts', reducedTotalCosts, 1)

// --------- UTIL ---------

function _ (s) {
  return Number(s[0].replace(/_/g, ''))
}

function log (key, costs, line) {
  const maxKey = 24
  const maxVal = 9

  if (line === 0) console.log()
  console.log(
    '  ' + key.padEnd(maxKey),
    ' ',
    (typeof costs === 'number'
      ? (costs.toFixed(2) + ' €').padStart(maxVal)
      : costs) + '  '
  )
  if (line) log('—'.repeat(maxKey), '—'.repeat(maxVal))
}
λ node .

  consumptionCosts            175.00 €
  reducedConsumptionCosts     169.40 €
  ————————————————————————   —————————

  invocationCosts              16.90 €
  reducedInvocationCosts       16.73 €
  ————————————————————————   —————————

  totalCosts                  191.90 €
  reducedTotalCosts           186.13 €
  ————————————————————————   —————————
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment