Skip to content

🎥 Make videos programmatically with React

is it really that cheap?

Jonny Burgerbe133f8a1c45
diff --git a/packages/lambda/src/api/estimate-price.ts b/packages/lambda/src/api/estimate-price.ts
index 0312b94da9..23e8ab452d 100644
--- a/packages/lambda/src/api/estimate-price.ts
+++ b/packages/lambda/src/api/estimate-price.ts
@@ -1,12 +1,21 @@
+import {MIN_EPHEMERAL_STORAGE_IN_MB} from '../defaults';
import {AwsRegion} from '../pricing/aws-regions';
import {pricing} from '../pricing/price-per-1-s';
+import {
+ LambdaArchitecture,
+ validateArchitecture,
+} from '../shared/validate-architecture';
import {validateAwsRegion} from '../shared/validate-aws-region';
+import {validateDiskSizeInMb} from '../shared/validate-disk-size-in-mb';
import {validateMemorySize} from '../shared/validate-memory-size';
export type EstimatePriceInput = {
region: AwsRegion;
durationInMiliseconds: number;
memorySizeInMb: number;
+ diskSizeInMb: number;
+ architecture: LambdaArchitecture;
+ lambdasInvoked: number;
};
/**
*
@@ -18,9 +27,14 @@ export const estimatePrice = ({
region,
durationInMiliseconds,
memorySizeInMb,
+ diskSizeInMb,
+ architecture,
+ lambdasInvoked,
}: EstimatePriceInput): number => {
validateMemorySize(memorySizeInMb);
validateAwsRegion(region);
+ validateArchitecture(architecture);
+ validateDiskSizeInMb(diskSizeInMb);
if (typeof durationInMiliseconds !== 'number') {
throw new TypeError(
`Parameter 'durationInMiliseconds' must be a number but got ${typeof durationInMiliseconds}`
@@ -45,11 +59,36 @@ export const estimatePrice = ({
);
}
+ const durationPrice =
+ architecture === 'x86_64'
+ ? pricing[region]['Lambda Duration'].price
+ : pricing[region]['Lambda Duration-ARM'].price;
+
+ // In GB-second
const timeCostDollars =
- Number(pricing[region]['Lambda Duration'].price) *
+ Number(durationPrice) *
((memorySizeInMb * durationInMiliseconds) / 1000 / 1024);
- const invocationCost = Number(pricing[region]['Lambda Requests'].price);
+ const diskSizePrice =
+ architecture === 'x86_64'
+ ? pricing[region]['Lambda Storage-Duration'].price
+ : pricing[region]['Lambda Storage-Duration-ARM'].price;
+
+ const chargedDiskSize = Math.max(
+ 0,
+ diskSizeInMb - MIN_EPHEMERAL_STORAGE_IN_MB
+ );
+ // In GB-second
+ const diskSizeDollars =
+ chargedDiskSize *
+ Number(diskSizePrice) *
+ (durationInMiliseconds / 1000 / 1024);
+ console.log({diskSizeDollars, chargedDiskSize});
+
+ const invocationCost =
+ Number(pricing[region]['Lambda Requests'].price) * lambdasInvoked;
- return Number((timeCostDollars + invocationCost).toFixed(5));
+ return Number(
+ (timeCostDollars + diskSizeDollars + invocationCost).toFixed(5)
+ );
};
diff --git a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
index a985c4b72e..b2df211194 100644
--- a/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
+++ b/packages/lambda/src/functions/helpers/calculate-price-from-bucket.ts
@@ -2,6 +2,7 @@ import {_Object} from '@aws-sdk/client-s3';
import {estimatePrice} from '../../api/estimate-price';
import {lambdaTimingsPrefix, RenderMetadata} from '../../shared/constants';
import {parseLambdaTimingsKey} from '../../shared/parse-lambda-timings-key';
+import {LambdaArchitecture} from '../../shared/validate-architecture';
import {calculateChunkTimes} from './calculate-chunk-times';
import {OutputFileMetadata} from './find-output-file-in-bucket';
import {getCurrentRegionInFunction} from './get-current-region';
@@ -12,11 +13,17 @@ export const estimatePriceFromBucket = ({
renderMetadata,
memorySizeInMb,
outputFileMetadata,
+ architecture,
+ diskSizeInMb,
+ lambdasInvoked,
}: {
contents: _Object[];
renderMetadata: RenderMetadata | null;
memorySizeInMb: number;
outputFileMetadata: OutputFileMetadata | null;
+ architecture: LambdaArchitecture;
+ diskSizeInMb: number;
+ lambdasInvoked: number;
}) => {
if (!renderMetadata) {
return null;
@@ -57,6 +64,9 @@ export const estimatePriceFromBucket = ({
type: 'combined-time-for-cost-calculation',
}) + timeElapsedOfUnfinished,
memorySizeInMb,
+ architecture,
+ diskSizeInMb,
+ lambdasInvoked,
}).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.ts
index a7b7d3348a..df9377d02e 100644
--- a/packages/lambda/src/functions/helpers/create-post-render-data.ts
+++ b/packages/lambda/src/functions/helpers/create-post-render-data.ts
@@ -2,6 +2,7 @@ import {_Object} from '@aws-sdk/client-s3';
import {estimatePrice} from '../../api/estimate-price';
import {AwsRegion} from '../../pricing/aws-regions';
import {
+ DEFAULT_EPHEMERAL_STORAGE_IN_MB,
lambdaTimingsPrefix,
PostRenderData,
RenderMetadata,
@@ -9,6 +10,7 @@ import {
import {parseLambdaTimingsKey} from '../../shared/parse-lambda-timings-key';
import {calculateChunkTimes} from './calculate-chunk-times';
import {OutputFileMetadata} from './find-output-file-in-bucket';
+import {getCurrentArchitecture} from './get-current-architecture';
import {getFilesToDelete} from './get-files-to-delete';
import {getLambdasInvokedStats} from './get-lambdas-invoked-stats';
import {getRetryStats} from './get-retry-stats';
@@ -55,6 +57,10 @@ export const createPostRenderData = async ({
durationInMiliseconds: times,
memorySizeInMb,
region,
+ architecture: getCurrentArchitecture(),
+ lambdasInvoked: renderMetadata.estimatedTotalLambdaInvokations,
+ // TODO: Cannot yet access disk size via env variable
+ diskSizeInMb: DEFAULT_EPHEMERAL_STORAGE_IN_MB,
});
if (!outputFile) {
diff --git a/packages/lambda/src/functions/helpers/get-current-architecture.ts b/packages/lambda/src/functions/helpers/get-current-architecture.ts
new file mode 100644
index 0000000000..8be3bfe2a9
--- /dev/null
+++ b/packages/lambda/src/functions/helpers/get-current-architecture.ts
@@ -0,0 +1,11 @@
+import {isInsideLambda} from '../../shared/is-in-lambda';
+import {LambdaArchitecture} from '../../shared/validate-architecture';
+
+export const getCurrentArchitecture = (): LambdaArchitecture => {
+ if (!isInsideLambda()) {
+ throw new Error(
+ 'Should not call getCurrentRegion() if not inside a lambda function'
+ );
+ }
+ return process.arch.includes('arm') ? 'arm64' : 'x86_64';
+};
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index 50e1928e71..739440a1f2 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -2,6 +2,7 @@ import {Internals} from 'remotion';
import {AwsRegion} from '../../pricing/aws-regions';
import {
chunkKey,
+ DEFAULT_EPHEMERAL_STORAGE_IN_MB,
encodingProgressKey,
renderMetadataKey,
RenderProgress,
@@ -13,6 +14,7 @@ import {getExpectedOutName} from './expected-out-name';
import {findOutputFileInBucket} from './find-output-file-in-bucket';
import {formatCostsInfo} from './format-costs-info';
import {getCleanupProgress} from './get-cleanup-progress';
+import {getCurrentArchitecture} from './get-current-architecture';
import {getCurrentRegionInFunction} from './get-current-region';
import {getEncodingMetadata} from './get-encoding-metadata';
import {getFinalEncodingStatus} from './get-final-encoding-status';
@@ -147,6 +149,10 @@ export const getProgress = async ({
renderMetadata,
memorySizeInMb,
outputFileMetadata: outputFile,
+ architecture: getCurrentArchitecture(),
+ lambdasInvoked: renderMetadata?.estimatedRenderLambdaInvokations ?? 0,
+ // TODO: Get from outside Lambda fn
+ diskSizeInMb: DEFAULT_EPHEMERAL_STORAGE_IN_MB,
})
);
diff --git a/packages/lambda/src/functions/still.ts b/packages/lambda/src/functions/still.ts
index b4aa087415..e79428991f 100644
--- a/packages/lambda/src/functions/still.ts
+++ b/packages/lambda/src/functions/still.ts
@@ -8,6 +8,7 @@ import {getOrCreateBucket} from '../api/get-or-create-bucket';
import {getLambdaClient} from '../shared/aws-clients';
import {
CURRENT_VERSION,
+ DEFAULT_EPHEMERAL_STORAGE_IN_MB,
LambdaPayload,
LambdaPayloads,
LambdaRoutines,
@@ -21,6 +22,7 @@ import {validatePrivacy} from '../shared/validate-privacy';
import {getExpectedOutName} from './helpers/expected-out-name';
import {formatCostsInfo} from './helpers/format-costs-info';
import {getBrowserInstance} from './helpers/get-browser-instance';
+import {getCurrentArchitecture} from './helpers/get-current-architecture';
import {getCurrentRegionInFunction} from './helpers/get-current-region';
import {getOutputUrlFromMetadata} from './helpers/get-output-url-from-metadata';
import {lambdaWriteFile} from './helpers/io';
@@ -138,6 +140,10 @@ const innerStillHandler = async (
durationInMiliseconds: Date.now() - start + 100,
memorySizeInMb: Number(process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE),
region: getCurrentRegionInFunction(),
+ lambdasInvoked: 1,
+ architecture: getCurrentArchitecture(),
+ // TODO: Get from outside function
+ diskSizeInMb: DEFAULT_EPHEMERAL_STORAGE_IN_MB,
});
return {
diff --git a/packages/lambda/src/test/unit/pricing.test.ts b/packages/lambda/src/test/unit/pricing.test.ts
index c1c0e2050d..9d7b13e555 100644
--- a/packages/lambda/src/test/unit/pricing.test.ts
+++ b/packages/lambda/src/test/unit/pricing.test.ts
@@ -6,6 +6,19 @@ test('Should calculate costs accurately', () => {
region: 'us-east-1',
durationInMiliseconds: 20000,
memorySizeInMb: 2048,
+ architecture: 'x86_64',
+ diskSizeInMb: 512,
+ lambdasInvoked: 1,
+ })
+ ).toEqual(0.00067);
+ expect(
+ estimatePrice({
+ region: 'us-east-1',
+ durationInMiliseconds: 20000,
+ memorySizeInMb: 2048,
+ architecture: 'x86_64',
+ diskSizeInMb: 10240,
+ lambdasInvoked: 1,
})
).toEqual(0.00067);
expect(
@@ -13,6 +26,9 @@ test('Should calculate costs accurately', () => {
region: 'us-east-1',
durationInMiliseconds: 20000,
memorySizeInMb: 1024,
+ architecture: 'x86_64',
+ diskSizeInMb: 512,
+ lambdasInvoked: 1,
})
).toEqual(0.00033);
});