Skip to content

πŸŽ₯ Make videos programmatically with React

implement documentation for serverless setup and make references to lambda package

Alex Fernandez6a25c332a830
diff --git a/packages/docs/docs/lambda/lambdarolepermissions.md b/packages/docs/docs/lambda/lambdarolepermissions.md
new file mode 100644
index 0000000000..c80e7f7ff1
--- /dev/null
+++ b/packages/docs/docs/lambda/lambdarolepermissions.md
@@ -0,0 +1,102 @@
+---
+image: /generated/articles-docs-lambda-lambda-role-permissions.png
+id: lambdarolepermissions
+title: Lambda permissions
+slug: /lambda/lambda-role-permissions
+crumb: "Lambda"
+---
+
+import {UserPolicy} from '../../components/lambda/user-permissions.tsx';
+
+## Context
+
+As documented on [`permission`](/docs/lambda/permissions) we create a remotion user and assign `policies`, this policies gives permissions to the `lambda` to render video from [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda). This approach is sometimes not acceptable as AWS client id and secret associated with the user are considered long term credentials and prone for inadvertent leakage.
+
+There also some that has requirements to execute the [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda) on services such as [`Lambda`](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html), [`EC2`](https://aws.amazon.com/ec2/) and other [`compute services`](https://aws.amazon.com/products/compute/) which using the long term credentials is not an option. AWS has the concept of [IAM Roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles), when a role is assigned to a AWS service it assumes any elevated privileges from the attached AWS `policies`, it is empowered to execute activities such as putting a file to S3 bucket. The role is given short lived AWS credentials such as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN`, to generate the video. With this approach security is enhanced as we don't have any long term credential lurking around and we don't need to keep track of it's rotation.
+
+The steps below provides authorization to the lambda function to execute `renderMediaOnLambda()` without permission issues.
+
+#### Prerequisites
+1. This assumes that you have deployed a `lambda` function running in AWS. An example is created in [`here`](https://github.com/remotion-dev/remotion/tree/main/packages/example-lambda) using [`CDK`](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-typescript.html) to give you an idea how to call [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda) inside another a lambda function, the function is triggered by [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html). This assumes that you have knowledge on using [`CDK`](https://docs.aws.amazon.com/cdk/v2/guide/work-with-cdk-typescript.html), a [write up](/docs/lambda/serverless-lambdas-setup) for this is also created for anyone to follow.
+2. Lambda execution role assigned to the lambda
+3. User Policy [user permissions](/docs/lambda/permissions#user-permissions).
+
+### Setup
+
+#### 1. Create role policy
+- Go to AWS account IAM Policies section
+- Click on "Create policy"
+- Click on JSON
+- Copy the JSON Policy template below
+ <details>
+ <summary>Show full role permissions JSON file for latest Remotion Lambda version
+ </summary>
+ <UserPolicy />
+ </details>
+- Click next. On the tags page, you don't need to fill in anything. Click next again.
+- Give the policy exactly the name `remotion-executionrole-policy`. The other fields can be left as they are.
+
+
+#### 2. Assign the policy to the Lamdba execution role
+- From your lambda function assign the policy to the lambda role execution role. To do so, go to the [AWS console](https://console.aws.amazon.com/console/home) ➞ [Lambda(change to your function region)](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/discover) ➞ [Functions](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions) ➞ Your lambda function ➞ Configuration tab ➞ Permissions tab ➞ Click the role under `Execution Role` ➞ When redirected, click Permissions tab ➞ Click Add permissions ➞ Attach policy ➞ Find the policy, ie `remotion-executionrole-policy` ➞ Select the policy ➞ Then click the attach policies button.
+
+With the assignment of the `policy` to the lambda execution role, it is now empowered to execute the [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda), without permission issues. In the background, when the lambda function is executed it is provided with environment variables such as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN` that has temporary permission to AWS resources that `renderMediaOnLambda()` requires to render the video. The elevated powers came from the policy statements in `remotion-executionrole-policy`.
+
+
+:::info
+This activity can be also applied to other AWS compute services such as `EC2`, `Fargate` etc..
+:::
+
+## Optional
+If you want to move the video to another s3 bucket when the video is rendered, the `lambda` also needs a permission to do so into the new bucket.
+
+#### Example
+```ts twoslash
+// @module: esnext
+// @target: es2017
+// ---cut---
+
+import { renderMediaOnLambda } from "@remotion/lambda/client";
+
+const { bucketName, renderId } = await renderMediaOnLambda({
+ region: "us-east-1",
+ functionName: "remotion-render-bds9aab",
+ composition: "MyVideo",
+ serveUrl:
+ "https://remotionlambda-qg35eyp1s1.s3.eu-central-1.amazonaws.com/sites/bf2jrbfkw",
+ codec: "h264",
+ outName: "transfer-to-this-bucket-after-render/the-filename.mp4",
+});
+```
+
+From the example above, the `renderMediaOnLambda()` is configured to output the video to `transfer-to-this-bucket-after-render` bucket. The steps to allows the lambda to move the file to the other bucket, which is similar to [this](/docs/lambda/trigger-lambda-from-aws#setup).
+
+### Steps
+- From your lambda function assign the policy to the lambda execution role. To do so, go to the [AWS console](https://console.aws.amazon.com/console/home) ➞ [Lambda(change to your function region)](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/discover) ➞ [Functions](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions) ➞ Your lambda function ➞ Configuration tab ➞ Permissions tab ➞ Click the role under `Execution role` ➞ When redirected, click Permissions tab ➞ click `add permissions` ➞ Click create inline policy.
+- Click JSON tab
+- Add policy statement similar below defining the bucket the lambda needs to transfer the rendered file.
+
+```json
+ {
+ "Version": "2012-10-17",
+ "Statement": [
+ {
+ "Action": [
+ "s3:PutObject"
+ ],
+ "Resource": [
+ "arn:aws:s3:::{buckername}",
+ "arn:aws:s3:::{buckername}/*"
+ ],
+ "Effect": "Allow"
+ }
+ ]
+}
+```
+- Click review policy
+- Then click save changes
+
+The lambda function can now move the rendered video to the other bucket when render process is completed.
+
+## See also
+- [Serverless lambda setup](/docs/lambda/serverlesslambdasetup)
\ No newline at end of file
diff --git a/packages/docs/docs/lambda/serverlesslambdasetup.md b/packages/docs/docs/lambda/serverlesslambdasetup.md
new file mode 100644
index 0000000000..0c8fabac52
--- /dev/null
+++ b/packages/docs/docs/lambda/serverlesslambdasetup.md
@@ -0,0 +1,203 @@
+---
+image: /generated/articles-docs-lambda-serverlesslambdasetup.png
+id: serverlesslambdasetup
+title: Serverless lambda setup
+slug: /lambda/serverless-lambdas-setup
+crumb: "Lambda"
+---
+import Tabs from '@theme/Tabs';
+import TabItem from '@theme/TabItem';
+
+This is a follow up write up for a serverless function from [lamdbda permissions](/docs/lambda/lambda-role-permissions).
+
+### Prequisites
+- Make sure that you're local AWS profile is able to deploy to AWS.
+
+### Setup
+
+#### 1. Clone or download the project
+Project is located inside the [`remotion project`](https://github.com/remotion-dev/remotion/tree/main/packages/example-lambda)
+
+#### 2. Install dependencies
+
+<Tabs
+defaultValue="npm"
+values={[
+{ label: 'npm', value: 'npm', },
+{ label: 'yarn', value: 'yarn', },
+{ label: 'pnpm', value: 'pnpm', },
+]
+}>
+<TabItem value="npm">
+
+```bash
+npm i
+```
+
+ </TabItem>
+
+ <TabItem value="pnpm">
+
+```bash
+pnpm i
+```
+
+ </TabItem>
+ <TabItem value="yarn">
+
+```bash
+yarn install
+```
+
+ </TabItem>
+
+</Tabs>
+
+#### 2. Create the CDK Stack
+This process will deploy the lambda function and any resources inside the stack.
+
+```bash
+npx aws-cdk deploy \
+ --outputs-file ./cdk-outputs.json
+```
+
+#### 3. After deployment
+
+```bash
+npx aws-cdk deploy \
+ --outputs-file ./cdk-outputs.json
+```
+Deployment progress
+
+```bash
+Bundling asset cdk-stack/render-function/Code/Stage...
+
+ cdk.out/bundling-temp-5e88d0b45626d59e8e8ddce3b05a886b0e1b381df6e5bbbea1dc2727080641a8/index.js 6.3mb ⚠️
+
+⚑ Done in 295ms
+
+✨ Synthesis time: 4.29s
+
+cdk-stack: building assets...
+
+[0%] start: Building 87d5e793cbd198c73c05023515153b142eb2f559e7150579cd2db53362c19b6e:676583371711-us-east-1
+[0%] start: Building 779e5babb0ddf0d17c0faebbe5596b03bcf13785f0b20c4cd0fe0c5e616d5593:676583371711-us-east-1
+[50%] success: Built 87d5e793cbd198c73c05023515153b142eb2f559e7150579cd2db53362c19b6e:676583371711-us-east-1
+[100%] success: Built 779e5babb0ddf0d17c0faebbe5596b03bcf13785f0b20c4cd0fe0c5e616d5593:676583371711-us-east-1
+
+cdk-stack: assets built
+
+cdk-stack: deploying... [1/1]
+[0%] start: Publishing 87d5e793cbd198c73c05023515153b142eb2f559e7150579cd2db53362c19b6e:676583371711-us-east-1
+[0%] start: Publishing 779e5babb0ddf0d17c0faebbe5596b03bcf13785f0b20c4cd0fe0c5e616d5593:676583371711-us-east-1
+[50%] success: Published 779e5babb0ddf0d17c0faebbe5596b03bcf13785f0b20c4cd0fe0c5e616d5593:676583371711-us-east-1
+[100%] success: Published 87d5e793cbd198c73c05023515153b142eb2f559e7150579cd2db53362c19b6e:676583371711-us-east-1
+
+ βœ… cdk-stack (no changes)
+
+✨ Deployment time: 1.39s
+```
+
+### Output
+
+```bash
+Outputs:
+cdk-stack.apiUrl = https://du7jfr.execute-api.us-east-1.amazonaws.com/
+cdk-stack.region = us-east-1
+cdk-stack.userPoolClientId = 4l5tsda2iu8lqugl73m8hgeb83
+cdk-stack.userPoolId = us-east-1_bVwFsBUGO
+Stack ARN:
+arn:aws:cloudformation:us-east-1:676583371711:stack/cdk-stack/faf43800-9878-11ed-a070-0aacc64c8662
+
+```
+
+The output contains the API Gateway base url, region and cognito client id and user pool id, which are used for authentication.
+
+#### 4. Cleanup
+
+This will delete the function, in case it's not needed anymore.
+```bash
+npx aws-cdk destroy
+```
+
+### Lamdbda role
+The CDK creates an IAM role named `remotionLambdaServerlessRole` which needs the remotion role [setup](/docs/lambda/lambdarolepermissions).
+
+
+### Test your endpoint
+The API is secured by Cognito which requires an authorization token.
+
+In order to test the API need to do the steps below, just in case you still don't have frontend.
+
+1. Create a Cognito User
+
+```bash
+aws cognito-idp sign-up \
+ --client-id YOUR_USER_POOL_CLIENT_ID \
+ --username "[email protected]" \
+ --password "compLicat3d123"
+```
+
+2. Confirm the user, so they can sign in
+```bash
+aws cognito-idp admin-confirm-sign-up \
+ --user-pool-id YOUR_USER_POOL_ID \
+ --username "[email protected]"
+```
+
+
+3. Log the user in to retrieve identity JWT token
+```bash
+aws cognito-idp initiate-auth \
+ --auth-flow USER_PASSWORD_AUTH \
+ --auth-parameters \
+ USERNAME="[email protected]",PASSWORD="compLicat3d123" \
+ --client-id YOUR_USER_POOL_CLIENT_ID
+
+```
+YOUR_USER_POOL_CLIENT_ID and YOUR_USER_POOL_ID are part of CDK [output](/docs/lambda/serverless-lambdas-setup#output).
+
+Output
+```bash
+{
+ "ChallengeParameters": {},
+ "AuthenticationResult": {
+ "AccessToken": "eyJraWQiOiJGcUJ....",
+ "ExpiresIn": 3600,
+ "TokenType": "Bearer",
+ "RefreshToken": "eyJjdHkiOiJKV1QiLCJlbm...",
+ "IdToken": "eyJraWQiOiJCcjY3Rk5WdzRpYVVYVlpNdF..."
+ }
+}
+```
+The API will give you a verbose response but will only use the `IdToken`.
+
+4. Use the token to invoke request to the endpoint using curl.
+
+ ##### Request
+ ```bash
+ curl --location --request POST 'https://du7jfr6.execute-api.us-east-1.amazonaws.com/render' \
+ --header 'Authorization: Bearer eyJraWQiOiJGcUJFV1B1cHhxM0NXRko0RVN2..........'
+ ```
+ ##### Response
+ ```bash
+ {"message":"SUCCESS","bucketName":"remotionlambda-apsoutheast2-5essis84y1","renderId":"1pwhfhh11z"}
+ ```
+
+
+That's it, you now have an API to invoke rendering of video.
+
+
+:::warning
+Don't allow the lambda function to be called by an `unauthenticated` user.
+:::warn
+
+### Next Steps
+- Customize the lambda [`function`](https://github.com/remotion-dev/remotion/tree/main/packages/example-lambda/src/render-function/index.ts) so that the rendered video will be moved to another directory.
+- Try assigning the remotion [role](/docs/lambda/lambda-role-permissions#1--create-role-policy) via CDK [`code`](https://github.com/remotion-dev/remotion/tree/main/packages/example-lambda/lib/remotion-cdk-starter-stack.ts).
+- Add request parameters to the lambda function as input parameters for[`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda).
+
+## See also
+- [Lambda permissions](/docs/lambda/lambdarolepermissions)
+- [Permissions](/docs/lambda/permissions)
+- [Some code are borrowed from bobbyhadz.com](https://bobbyhadz.com/blog/aws-cdk-api-authorizer)
\ No newline at end of file
diff --git a/packages/docs/docs/lambda/triggerlambdafromaws.md b/packages/docs/docs/lambda/triggerlambdafromaws.md
deleted file mode 100644
index a38f3a17ca..0000000000
--- a/packages/docs/docs/lambda/triggerlambdafromaws.md
+++ /dev/null
@@ -1,101 +0,0 @@
----
-image: /generated/articles-docs-lambda-triggerlambdafromaws.png
-id: triggerlambdafromaws
-title: Remotion and AWS Compute Services
-slug: /lambda/trigger-lambda-from-aws
-crumb: "Lambda"
----
-
-import {UserPolicy} from '../../components/lambda/user-permissions.tsx';
-
-## Context
-
-As specified on [`permission()`](/docs/lambda/permissions), we create a remotion user to assume the permissions to render a video using [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda). This approach is sometimes not acceptable for other companies as the AWS client id and secret associated with the user are long term credentials and prone to be leaked.
-
-Sometimes companies would need to execute the [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda) on services such as [`Lambda`](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html), [`EC2`](https://aws.amazon.com/ec2/) and other [`compute services`](https://aws.amazon.com/products/compute/). Using the long term credential provides risks to be shared inadvertently by residing inside the artifact. The issue of credential sharing can be solve by using [`IAM Roles`](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html), this role can be assumed by lambda to execute activities to AWS resources. The role is given short lived AWS credentials ie. client id, secret and token, to perform operation it is required to do ie. render a video.
-
-#### Allowing lambda to execute `renderMediaOnLambda`
-
-#### Prerequisites
-1. This assumes that you have deployed a `lambda` function running in AWS. Example is created in here using typesript. This function can be triggered by [API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) or [SQS](https://aws.amazon.com/sqs/).
-2. Lambda Execution Role assiigned to the lambda
-3. User Policy [User permissions](/docs/lambda/permissions#user-permissions).
-
-### Setup
-
-#### 1. Create role policy
-- Go to AWS account IAM Policies section
-- Click on "Create policy"
-- Click on JSON
-- Copy the JSON Policy template below
-
-<details>
-<summary>Show full role permissions JSON file for latest Remotion Lambda version
-</summary>
-<UserPolicy />
-</details>
-- Click next. On the tags page, you don't need to fill in anything. Click next again.
-- Give the policy exactly the name `remotion-executionrole-policy`. The other fields can be left as they are.
-
-
-#### 2. Assign the policy to the Lamdba execution role
-- From your lambda function assign the policy to the lambda role execution role. To do so, go to the [AWS console](https://console.aws.amazon.com/console/home) ➞ [Lambda(change to your function region)](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/discover) ➞ [Functions](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions) ➞ Your lambda function ➞ Configuration tab ➞ Permissions tab ➞ Click the role under `Execution Role` ➞ When redirected, click Permissions tab ➞ Click Add permissions ➞ Attach policy ➞ Find the policy, ie `remotion-executionrole-policy` ➞ Select the policy ➞ Then click the attach policies.
-
-With the assignment of the `policy` to the lambda role, it is now empowered to execute the [`renderMediaOnLambda()`](/docs/lambda/rendermediaonlambda), as it assumed permission required to do so from policy statements. In the background, when the lambda function is executed it is provided with environment variables such as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY` and `AWS_SESSION_TOKEN` that has temporary permission to AWS resources that `renderMediaOnLambda` requires to render the video.
-
-
-:::info
-This activity can be also applied to other AWS compute services such as `EC2`.
-:::
-
-#### Optional
-If you want to move the video to another s3 bucket when the video is rendered, the `lambda` also needs a permission to do so into the new bucket.
-
-#### Example
-```ts twoslash
-// @module: esnext
-// @target: es2017
-// ---cut---
-
-import { renderMediaOnLambda } from "@remotion/lambda/client";
-
-const { bucketName, renderId } = await renderMediaOnLambda({
- region: "us-east-1",
- functionName: "remotion-render-bds9aab",
- composition: "MyVideo",
- serveUrl:
- "https://remotionlambda-qg35eyp1s1.s3.eu-central-1.amazonaws.com/sites/bf2jrbfkw",
- codec: "h264",
- outName: "transfer-to-this-bucket-after-render",
-});
-```
-
-From the example above, the `renderMediaOnLambda` is configured to output the video to `transfer-to-this-bucket-after-render`. The steps to allow the lambda to access the bucket is similar to [this](/docs/lambda/trigger-lambda-from-aws#setup).
-
-### Steps
-- From your lambda function assign the policy to the lambda role execution role. To do so, go to the [AWS console](https://console.aws.amazon.com/console/home) ➞ [Lambda(change to your function region)](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/discover) ➞ [Functions](https://us-east-1.console.aws.amazon.com/lambda/home?region=us-east-1#/functions) ➞ Your lambda function ➞ Configuration tab ➞ Permissions tab ➞ Click the role under `Execution Role` ➞ When redirected, click Permissions tab ➞ Click Add permissions ➞ Click create inline policy.
-- Click JSON tab
-- Add policy statement similar below defining the bucket the lambda needs to transfer the rendered file.
-
-```json
- {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Action": "s3:*",
- "Resource": [
- "arn:aws:s3:::{buckername}",
- "arn:aws:s3:::{buckername}/*"
- ],
- "Effect": "Allow"
- }
- ]
-}
-```
-- Click review policy
-- Then click save changes
-
-
-## See also
-
-[Permissions](/docs/lambda/permissions)
\ No newline at end of file
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 707876d0ed..4f6012debf 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -395,7 +395,6 @@ module.exports = {
"lambda/custom-destination",
"lambda/checklist",
"lambda/webhooks",
- "lambda/triggerlambdafromaws",
{
type: "category",
label: "Troubleshooting",
@@ -407,6 +406,8 @@ module.exports = {
"lambda/troubleshooting/bucket-disallows-acl",
],
},
+ "lambda/lambdarolepermissions",
+ "lambda/serverlesslambdasetup",
"lambda/optimizing-cost",
"lambda/optimizing-speed",
"lambda/limits",
diff --git a/packages/example-lambda/lib/remotion-cdk-starter-stack.ts b/packages/example-lambda/lib/remotion-cdk-starter-stack.ts
index 590326ef21..d476db0e40 100644
--- a/packages/example-lambda/lib/remotion-cdk-starter-stack.ts
+++ b/packages/example-lambda/lib/remotion-cdk-starter-stack.ts
@@ -62,7 +62,7 @@ export class RemotionLambdaStarterStack extends cdk.Stack {
}
);
- // πŸ‘‡ create render function
+ // πŸ‘‡ create the render function
const remotionRenderFunction = new NodejsFunction(this, "render-function", {
runtime: lambda.Runtime.NODEJS_16_X,
handler: "main",
@@ -95,6 +95,7 @@ export class RemotionLambdaStarterStack extends cdk.Stack {
authorizer,
});
+ // πŸ‘‡ Output
new cdk.CfnOutput(this, "region", { value: cdk.Stack.of(this).region });
new cdk.CfnOutput(this, "userPoolId", { value: userPool.userPoolId });
new cdk.CfnOutput(this, "userPoolClientId", {