使用 Lambda 与 SQS
这个过程存在一个缺陷:队列会在触发渲染时立即继续,而不是在渲染完成时继续。这意味着渲染不会按顺序执行。我们正在研究改进的 Lambda 排队策略版本,同时请注意这种行为。
本指南将向您展示如何使用 Amazon SQS(简单队列服务)和 Remotion 的 renderMediaOnLambda()
API。
队列用于暂停请求,以便底层资源能够满足需求。由于 AWS Lambda 受到并发限制的影响,您可以使用 SQS 在后台排队渲染,并通过发送电子邮件或使用其他通知方式通知用户渲染过程何时完成。
为了补充本指南,已创建了两个项目:
- remotion-app 包含 Remotion 组合和用于在 AWS 中部署和删除 Remotion Lambda 基础设施的实用脚本。请注意,这与Serverless Framework 指南中的应用程序相同 。
- apigw-sqs-app(API Gateway SQS)包含一个 CDK 项目,部署了两个 Lambda 函数。enqueue-function 函数在调用时将用户发送的 JSON 数据排入队列;这可以是 Remotion Lambda 函数的输入有效负载。render-lambda-function 函数监听来自 SQS 的排队消息,处理并渲染视频。
enqueue-function
被配置为通过API Gateway调用,并由Cognito进行保护。API Gateway和Cognito的设置是由CDK Stack在执行cdk deploy
时自动创建的。
本指南假定您具有使用CDK与TypeScript和AWS SQS知识的知识。AWS云开发工具包(CDK)已被选择用于提供基础设施,因为它是来自AWS的官方库。
remotion-app
- 请按照remotion-app指南中的相同设置说明进行操作,因为我们将重复使用该应用程序。
apigw-sqs-app
在以下步骤中,Lambda函数enqueue-function
和render-lambda-function
将部署到您的AWS帐户。本指南旨在在您的本地计算机上执行。
该项目将创建CDK堆栈定义的所有资源,包括为项目的身份验证和授权系统设置Cognito,上传Lambda代码,生成并关联IAM角色到您的AWS帐户。
先决条件
- 在您的本地计算机上具有AWS部署配置文件,以配置AWS部署配置文件。
- 创建名为
remotion-executionrole-policy
的AWS策略,该策略是从此指南创建的。 - AWS CDK应在您的本地计算机上全局安装。如果尚未完成,请按照 此处的“安装AWS CDK”部分的说明进行操作。
设置
1. 克隆或下载项目
该项目可以在 remotion-serverless 项目
找到。如果在上一步没有完成,请使用以下命令克隆项目:
bash
git clone https://github.com/alexfernandez803/remotion-serverless
bash
git clone https://github.com/alexfernandez803/remotion-serverless
2. 进入 remotion-serverless
并切换到 apigw-sqs-app
目录
bash
cd remotion-serverless && cd apigw-sqs-app
bash
cd remotion-serverless && cd apigw-sqs-app
3. 安装依赖项
- npm
- yarn
- pnpm
bash
npm i
bash
npm i
bash
pnpm i
bash
pnpm i
bash
yarn
bash
yarn
4. 创建 Remotion 策略
- 如果尚未创建
remotion-executionrole-policy
,请按照此 指南 进行设置。
remotion-executionrole-policy
是从 这里 引用的。CDK
堆栈为 render-lambda-function
创建了一个名为 remotion-executionrole-policy
的角色。
ts
// 👇 Create a role with custom nameconst renderFunctionLambdaRole = new Role(this, 'remotionSQSLambdaRole', {roleName: 'remotionSQSLambdaRole',assumedBy: new ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),ManagedPolicy.fromManagedPolicyName(this,'remotion-executionrole-policy','remotion-executionrole-policy',),],});
ts
// 👇 Create a role with custom nameconst renderFunctionLambdaRole = new Role(this, 'remotionSQSLambdaRole', {roleName: 'remotionSQSLambdaRole',assumedBy: new ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),ManagedPolicy.fromManagedPolicyName(this,'remotion-executionrole-policy','remotion-executionrole-policy',),],});
这将在 AWS 中创建一个名为 remotionSQSLambdaRole
的角色,并附加了 2 个策略:
service-role/AWSLambdaBasicExecutionRole
授予 Lambda 函数与 CloudWatch 交互和记录信息的权限。此策略是 AWS 策略库的一部分。remotion-executionrole-policy
策略授予 Lambda 函数与 Remotion Lambda 需要访问的 AWS 服务进行交互以渲染视频的权限。此策略与此 指南 中的策略完全相同。
5. 可选 - 合成
从 apigw-sqs-app
目录中,执行 cdk synthesize
命令。
bash
cdk synthesize
bash
cdk synthesize
这个命令将展示 CDK 将要在您的 AWS 账户中执行的 CloudFormation 模板。
6. 部署 apigw-sqs-app 项目
从 apigw-sqs-app
目录中,执行 cdk deploy
命令。
bash
cdk deploy
bash
cdk deploy
这将协调 CDK 栈中定义的资源的生成,这些资源是后台中的 CloudFormation 模板。
部署输出bash
Bundling asset apigw-sqs-app-stack/enqueue-function/Code/Stage...cdk.out/bundling-temp-a813aece2454684086de775f918faac45b1b77c67fff24ec6aad4bff8c978ebe/index.js 881.5kb⚡ Done in 72msBundling asset apigw-sqs-app-stack/render-function/Code/Stage...cdk.out/bundling-temp-e7d973ee34691a8e6a2ceda969fbf59380866bb486be333238b7e554907f7b95/index.js 2.6kb⚡ Done in 2msadded 279 packages, and audited 280 packages in 2s21 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities✨ Synthesis time: 7.97sapigw-sqs-app-stack: building assets...[0%] start: Building 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[0%] start: Building 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[0%] start: Building cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2[33%] success: Built 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[66%] success: Built 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[100%] success: Built cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2apigw-sqs-app-stack: assets builtThis deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).Please confirm you intend to make the following modifications:IAM Statement Changes┌───┬─────────────────────────────┬────────┬─────────────────────────────┬─────────────────────────────┬────────────────────────────────┐│ │ Resource │ Effect │ Action │ Principal │ Condition │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${api-integration-role.Arn} │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.co │ ││ │ │ │ │ m │ │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${enqueue-function.Arn} │ Allow │ lambda:InvokeFunction │ Service:apigateway.amazonaw │ "ArnLike": { ││ │ │ │ │ s.com │ "AWS:SourceArn": "arn:${AWS: ││ │ │ │ │ │ :Partition}:execute-api:ap-sou ││ │ │ │ │ │ theast-2:XXXXXXXXXX:${apiC85 ││ │ │ │ │ │ 50315}/*/*/enqueue" ││ │ │ │ │ │ } ││ + │ ${enqueue-function.Arn} │ Allow │ lambda:InvokeFunction │ Service:apigateway.amazonaw │ "ArnLike": { ││ │ │ │ │ s.com │ "AWS:SourceArn": "arn:${AWS: ││ │ │ │ │ │ :Partition}:execute-api:ap-sou ││ │ │ │ │ │ theast-2:XXXXXXXXXX:${apiC85 ││ │ │ │ │ │ 50315}/*/*/enqueue" ││ │ │ │ │ │ } │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${queue.Arn} │ Allow │ sqs:ChangeMessageVisibility │ AWS:${remotionSQSLambdaRole │ ││ │ │ │ sqs:DeleteMessage │ } │ ││ │ │ │ sqs:GetQueueAttributes │ │ ││ │ │ │ sqs:GetQueueUrl │ │ ││ │ │ │ sqs:ReceiveMessage │ │ ││ + │ ${queue.Arn} │ Allow │ sqs:GetQueueAttributes │ AWS:${api-integration-role} │ ││ │ │ │ sqs:GetQueueUrl │ │ ││ │ │ │ sqs:SendMessage │ │ │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${remotionSQSLambdaRole.Arn │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.co │ ││ │ } │ │ │ m │ │└───┴─────────────────────────────┴────────┴─────────────────────────────┴─────────────────────────────┴────────────────────────────────┘IAM Policy Changes┌───┬──────────────────────────┬────────────────────────────────────────────────────────────────────────────────┐│ │ Resource │ Managed Policy ARN │├───┼──────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤│ + │ ${api-integration-role} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole │├───┼──────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤│ + │ ${remotionSQSLambdaRole} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole ││ + │ ${remotionSQSLambdaRole} │ arn:${AWS::Partition}:iam::XXXXXXXXXX:policy/remotion-executionrole-policy │└───┴──────────────────────────┴────────────────────────────────────────────────────────────────────────────────┘(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)Do you wish to deploy these changes (y/n)? y
部署输出bash
Bundling asset apigw-sqs-app-stack/enqueue-function/Code/Stage...cdk.out/bundling-temp-a813aece2454684086de775f918faac45b1b77c67fff24ec6aad4bff8c978ebe/index.js 881.5kb⚡ Done in 72msBundling asset apigw-sqs-app-stack/render-function/Code/Stage...cdk.out/bundling-temp-e7d973ee34691a8e6a2ceda969fbf59380866bb486be333238b7e554907f7b95/index.js 2.6kb⚡ Done in 2msadded 279 packages, and audited 280 packages in 2s21 packages are looking for fundingrun `npm fund` for detailsfound 0 vulnerabilities✨ Synthesis time: 7.97sapigw-sqs-app-stack: building assets...[0%] start: Building 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[0%] start: Building 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[0%] start: Building cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2[33%] success: Built 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[66%] success: Built 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[100%] success: Built cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2apigw-sqs-app-stack: assets builtThis deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).Please confirm you intend to make the following modifications:IAM Statement Changes┌───┬─────────────────────────────┬────────┬─────────────────────────────┬─────────────────────────────┬────────────────────────────────┐│ │ Resource │ Effect │ Action │ Principal │ Condition │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${api-integration-role.Arn} │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.co │ ││ │ │ │ │ m │ │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${enqueue-function.Arn} │ Allow │ lambda:InvokeFunction │ Service:apigateway.amazonaw │ "ArnLike": { ││ │ │ │ │ s.com │ "AWS:SourceArn": "arn:${AWS: ││ │ │ │ │ │ :Partition}:execute-api:ap-sou ││ │ │ │ │ │ theast-2:XXXXXXXXXX:${apiC85 ││ │ │ │ │ │ 50315}/*/*/enqueue" ││ │ │ │ │ │ } ││ + │ ${enqueue-function.Arn} │ Allow │ lambda:InvokeFunction │ Service:apigateway.amazonaw │ "ArnLike": { ││ │ │ │ │ s.com │ "AWS:SourceArn": "arn:${AWS: ││ │ │ │ │ │ :Partition}:execute-api:ap-sou ││ │ │ │ │ │ theast-2:XXXXXXXXXX:${apiC85 ││ │ │ │ │ │ 50315}/*/*/enqueue" ││ │ │ │ │ │ } │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${queue.Arn} │ Allow │ sqs:ChangeMessageVisibility │ AWS:${remotionSQSLambdaRole │ ││ │ │ │ sqs:DeleteMessage │ } │ ││ │ │ │ sqs:GetQueueAttributes │ │ ││ │ │ │ sqs:GetQueueUrl │ │ ││ │ │ │ sqs:ReceiveMessage │ │ ││ + │ ${queue.Arn} │ Allow │ sqs:GetQueueAttributes │ AWS:${api-integration-role} │ ││ │ │ │ sqs:GetQueueUrl │ │ ││ │ │ │ sqs:SendMessage │ │ │├───┼─────────────────────────────┼────────┼─────────────────────────────┼─────────────────────────────┼────────────────────────────────┤│ + │ ${remotionSQSLambdaRole.Arn │ Allow │ sts:AssumeRole │ Service:lambda.amazonaws.co │ ││ │ } │ │ │ m │ │└───┴─────────────────────────────┴────────┴─────────────────────────────┴─────────────────────────────┴────────────────────────────────┘IAM Policy Changes┌───┬──────────────────────────┬────────────────────────────────────────────────────────────────────────────────┐│ │ Resource │ Managed Policy ARN │├───┼──────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤│ + │ ${api-integration-role} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole │├───┼──────────────────────────┼────────────────────────────────────────────────────────────────────────────────┤│ + │ ${remotionSQSLambdaRole} │ arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole ││ + │ ${remotionSQSLambdaRole} │ arn:${AWS::Partition}:iam::XXXXXXXXXX:policy/remotion-executionrole-policy │└───┴──────────────────────────┴────────────────────────────────────────────────────────────────────────────────┘(NOTE: There may be security-related changes not in this list. See https://github.com/aws/aws-cdk/issues/1299)Do you wish to deploy these changes (y/n)? y
选择 'y' 作为提示的答案,CDK 将继续部署 Stack,其中包括 2 个函数。
最终输出bash
apigw-sqs-app-stack: deploying... [1/1][0%] start: Publishing 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[0%] start: Publishing 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[0%] start: Publishing cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2[33%] success: Published 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[66%] success: Published 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[100%] success: Published cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2apigw-sqs-app-stack: creating CloudFormation changeset...✅ apigw-sqs-app-stack✨ Deployment time: 158.36sOutputs:apigw-sqs-app-stack.apiUrl = https://6mvgq2iad9.execute-api.ap-southeast-2.amazonaws.com/apigw-sqs-app-stack.queuearn = arn:aws:sqs:ap-southeast-2:XXXXXXXXXX:remotion_queueapigw-sqs-app-stack.queuename = remotion_queueapigw-sqs-app-stack.queueurl = https://sqs.ap-southeast-2.amazonaws.com/XXXXXXXXXX/remotion_queueapigw-sqs-app-stack.region = ap-southeast-2apigw-sqs-app-stack.userPoolClientId = 5d88adjpffj314pm4pot8g292iapigw-sqs-app-stack.userPoolId = ap-southeast-2_vzSlhO9O0Stack ARN:arn:aws:cloudformation:ap-southeast-2:XXXXXXXXXX:stack/apigw-sqs-app-stack/acb8b8f0-a52a-11ed-a440-024da00b5a8e
最终输出bash
apigw-sqs-app-stack: deploying... [1/1][0%] start: Publishing 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[0%] start: Publishing 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[0%] start: Publishing cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2[33%] success: Published 3b7a9f596977e2db94a676c6c89c99dd7eb87a5985f97a11ff23b9f338027764:XXXXXXXXXX-ap-southeast-2[66%] success: Published 8efaff13bbe794558db1f1cb8f506bc13b87d7ab3e568ebc324bac680da3a75d:XXXXXXXXXX-ap-southeast-2[100%] success: Published cf7f13fe5c0ff3b22e7352152a554dd8a4767f6a5e2285e6bf353fc42070e697:XXXXXXXXXX-ap-southeast-2apigw-sqs-app-stack: creating CloudFormation changeset...✅ apigw-sqs-app-stack✨ Deployment time: 158.36sOutputs:apigw-sqs-app-stack.apiUrl = https://6mvgq2iad9.execute-api.ap-southeast-2.amazonaws.com/apigw-sqs-app-stack.queuearn = arn:aws:sqs:ap-southeast-2:XXXXXXXXXX:remotion_queueapigw-sqs-app-stack.queuename = remotion_queueapigw-sqs-app-stack.queueurl = https://sqs.ap-southeast-2.amazonaws.com/XXXXXXXXXX/remotion_queueapigw-sqs-app-stack.region = ap-southeast-2apigw-sqs-app-stack.userPoolClientId = 5d88adjpffj314pm4pot8g292iapigw-sqs-app-stack.userPoolId = ap-southeast-2_vzSlhO9O0Stack ARN:arn:aws:cloudformation:ap-southeast-2:XXXXXXXXXX:stack/apigw-sqs-app-stack/acb8b8f0-a52a-11ed-a440-024da00b5a8e
部署将提供变量,如 apigw-sqs-app-stack.region
、apigw-sqs-app-stack.userPoolClientId
和 apigw-sqs-app-stack.userPoolId
。
8. 如果不再需要,从您的 AWS 账户中移除 apigw-sqs-app
从 apigw-sqs-app
目录中。
bash
cdk destroy
bash
cdk destroy
将 API Gateway、Remotion Lambda 和 SQS 结合在一起。
这些是有关 IAM 角色如何被函数使用以及如何启用 render-lambda-function 来消耗 SQS 消息的重要信息。
从 CDK 栈代码 中,以下是需要注意的重要部分。
- 该栈创建了一个名为
remotion_queue
的队列。
创建队列js
// 👇 create the queueconst remotionQueue = new sqs.Queue(this, 'queue', {encryption: sqs.QueueEncryption.KMS_MANAGED,queueName: 'remotion_queue',});
创建队列js
// 👇 create the queueconst remotionQueue = new sqs.Queue(this, 'queue', {encryption: sqs.QueueEncryption.KMS_MANAGED,queueName: 'remotion_queue',});
-
remotion_queue
授予 2 个 Lambda 函数与其交互的访问权限。每个单独的角色分配给其各自的 Lambda 函数。apiIntegrationRolejs// 👇 create the apiIntegrationRole roleconst apiIntegrationRole = new IAM.Role(this, 'api-integration-role', {assumedBy: new IAM.ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),],});apiIntegrationRolejs// 👇 create the apiIntegrationRole roleconst apiIntegrationRole = new IAM.Role(this, 'api-integration-role', {assumedBy: new IAM.ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),],}); -
此角色分配给
enqueue-function
,以允许其写入 CloudWatch 日志。remotionSQSLambdaRolejs// 👇 create a role with custom nameconst renderFunctionLambdaRole = new Role(this, 'remotionSQSLambdaRole', {roleName: 'remotionSQSLambdaRole',assumedBy: new ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),ManagedPolicy.fromManagedPolicyName(this,'remotion-executionrole-policy','remotion-executionrole-policy',),],});remotionSQSLambdaRolejs// 👇 create a role with custom nameconst renderFunctionLambdaRole = new Role(this, 'remotionSQSLambdaRole', {roleName: 'remotionSQSLambdaRole',assumedBy: new ServicePrincipal('lambda.amazonaws.com'),managedPolicies: [ManagedPolicy.fromAwsManagedPolicyName('service-role/AWSLambdaBasicExecutionRole',),ManagedPolicy.fromManagedPolicyName(this,'remotion-executionrole-policy','remotion-executionrole-policy',),],}); -
render-lambda-function
已被分配此角色,其中包括与 Cloudwatch 交互的访问权限以及根据remotion-executionrole-policy
中指定的其他 AWS 服务的访问权限。授予队列访问权限js// 👇 Grant permission to publish to the queueremotionQueue.grantSendMessages(apiIntegrationRole);// 👇 grant permission to consume messages from the queueremotionQueue.grantConsumeMessages(renderFunctionLambdaRole);授予队列访问权限js// 👇 Grant permission to publish to the queueremotionQueue.grantSendMessages(apiIntegrationRole);// 👇 grant permission to consume messages from the queueremotionQueue.grantConsumeMessages(renderFunctionLambdaRole); -
允许渲染函数监听队列。
监听队列jsremotionRenderFunction.addEventSource(new SqsEventSource(remotionQueue, {batchSize: 1,maxBatchingWindow: Duration.minutes(5),reportBatchItemFailures: true, // default to false}),);监听队列jsremotionRenderFunction.addEventSource(new SqsEventSource(remotionQueue, {batchSize: 1,maxBatchingWindow: Duration.minutes(5),reportBatchItemFailures: true, // default to false}),);
与 API 交互
API 需要授权令牌才能与其交互。要获取令牌:
- 部署成功后,您将获得输出,例如
apigw-sqs-app-stack.region
、apigw-sqs-app-stack.userPoolClientId
和apigw-sqs-app-stack.userPoolId
,用于与 Cognito 进行身份验证。 - 如果您没有需要用户登录的前端,您可以按照此 指南 为 API 创建用户和身份验证令牌。
从指南中,YOUR_USER_POOL_CLIENT_ID
是 apigw-sqs-app-stack.userPoolClientId
,YOUR_USER_POOL_ID
是 apigw-sqs-app-stack.userPoolId
,应按照步骤进行,直到检索到 IdToken
。
基本 API URL 的格式为 https://25w651t09g.execute-api.ap-southeast-2.amazonaws.com/dev/enqueue
,来自输出 apigw-sqs-app-stack.apiUrl
。
触发视频生成请求
渲染视频bash
curl --location --request POST 'https://xxxxxxxx.execute-api.ap-southeast-2.amazonaws.com/dev/enqueue' \--header 'Authorization: Bearer eyJraWQiOiJMVVVVZGtIQ1JXWEEyWEEXXXXXXXXXjMKR1t5S-oA' \--data-raw '{"message": "Hello"}'
渲染视频bash
curl --location --request POST 'https://xxxxxxxx.execute-api.ap-southeast-2.amazonaws.com/dev/enqueue' \--header 'Authorization: Bearer eyJraWQiOiJMVVVVZGtIQ1JXWEEyWEEXXXXXXXXXjMKR1t5S-oA' \--data-raw '{"message": "Hello"}'
响应bash
{{"message":"消息已发送到 SQS- 这是 MessageId: a6abd0bc-b838-48b5-a562-4c511fac5b2f"}}
响应bash
{{"message":"消息已发送到 SQS- 这是 MessageId: a6abd0bc-b838-48b5-a562-4c511fac5b2f"}}
这将通过将 JSON 放入队列来启动视频的渲染请求。
在另一端,render-lambda-function
将通过 SQS 被通知有一个视频渲染请求,它将从渲染请求中消耗数据并根据指示生成视频,该函数执行 renderMediaOnLambda()
作为流程的一部分。
注意
- Remotion Lambda 的部署配置为仅部署到
ap-southeast-2
地区,以简化项目,您可以在region.ts
中的代码中进行调整。 apigw-sqs-app
的部署配置为部署到ap-southeast-2
地区,以简化项目,您可以在remotion-cdk-starter.ts
中的代码中进行调整。- 在部署时,Remotion 包应该被捆绑在函数内部,您可以在
bundling
对象的nodeModules
属性中找到此代码,代码位于这里。