🎥 Make videos programmatically with React
rename calculateprice to estimateprice and
Jonny Burger817d15a54ffd
diff --git a/packages/docs/docs/calculateprice.md b/packages/docs/docs/calculateprice.mddeleted file mode 100644index 721971a0c2..0000000000--- a/packages/docs/docs/calculateprice.md+++ /dev/null@@ -1,5 +0,0 @@-----id: calculateprice-title: calculatePrice()-slug: /lambda/calculateprice----diff --git a/packages/docs/docs/estimateprice.md b/packages/docs/docs/estimateprice.mdnew file mode 100644index 0000000000..7e94986b76--- /dev/null+++ b/packages/docs/docs/estimateprice.md@@ -0,0 +1,52 @@+---+id: estimateprice+title: estimatePrice()+slug: /lambda/estimateprice+---++Calculates the AWS costs incurred for AWS Lambda given the region, execution duration and memory size based on the AWS Lambda pricing matrix.++During rendering, many Lambda functions are spawned:++- The main function spawns many worker functions, waits for chunks to be rendered, and stitches them together for the full video. This is the longest-running Lambda function.+- Render functions render a short portion of a video and then shut down.+- Other short-lived, negligible functions get launched for initializing lambdas and fetching progress.++The total duration is the sum of execution duration of all of the above Lambda functions.+This duration can be passed to `estimatePrice()` to estimate the cost of AWS Lambda.++The calculated duration does not include costs for S3 and Remotion licensing fees.++## Example++```ts twoslash+import {estimatePrice} from '@remotion/lambda';++console.log(+ estimatePrice({+ region: 'us-east-1',+ durationInMiliseconds: 20000,+ memorySizeInMb: 2048,+ })+) // 0.00067+```++## Arguments++An object containing the following parameters:++### `region`++The region in which the Lambda function is executed in. [Pricing varies across regions](/docs/lambda/region-selection#other-considerations).++### `memorySizeInMb`++The amount of memory that has been given to the Lambda function. May be received with [`getFunctionInfo()`](/docs/lambda/getfunctioninfo).++### `durationInMiliseconds`++The estimated total execution duration in miliseconds. See the top of this page for a guide on how to approximate the duration.++## Return value++The estimated cost in USD as a `number`.diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.jsindex 28eba640d4..0a08240603 100644--- a/packages/docs/sidebars.js+++ b/packages/docs/sidebars.js@@ -17,7 +17,7 @@ module.exports = {type: "category",label: "Node.JS APIs",items: [- "calculateprice",+ "estimateprice","deployfunction","deletefunction","getfunctioninfo",diff --git a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.tsindex 9fded2f68f..0f51beee0c 100644--- a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts+++ b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts@@ -1,5 +1,5 @@import {_Object} from '@aws-sdk/client-s3';-import {calculatePrice} from '../../pricing/calculate-price';+import {estimatePrice} from '../../pricing/calculate-price';import {lambdaTimingsPrefix, RenderMetadata} from '../../shared/constants';import {parseLambdaTimingsKey} from '../../shared/parse-lambda-timings-key';import {findOutputFileInBucket} from './find-output-file-in-bucket';@@ -7,7 +7,7 @@ import {getCurrentRegionInFunction} from './get-current-region';import {getTimeToFinish} from './get-time-to-finish';// TODO: Should differentiate between finished and in progress-export const calculatePriceFromBucket = ({+export const estimatePriceFromBucket = ({renderId,contents,renderMetadata,@@ -61,10 +61,10 @@ export const calculatePriceFromBucket = ({.reduce((a, b) => a + b, 0);const accruedSoFar = Number(- calculatePrice({+ estimatePrice({region: getCurrentRegionInFunction(),durationInMiliseconds: totalEncodingTimings + timeElapsedOfUnfinished,- memorySize,+ memorySizeInMb: memorySize,}).toPrecision(5));return accruedSoFar;diff --git a/packages/lambda/src/functions/helpers/create-post-render-data.ts b/packages/lambda/src/functions/helpers/create-post-render-data.tsindex 741c3e3dfc..90a2a7b595 100644--- a/packages/lambda/src/functions/helpers/create-post-render-data.ts+++ b/packages/lambda/src/functions/helpers/create-post-render-data.ts@@ -1,6 +1,6 @@import {_Object} from '@aws-sdk/client-s3';import {AwsRegion} from '../..';-import {calculatePrice} from '../../pricing/calculate-price';+import {estimatePrice} from '../../pricing/calculate-price';import {lambdaTimingsPrefix,PostRenderData,@@ -42,9 +42,9 @@ export const createPostRenderData = async ({.map((p) => p.end - p.start + OVERHEAD_TIME_PER_LAMBDA).reduce((a, b) => a + b);- const cost = calculatePrice({+ const cost = estimatePrice({durationInMiliseconds: times,- memorySize,+ memorySizeInMb: memorySize,region,});diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.tsindex 66b805c4ee..063cbcaacc 100644--- a/packages/lambda/src/functions/helpers/get-progress.ts+++ b/packages/lambda/src/functions/helpers/get-progress.ts@@ -7,7 +7,7 @@ import {RenderProgress,rendersPrefix,} from '../../shared/constants';-import {calculatePriceFromBucket} from './calculate-price-from-bucket';+import {estimatePriceFromBucket} from './calculate-price-from-bucket';import {findOutputFileInBucket} from './find-output-file-in-bucket';import {getCleanupProgress} from './get-cleanup-progress';import {getCurrentRegionInFunction} from './get-current-region';@@ -110,7 +110,7 @@ export const getProgress = async ({);const accruedSoFar = Number(- calculatePriceFromBucket({+ estimatePriceFromBucket({renderId,contents,renderMetadata,diff --git a/packages/lambda/src/index.ts b/packages/lambda/src/index.tsindex f40dc656aa..f0edd9b4b4 100644--- a/packages/lambda/src/index.ts+++ b/packages/lambda/src/index.ts@@ -14,6 +14,7 @@ import {import {renderVideoOnLambda} from './api/render-video-on-lambda';import {LambdaInternals} from './internals';import {AwsRegion} from './pricing/aws-regions';+import {estimatePrice} from './pricing/calculate-price';export {deployFunction,@@ -28,6 +29,7 @@ export {simulatePermissions,deleteFunction,getFunctionInfo,+ estimatePrice,LambdaInternals,};export type {AwsRegion};diff --git a/packages/lambda/src/pricing/calculate-price.ts b/packages/lambda/src/pricing/calculate-price.tsindex 53d3f701da..5691fe7566 100644--- a/packages/lambda/src/pricing/calculate-price.ts+++ b/packages/lambda/src/pricing/calculate-price.ts@@ -3,16 +3,23 @@ import {validateMemorySize} from '../shared/validate-memory-size';import {AwsRegion} from './aws-regions';import {pricing} from './price-per-1-s';-export const calculatePrice = ({- region,- durationInMiliseconds,- memorySize: memory,-}: {+type EstimatePriceInput = {region: AwsRegion;durationInMiliseconds: number;- memorySize: number;-}) => {- validateMemorySize(memory);+ memorySizeInMb: number;+};+/**+ *+ * @description Calculates the AWS costs incurred for AWS Lambda given the region, execution duration and memory size.+ * @returns `number` Price in USD+ * @link https://remotion.dev/docs/lambda/estimateprice+ */+export const estimatePrice = ({+ region,+ durationInMiliseconds,+ memorySizeInMb,+}: EstimatePriceInput) => {+ validateMemorySize(memorySizeInMb);validateAwsRegion(region);if (typeof durationInMiliseconds !== 'number') {throw new TypeError(@@ -40,7 +47,7 @@ export const calculatePrice = ({const timeCostDollars =Number(pricing[region]['Lambda Duration'].price) *- ((memory * durationInMiliseconds) / 1000 / 1024);+ ((memorySizeInMb * durationInMiliseconds) / 1000 / 1024);const invocationCost = Number(pricing[region]['Lambda Requests'].price);diff --git a/packages/lambda/src/test/pricing.test.ts b/packages/lambda/src/test/pricing.test.tsindex 4c94aaf578..2c3b43d140 100644--- a/packages/lambda/src/test/pricing.test.ts+++ b/packages/lambda/src/test/pricing.test.ts@@ -1,18 +1,18 @@-import {calculatePrice} from '../pricing/calculate-price';+import {estimatePrice} from '../pricing/calculate-price';test('Should calculate costs accurately', () => {expect(- calculatePrice({+ estimatePrice({region: 'us-east-1',durationInMiliseconds: 20000,- memorySize: 2048,+ memorySizeInMb: 2048,})).toEqual(0.00067);expect(- calculatePrice({+ estimatePrice({region: 'us-east-1',durationInMiliseconds: 20000,- memorySize: 1024,+ memorySizeInMb: 1024,})).toEqual(0.00033);});