Skip to content

🎥 Make videos programmatically with React

validation

Jonny Burger2bbc09f06d50
diff --git a/packages/docs/docs/lambda/cli/functions.md b/packages/docs/docs/lambda/cli/functions.md
index 532ac7723d..6f02334f46 100644
--- a/packages/docs/docs/lambda/cli/functions.md
+++ b/packages/docs/docs/lambda/cli/functions.md
@@ -64,7 +64,7 @@ Does not create a CloudWatch log group.
### `--retention-period`
-Retention period for the CloudWatch Logs. Default: <DefaultLogRetention /> days.
+Retention period for the CloudWatch Logs in days. Default: <DefaultLogRetention /> days.
### `--quiet`, `-q`
diff --git a/packages/lambda/remotionlambda.zip b/packages/lambda/remotionlambda.zip
index 496722ebca..244fbb7ec9 100644
Binary files a/packages/lambda/remotionlambda.zip and b/packages/lambda/remotionlambda.zip differ
diff --git a/packages/lambda/src/api/deploy-function.ts b/packages/lambda/src/api/deploy-function.ts
index 53453fdbc8..51ede54b8e 100644
--- a/packages/lambda/src/api/deploy-function.ts
+++ b/packages/lambda/src/api/deploy-function.ts
@@ -1,10 +1,15 @@
import {getFunctions} from '../api/get-functions';
import {AwsRegion} from '../pricing/aws-regions';
-import {CURRENT_VERSION, RENDER_FN_PREFIX} from '../shared/constants';
+import {
+ CURRENT_VERSION,
+ DEFAULT_CLOUDWATCH_RETENTION_PERIOD,
+ RENDER_FN_PREFIX,
+} from '../shared/constants';
import {FUNCTION_ZIP} from '../shared/function-zip-path';
import {getAccountId} from '../shared/get-account-id';
import {validateAwsRegion} from '../shared/validate-aws-region';
import {validateMemorySize} from '../shared/validate-memory-size';
+import {validateCloudWatchRetentionPeriod} from '../shared/validate-retention-period';
import {validateTimeout} from '../shared/validate-timeout';
import {createFunction} from './create-function';
@@ -25,7 +30,7 @@ export type DeployFunctionOutput = {
* @description Creates an AWS Lambda function in your account that will be able to render a video in the cloud.
* @link https://v3.remotion.dev/docs/lambda/deployfunction
* @param options.createCloudWatchLogGroup Whether you'd like to create a CloudWatch Log Group to store the logs for this function.
- * @param options.cloudWatchLogRetentionPeriodInDays Optional. The number of days to retain the CloudWatch logs for this function. Default is 14 days.
+ * @param options.cloudWatchLogRetentionPeriodInDays (optional) The number of days to retain the CloudWatch logs for this function. Default is 14 days.
* @param options.region The region you want to deploy your function to.
* @param options.timeoutInSeconds After how many seconds the lambda function should be killed if it does not end itself.
* @param options.memorySizeInMb How much memory should be allocated to the Lambda function.
@@ -37,6 +42,7 @@ export const deployFunction = async (
validateMemorySize(options.memorySizeInMb);
validateTimeout(options.timeoutInSeconds);
validateAwsRegion(options.region);
+ validateCloudWatchRetentionPeriod(options.cloudWatchLogRetentionPeriodInDays);
const fnNameRender = `${RENDER_FN_PREFIX}${CURRENT_VERSION}-${options.memorySizeInMb}mb-${options.timeoutInSeconds}sec`;
const accountId = await getAccountId({region: options.region});
@@ -61,7 +67,9 @@ export const deployFunction = async (
accountId,
memorySizeInMb: options.memorySizeInMb,
timeoutInSeconds: options.timeoutInSeconds,
- retentionInDays: options.cloudWatchLogRetentionPeriodInDays as number,
+ retentionInDays:
+ options.cloudWatchLogRetentionPeriodInDays ??
+ DEFAULT_CLOUDWATCH_RETENTION_PERIOD,
alreadyCreated: Boolean(alreadyDeployed),
});
diff --git a/packages/lambda/src/shared/validate-retention-period.ts b/packages/lambda/src/shared/validate-retention-period.ts
new file mode 100644
index 0000000000..cd07b87458
--- /dev/null
+++ b/packages/lambda/src/shared/validate-retention-period.ts
@@ -0,0 +1,46 @@
+const MIN_RETENTION_PERIOD = 1;
+const MAX_RETENTION_PERIOD = 10 * 365;
+
+export const validateCloudWatchRetentionPeriod = (period: unknown) => {
+ if (period === null || period === undefined) {
+ return;
+ }
+
+ if (typeof period !== 'number') {
+ throw new TypeError(
+ `CloudWatch retention period should be a number, got: ${JSON.stringify(
+ period
+ )}`
+ );
+ }
+
+ if (Number.isNaN(period)) {
+ throw new TypeError(
+ `CloudWatch retention period must be an integer, but is NaN`
+ );
+ }
+
+ if (!Number.isFinite(period)) {
+ throw new TypeError(
+ `CloudWatch retention period must be finite, but is ${period}`
+ );
+ }
+
+ if (period % 1 !== 0) {
+ throw new TypeError(
+ `CloudWatch retention period must be an integer, but is ${period}`
+ );
+ }
+
+ if (period < MIN_RETENTION_PERIOD) {
+ throw new Error(
+ `CloudWatch retention period must be at least ${MIN_RETENTION_PERIOD}, but is ${period}`
+ );
+ }
+
+ if (period > MAX_RETENTION_PERIOD) {
+ throw new Error(
+ `CloudWatch retention period must be at most ${MAX_RETENTION_PERIOD}, but is ${period}`
+ );
+ }
+};
diff --git a/packages/lambda/src/test/cloudwatch-validation.test.ts b/packages/lambda/src/test/cloudwatch-validation.test.ts
new file mode 100644
index 0000000000..0cb6a5bc16
--- /dev/null
+++ b/packages/lambda/src/test/cloudwatch-validation.test.ts
@@ -0,0 +1,15 @@
+import {validateCloudWatchRetentionPeriod} from '../shared/validate-retention-period';
+import {expectToThrow} from './helpers/expect-to-throw';
+
+test('Should be a valid cloudwatch retention period', () => {
+ expect(validateCloudWatchRetentionPeriod(1)).toBe(undefined);
+ expect(validateCloudWatchRetentionPeriod(undefined)).toBe(undefined);
+ expectToThrow(
+ () => validateCloudWatchRetentionPeriod(0),
+ /CloudWatch retention period must be at least 1, but is 0/
+ );
+ expectToThrow(
+ () => validateCloudWatchRetentionPeriod(1000000),
+ /CloudWatch retention period must be at most 3650, but is 1000000/
+ );
+});