Skip to content

πŸŽ₯ Make videos programmatically with React

lfg

Jonny Burgerc25cb6c490a7
diff --git a/packages/docs/docs/deployfunction.md b/packages/docs/docs/deployfunction.md
index 2a5b564be0..a73d82db11 100644
--- a/packages/docs/docs/deployfunction.md
+++ b/packages/docs/docs/deployfunction.md
@@ -21,7 +21,7 @@ const {functionName} = await deployFunction({
layerArn,
region: 'us-east-1',
timeoutInSeconds: 120,
- memorySize: 1024
+ memorySizeInMb: 1024
});
console.log(functionName)
```
@@ -43,9 +43,9 @@ The AWS region which you want to deploy the Lambda function too. It must be the
How long the Lambda function may run before it gets killed. Must be below 900 seconds.
We recommend a timeout of 120 seconds or lower - remember, Remotion Lambda is the fastest if you render with a high concurrency. If your video takes longer to render, the concurrency should be increased rather than the timeout.
-### `memorySize`
+### `memorySizeInMb`
-How many megabytes of RAM the Lambda function should have. By default we recommend a value of 1024MB. You may increase or decrease it depending on how memory-consuming your video is. The minimum allowed number is `512`, the maximum allowed number is `10240`. Since the costs of Remotion Lambda is directly proportional to the amount of RAM, we recommend to keep this amount as low as possiblr.
+How many megabytes of RAM the Lambda function should have. By default we recommend a value of 1024MB. You may increase or decrease it depending on how memory-consuming your video is. The minimum allowed number is `512`, the maximum allowed number is `10240`. Since the costs of Remotion Lambda is directly proportional to the amount of RAM, we recommend to keep this amount as low as possible.
## Return value
diff --git a/packages/docs/docs/lambda-changelog.md b/packages/docs/docs/lambda-changelog.md
index 0d4a4c60ab..7336e599b5 100644
--- a/packages/docs/docs/lambda-changelog.md
+++ b/packages/docs/docs/lambda-changelog.md
@@ -18,6 +18,7 @@ Keep track of changes to the APIs of Remotion Lambda here.
- `getDeployedLambdas()` was renamed to `getFunctions()`
- `getFunctionVersion()` was removed, use `getFunctionInfo()`
+- New function `estimatePrice` is now available.
## July 14th, 2021
diff --git a/packages/docs/docs/lambda-setup.md b/packages/docs/docs/lambda-setup.md
index bc6e285822..5098ae3b32 100644
--- a/packages/docs/docs/lambda-setup.md
+++ b/packages/docs/docs/lambda-setup.md
@@ -4,6 +4,9 @@ title: Setup
slug: /lambda/setup
---
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
## 1. Install `@remotion/lambda`
Check the newest version number in the #lambda Discord channel
@@ -46,10 +49,9 @@ Also update **all the other Remotion packages** to have the same version. Make s
- Copy Access key ID and Secret Access Key
- Add a `.env` file to your project
-```dotenv
+```txt title=".env"
AWS_ACCESS_KEY_ID=xxxxxx
AWS_SECRET_ACCESS_KEY=xxx
-
```
## 5. Add permissions to your user
@@ -65,14 +67,234 @@ AWS_SECRET_ACCESS_KEY=xxx
- Run `npx remotion lambda policies validate`
-## 7. Deploy a lambda
+<hr/>
+
+For the following steps, you may execute them on the CLI, or programmatically using the Node.JS APIs.
+
+## 7. Deploy a function
+
+<Tabs
+defaultValue="cli"
+values={[
+{ label: 'CLI', value: 'cli', },
+{ label: 'Node.JS', value: 'node', },
+]
+}>
+<TabItem value="cli">
+
+Deploy a function by executing the following command:
+
+```bash
+npx remotion lambda functions deploy
+```
+
+</TabItem>
+<TabItem value="node">
+
+First create a Lambda layer - it will contain the necessary binaries such as Google Chrome and FFMPEG. If one already exists, it will be used instead.
+
+We'll assume `us-east-1`, but you can choose [any supported region](/docs/lambda/region-selection).
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {ensureLambdaBinaries, deployFunction} from '@remotion/lambda';
+
+const {layerArn} = await ensureLambdaBinaries('us-east-1')
+```
+
+Now you are ready to deploy the function itself using [`deployFunction()`](/docs/lambda/deployfunction).
-- Run `npx remotion lambda functions deploy`
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {ensureLambdaBinaries, deployFunction} from '@remotion/lambda';
+
+const {layerArn} = await ensureLambdaBinaries('us-east-1')
+
+// ---cut---
+const {functionName} = await deployFunction({
+ layerArn,
+ region: 'us-east-1',
+ timeoutInSeconds: 120,
+ memorySize: 1536
+})
+```
+
+The function name is returned which you'll need for rendering.
+</TabItem>
+</Tabs>
## 8. Deploy a website
-- Run `npx remotion lambda sites create src/index.tsx`
+<Tabs
+defaultValue="cli"
+values={[
+{ label: 'CLI', value: 'cli', },
+{ label: 'Node.JS', value: 'node', },
+]
+}>
+<TabItem value="cli">
+
+Run the following command to deploy your Remotion project to an S3 bucket. Pass as the last argument the entry file of the project - this is the file where [`registerRoot()`](/docs/register-root) is called.
+
+```bash
+npx remotion lambda sites create src/index.tsx
+```
+
+A URL will be printed pointing to the deployed project.
+
+</TabItem>
+<TabItem value="node">
+
+First, you need to create an S3 bucket in your preferred region. If one already exists, it will be used instead:
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import path from 'path';
+import {deployProject, getOrCreateBucket} from '@remotion/lambda';
+
+const {bucketName} = await getOrCreateBucket({
+ region: 'us-east-1'
+})
+```
+
+Next, upload your Remotion project to an S3 bucket. Specify the entry point of your Remotion project, this is the file where [`registerRoot()`](/docs/register-root) is called.
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import path from 'path';
+import {deployProject, getOrCreateBucket} from '@remotion/lambda';
+
+const {bucketName} = await getOrCreateBucket({
+ region: 'us-east-1'
+})
+// ---cut---
+const {url} = await deployProject({
+ bucketName,
+ entryPoint: path.resolve(process.cwd(), 'src/index.tsx'),
+ region: 'us-east-1'
+})
+```
+
+You are now ready to render a video.
+
+</TabItem>
+</Tabs>
## 9. Render a video
-- Run `npx remotion lambda render <serve-url> <composition-name>`
+<Tabs
+defaultValue="cli"
+values={[
+{ label: 'CLI', value: 'cli', },
+{ label: 'Node.JS', value: 'node', },
+]
+}>
+<TabItem value="cli">
+
+Take the URL you received from the previous step and run the following command. Also pass in the ID of the composition you'd like to render.
+
+```bash
+npx remotion lambda render <serve-url> <composition-id>
+```
+
+Progress will be printed until the video finished rendering. Congrats! You rendered your first video using Remotion Lambda πŸš€
+
+</TabItem>
+<TabItem value="node">
+
+You already have the function name from a previous step. But since you only need to deploy a function once, it's useful to retrieve the name of your deployed function programmatically before rendering a video in case your Node.JS program restarts. We can call [`getFunctions()`](/docs/lambda/getfunctions) with the `compatibleOnly` flag to get only functions with a matching version.
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {getFunctions, renderVideoOnLambda, getRenderProgress} from '@remotion/lambda';
+
+const functions = await getFunctions({
+ region: 'us-east-1',
+ compatibleOnly: true
+})
+
+const functionName = functions[0].name
+```
+
+We can now trigger a render using the [`renderVideoOnLambda()`](/docs/rendervideoonlambda) function.
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {getFunctions, renderVideoOnLambda, getRenderProgress} from '@remotion/lambda';
+
+const url = 'string'
+const functions = await getFunctions({
+ region: 'us-east-1',
+ compatibleOnly: true
+})
+
+const functionName = functions[0].name
+// ---cut---
+
+const {renderId, bucketName} = await renderVideoOnLambda({
+ region: 'us-east-1',
+ functionName,
+ serveUrl: url,
+ composition: 'HelloWorld',
+ inputProps: {},
+ codec: 'h264-mkv',
+ imageFormat: 'jpeg',
+ maxRetries: 3
+})
+```
+
+The render will now run and after a while the video will be available in your S3 bucket. You can at any time get the status of the video render by calling [`getRenderProgress()`](/docs/lambda/getrenderprogress).
+
+```ts twoslash
+// @module: ESNext
+// @target: ESNext
+import {getFunctions, renderVideoOnLambda, getRenderProgress} from '@remotion/lambda';
+
+const url = 'string'
+const functions = await getFunctions({
+ region: 'us-east-1',
+ compatibleOnly: true
+})
+
+const functionName = functions[0].name
+
+const {renderId, bucketName} = await renderVideoOnLambda({
+ region: 'us-east-1',
+ functionName,
+ serveUrl: url,
+ composition: 'HelloWorld',
+ inputProps: {},
+ codec: 'h264-mkv',
+ imageFormat: 'jpeg',
+ maxRetries: 3
+})
+// ---cut---
+while (true) {
+ await new Promise(resolve => setTimeout(resolve, 1000))
+ const progress = await getRenderProgress({
+ renderId,
+ bucketName,
+ functionName,
+ region: 'us-east-1'
+ })
+ if (progress.done) {
+ console.log('Render finished!', progress.outputFile)
+ process.exit(0)
+ }
+ if (progress.fatalErrorEncountered) {
+ console.error('Error enountered', progress.errors)
+ process.exit(1)
+ }
+}
+```
+
+This code will poll every second to check the progress of the video and exit the script if the render is done. Congrats! You rendered your first video using Remotion Lambda πŸš€
+
+</TabItem>
+</Tabs>
diff --git a/packages/lambda/src/api/deploy-function.ts b/packages/lambda/src/api/deploy-function.ts
index 984e2e56bf..65b27ac078 100644
--- a/packages/lambda/src/api/deploy-function.ts
+++ b/packages/lambda/src/api/deploy-function.ts
@@ -14,9 +14,9 @@ export const deployFunction = async (options: {
region: AwsRegion;
layerArn: string;
timeoutInSeconds: number;
- memorySize: number;
+ memorySizeInMb: number;
}) => {
- validateMemorySize(options.memorySize);
+ validateMemorySize(options.memorySizeInMb);
validateTimeout(options.timeoutInSeconds);
validateAwsRegion(options.region);
@@ -37,7 +37,7 @@ export const deployFunction = async (options: {
Role: `arn:aws:iam::${accountId[1]}:role/remotion-lambda-role`,
Runtime: 'nodejs14.x',
Description: 'Renders a Remotion video.',
- MemorySize: options.memorySize,
+ MemorySize: options.memorySizeInMb,
Timeout: options.timeoutInSeconds,
Layers: [options.layerArn],
})
diff --git a/packages/lambda/src/api/get-functions.ts b/packages/lambda/src/api/get-functions.ts
index 0ffab1eaf7..d2d333c168 100644
--- a/packages/lambda/src/api/get-functions.ts
+++ b/packages/lambda/src/api/get-functions.ts
@@ -1,10 +1,14 @@
import {ListFunctionsCommand} from '@aws-sdk/client-lambda';
import {AwsRegion} from '../pricing/aws-regions';
import {getLambdaClient} from '../shared/aws-clients';
-import {RENDER_FN_PREFIX} from '../shared/constants';
+import {CURRENT_VERSION, RENDER_FN_PREFIX} from '../shared/constants';
import {getFunctionVersion} from '../shared/get-function-version';
-export const getFunctions = async (options: {region: AwsRegion}) => {
+// TODO: Compatible only
+export const getFunctions = async (options: {
+ region: AwsRegion;
+ compatibleOnly: boolean;
+}) => {
const lambdas = await getLambdaClient(options.region).send(
new ListFunctionsCommand({})
);
@@ -22,7 +26,7 @@ export const getFunctions = async (options: {region: AwsRegion}) => {
})
);
- return remotionLambdas.map((lambda, i) => {
+ const list = remotionLambdas.map((lambda, i) => {
return {
name: lambda.FunctionName as string,
version: configs[i],
@@ -30,4 +34,15 @@ export const getFunctions = async (options: {region: AwsRegion}) => {
timeout: lambda.Timeout as number,
};
});
+ if (!options.compatibleOnly) {
+ return list;
+ }
+
+ return list.filter((l) => {
+ if (!options.compatibleOnly) {
+ return true;
+ }
+
+ return l.version === CURRENT_VERSION;
+ });
};
diff --git a/packages/lambda/src/api/get-or-create-bucket.ts b/packages/lambda/src/api/get-or-create-bucket.ts
index 9f588a57c5..955b296252 100644
--- a/packages/lambda/src/api/get-or-create-bucket.ts
+++ b/packages/lambda/src/api/get-or-create-bucket.ts
@@ -5,7 +5,17 @@ import {REMOTION_BUCKET_PREFIX} from '../shared/constants';
import {randomHash} from '../shared/random-hash';
import {getRemotionS3Buckets} from './get-buckets';
-export const getOrCreateBucket = async (options: {region: AwsRegion}) => {
+type GetOrCreateBucketInput = {
+ region: AwsRegion;
+};
+
+type GetOrCreateBucketResult = {
+ bucketName: string;
+};
+
+export const getOrCreateBucket = async (
+ options: GetOrCreateBucketInput
+): Promise<GetOrCreateBucketResult> => {
const {remotionBuckets} = await getRemotionS3Buckets(options.region);
if (remotionBuckets.length > 1) {
throw new Error(
@@ -18,7 +28,7 @@ export const getOrCreateBucket = async (options: {region: AwsRegion}) => {
}
if (remotionBuckets.length === 1) {
- return remotionBuckets[0].Name as string;
+ return {bucketName: remotionBuckets[0].Name as string};
}
const bucketName = REMOTION_BUCKET_PREFIX + randomHash();
@@ -30,5 +40,5 @@ export const getOrCreateBucket = async (options: {region: AwsRegion}) => {
})
);
- return bucketName;
+ return {bucketName};
};
diff --git a/packages/lambda/src/cli/commands/functions/deploy.ts b/packages/lambda/src/cli/commands/functions/deploy.ts
index 65ddae6a2f..ce943bf52e 100644
--- a/packages/lambda/src/cli/commands/functions/deploy.ts
+++ b/packages/lambda/src/cli/commands/functions/deploy.ts
@@ -35,7 +35,7 @@ export const functionsDeploySubcommand = async () => {
region,
timeoutInSeconds,
layerArn,
- memorySize,
+ memorySizeInMb: memorySize,
});
output.update(`Deployed to ${functionName}\n`);
};
diff --git a/packages/lambda/src/cli/commands/functions/ls.ts b/packages/lambda/src/cli/commands/functions/ls.ts
index 60d1f0b3f0..e0b0a3015b 100644
--- a/packages/lambda/src/cli/commands/functions/ls.ts
+++ b/packages/lambda/src/cli/commands/functions/ls.ts
@@ -21,6 +21,7 @@ export const functionsLsCommand = async () => {
const functions = await getFunctions({
region,
+ compatibleOnly: false,
});
if (quiet) {
diff --git a/packages/lambda/src/cli/commands/sites/create.ts b/packages/lambda/src/cli/commands/sites/create.ts
index 0db3f61d4d..7fea4a5623 100644
--- a/packages/lambda/src/cli/commands/sites/create.ts
+++ b/packages/lambda/src/cli/commands/sites/create.ts
@@ -77,7 +77,7 @@ export const sitesCreateSubcommand = async (args: string[]) => {
);
};
- const bucketName = await getOrCreateBucket({region: getAwsRegion()});
+ const {bucketName} = await getOrCreateBucket({region: getAwsRegion()});
multiProgress.bucketProgress = {
bucketCreated: true,
doneIn: null,
diff --git a/packages/lambda/src/cli/render.ts b/packages/lambda/src/cli/render.ts
index c8339676fb..22b277d1e6 100644
--- a/packages/lambda/src/cli/render.ts
+++ b/packages/lambda/src/cli/render.ts
@@ -36,7 +36,10 @@ export const renderCommand = async (args: string[]) => {
// TODO: Further validate serveUrl
- const remotionLambdas = await getFunctions({region: getAwsRegion()});
+ const remotionLambdas = await getFunctions({
+ region: getAwsRegion(),
+ compatibleOnly: false,
+ });
const lambdasWithMatchingVersion = remotionLambdas.filter(
(l) => l.version === CURRENT_VERSION
);
diff --git a/packages/lambda/src/functions/start.ts b/packages/lambda/src/functions/start.ts
index 206495081e..7ca2c2f457 100644
--- a/packages/lambda/src/functions/start.ts
+++ b/packages/lambda/src/functions/start.ts
@@ -10,7 +10,7 @@ export const startHandler = async (params: LambdaPayload) => {
throw new TypeError('Expected type start');
}
- const bucketName = await getOrCreateBucket({
+ const {bucketName} = await getOrCreateBucket({
region: getCurrentRegionInFunction(),
});
const renderId = randomHash();
diff --git a/packages/lambda/src/run.ts b/packages/lambda/src/run.ts
index 7a4c9fc266..532f42f0e1 100644
--- a/packages/lambda/src/run.ts
+++ b/packages/lambda/src/run.ts
@@ -30,7 +30,7 @@ const getFnName = async (options: {
bucketUrl: string;
compositionName: string;
}> => {
- const bucketName = await getOrCreateBucket({region: getAwsRegion()});
+ const {bucketName} = await getOrCreateBucket({region: getAwsRegion()});
if (DEPLOY) {
await cleanupLambdas({lambdaClient: getLambdaClient(options.region)});
// await cleanUpBuckets({s3client: s3Client});
@@ -40,7 +40,7 @@ const getFnName = async (options: {
region: getAwsRegion(),
timeoutInSeconds: 120,
layerArn,
- memorySize: 2048,
+ memorySizeInMb: 2048,
});
const {url} = await deployProject({