Skip to content

🎥 Make videos programmatically with React

implement getAwsClient

Jonny Burger62f03a214e1a
diff --git a/packages/docs/docs/lambda/getawsclient.md b/packages/docs/docs/lambda/getawsclient.md
new file mode 100644
index 0000000000..c07d3589cc
--- /dev/null
+++ b/packages/docs/docs/lambda/getawsclient.md
@@ -0,0 +1,78 @@
+---
+id: getawsclient
+title: getAwsClient()
+---
+
+This API exposes full access to the AWS SDK that Remotion uses under the hood. You can use it to interact with your AWS infrastructure in ways that Remotion doesn't provide a function for.
+
+## Example: Getting a buffer for a render
+
+```tsx {18-27}
+import { getAwsClient, getRenderProgress } from "@remotion/lambda";
+import { Readable } from "stream";
+
+const bucketName = "remotionlambda-d9mafgx";
+
+const getFileAsBuffer = async () => {
+ const progress = await getRenderProgress({
+ renderId: "d7nlc2y",
+ bucketName: "remotionlambda-d9mafgx",
+ functionName: "remotion-render-la8ffw",
+ region: "us-east-1",
+ });
+
+ if (!progress.outKey) {
+ // Video not yet rendered
+ return;
+ }
+
+ const { client, sdk } = getAwsClient({ region: "us-east-1", service: "s3" });
+
+ const data = client.send(
+ new sdk.GetObjectCommand({
+ Bucket: bucketName,
+ Key: progress.outKey,
+ })
+ );
+
+ return data.Body as Readable;
+};
+```
+
+## Arguments
+
+An object with two mandatory parameters:
+
+### `region`
+
+One of the [supported regions](/docs/lambda/region-selection) of Remotion Lambda, for which the client should be instantiated
+
+### `service`
+
+One of `lambda`, `cloudwatch`, `iam` and `s3`.
+
+## Return value
+
+An object with two properties:
+
+### client
+
+An AWS SDK client instantiated with the region you passed and the credentials you had set at the time of calling the function.
+
+- For `s3`: An instance of [S3Client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/classes/s3client.html)
+- For `iam`: An instance of [IAMClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-iam/classes/iamclient.html)
+- For `cloudwatch`: An instance of [CloudWatchLogsClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudwatch-logs/classes/cloudwatchlogsclient.html)
+- For `lambda`: An instance of [LambdaClient](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lambda/classes/lambdaclient.html)
+
+### sdk
+
+The full SDK JavaScript module for the service you specified.
+
+- For `s3`: The [`@aws-sdk/client-s3`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/index.html#aws-sdkclient-s3) package
+- For `iam`: The [`@aws-sdk/client-iam`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-iam/index.html#aws-sdkclient-iam) package
+- For `cloudwatch`: The [`@aws-sdk/client-cloudwatch-logs`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudwatch-logs/index.html#aws-sdkclient-cloudwatch-logs) package
+- For `lambda`: The [`@aws-sdk/client-lambda`](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-lambda/index.html#aws-sdkclient-lambda) package
+
+:::note
+You don't need to create a new client from the SDK and instead reuse the `client` that is also returned and being used by Remotion, in order to save memory.
+:::
diff --git a/packages/docs/docs/lambda/getrenderprogress.md b/packages/docs/docs/lambda/getrenderprogress.md
index b25e25ee72..16285ffaf1 100644
--- a/packages/docs/docs/lambda/getrenderprogress.md
+++ b/packages/docs/docs/lambda/getrenderprogress.md
@@ -88,6 +88,10 @@ Which bucket the render and other artifacts get saved.
`null` if the video is not yet rendered, a `string` containing a URL pointing to the final artifact if the video finished rendering.
+### `outKey`
+
+`null` if the video is not yet rendered, a `string` containi ng the S3 key where the final artifact is stored.
+
### `timeToFinish`
`null` is the video is not yet rendered, a `number` describing how long the render took to finish in miliseconds.
diff --git a/packages/docs/docs/lambda/presignurl.md b/packages/docs/docs/lambda/presignurl.md
index 72c2397473..6bc4cb1101 100644
--- a/packages/docs/docs/lambda/presignurl.md
+++ b/packages/docs/docs/lambda/presignurl.md
@@ -1,7 +1,6 @@
---
id: presignurl
title: presignUrl()
-slug: /lambda/presignurl
---
Returns a presigned url for public access of an object stored in Remotion's S3 bucket.
@@ -9,6 +8,8 @@ Returns a presigned url for public access of an object stored in Remotion's S3 b
## Example
```ts twoslash
+// @module: ESNext
+// @target: ESNext
import { presignUrl } from "@remotion/lambda";
const url = await presignUrl({
@@ -48,3 +49,5 @@ The number of seconds before the presigned URL expires.
Must be an integer and `>=1` and `<=604800` (maximum of 7 days)
## See also
+
+- [`downloadMedia()`](/docs/lambda/downloadmedia)
diff --git a/packages/docs/sidebars.js b/packages/docs/sidebars.js
index 7eda444abb..f4aa239bc6 100644
--- a/packages/docs/sidebars.js
+++ b/packages/docs/sidebars.js
@@ -80,6 +80,7 @@ module.exports = {
"lambda/getfunctions",
"lambda/deletesite",
"lambda/deploysite",
+ "lambda/getawsclient",
"lambda/getregions",
"lambda/getsites",
"lambda/downloadmedia",
@@ -87,6 +88,7 @@ module.exports = {
"lambda/getrolepolicy",
"lambda/getorcreatebucket",
"lambda/getrenderprogress",
+ "lambda/presignurl",
"lambda/rendermediaonlambda",
"lambda/renderstillonlambda",
"lambda/simulatepermissions",
diff --git a/packages/lambda/src/api/get-aws-client.ts b/packages/lambda/src/api/get-aws-client.ts
new file mode 100644
index 0000000000..7370eee684
--- /dev/null
+++ b/packages/lambda/src/api/get-aws-client.ts
@@ -0,0 +1,45 @@
+import * as CloudWatchSDK from '@aws-sdk/client-cloudwatch-logs';
+import * as IamSdk from '@aws-sdk/client-iam';
+import * as LambdaSDK from '@aws-sdk/client-lambda';
+import * as S3SDK from '@aws-sdk/client-s3';
+import {AwsRegion} from '../client';
+import {getServiceClient, ServiceMapping} from '../shared/aws-clients';
+
+export type GetAwsClientInput<T extends keyof ServiceMapping> = {
+ region: AwsRegion;
+ service: T;
+};
+
+type SdkMapping = {
+ s3: typeof S3SDK;
+ cloudwatch: typeof CloudWatchSDK;
+ iam: typeof IamSdk;
+ lambda: typeof LambdaSDK;
+};
+
+export type GetAwsClientOutput<T extends keyof ServiceMapping> = {
+ client: ServiceMapping[T];
+ sdk: SdkMapping[T];
+};
+
+/**
+ * @description Gets the full AWS SDK and an instantiated client for an AWS service
+ * @link https://v3.remotion.dev/docs/lambda/getawsclient
+ * @param {AwsRegion} params.region The region in which the S3 bucket resides in.
+ * @param {string} params.service One of `iam`, `s3`, `cloudwatch` and `iam`
+ * @returns {GetAwsClientOutput<T>} Returns `client` and `sdk` of a AWS service
+ */
+export const getAwsClient = <T extends keyof ServiceMapping>({
+ region,
+ service,
+}: GetAwsClientInput<T>): GetAwsClientOutput<T> => {
+ return {
+ client: getServiceClient(region, service),
+ sdk: {
+ lambda: LambdaSDK,
+ cloudwatch: CloudWatchSDK,
+ iam: IamSdk,
+ s3: S3SDK,
+ }[service],
+ };
+};
diff --git a/packages/lambda/src/api/get-aws-clients.ts b/packages/lambda/src/api/get-aws-clients.ts
deleted file mode 100644
index 933420f668..0000000000
--- a/packages/lambda/src/api/get-aws-clients.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import * as cloudwatchSdk from '@aws-sdk/client-cloudwatch-logs';
-import * as iamSdk from '@aws-sdk/client-iam';
-import * as lambdaSdk from '@aws-sdk/client-lambda';
-import * as s3Sdk from '@aws-sdk/client-s3';
-import {AwsRegion} from '../client';
-import {
- getCloudWatchLogsClient,
- getIamClient,
- getLambdaClient,
- getS3Client,
-} from '../shared/aws-clients';
-
-export const getAwsClients = (region: AwsRegion) => {
- return {
- s3Client: () => getS3Client(region),
- iamClient: () => getIamClient(region),
- cloudwatchClient: () => getCloudWatchLogsClient(region),
- lambdaClient: () => getLambdaClient(region),
- cloudwatchSdk,
- iamSdk,
- lambdaSdk,
- s3Sdk,
- };
-};
diff --git a/packages/lambda/src/functions/helpers/get-progress.ts b/packages/lambda/src/functions/helpers/get-progress.ts
index fddffa27e5..5576f661ec 100644
--- a/packages/lambda/src/functions/helpers/get-progress.ts
+++ b/packages/lambda/src/functions/helpers/get-progress.ts
@@ -9,6 +9,7 @@ import {
} from '../../shared/constants';
import {calculateChunkTimes} from './calculate-chunk-times';
import {estimatePriceFromBucket} from './calculate-price-from-bucket';
+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';
@@ -83,6 +84,7 @@ export const getProgress = async ({
timeToInvokeLambdas: postRenderData.timeToInvokeLambdas,
overallProgress: 1,
retriesInfo: postRenderData.retriesInfo,
+ outKey: getExpectedOutName(postRenderData.renderMetadata),
};
}
@@ -245,5 +247,7 @@ export const getProgress = async ({
rendering: renderMetadata ? chunkCount / renderMetadata.totalChunks : 0,
}),
retriesInfo,
+ outKey:
+ outputFile && renderMetadata ? getExpectedOutName(renderMetadata) : null,
};
};
diff --git a/packages/lambda/src/index.ts b/packages/lambda/src/index.ts
index c53a2e2733..b40d7adcc7 100644
--- a/packages/lambda/src/index.ts
+++ b/packages/lambda/src/index.ts
@@ -13,7 +13,11 @@ import {
downloadVideo,
} from './api/download-media';
import {estimatePrice, EstimatePriceInput} from './api/estimate-price';
-import {getAwsClients} from './api/get-aws-clients';
+import {
+ getAwsClient,
+ GetAwsClientInput,
+ GetAwsClientOutput,
+} from './api/get-aws-client';
import {
FunctionInfo,
getFunctionInfo,
@@ -75,7 +79,7 @@ export {
LambdaInternals,
renderStillOnLambda,
getRegions,
- getAwsClients,
+ getAwsClient,
presignUrl,
};
export type {
@@ -107,4 +111,6 @@ export type {
RenderStillOnLambdaOutput,
SimulatePermissionsInput,
SimulatePermissionsOutput,
+ GetAwsClientInput,
+ GetAwsClientOutput,
};
diff --git a/packages/lambda/src/shared/aws-clients.ts b/packages/lambda/src/shared/aws-clients.ts
index 14d160379d..cfacb191a1 100644
--- a/packages/lambda/src/shared/aws-clients.ts
+++ b/packages/lambda/src/shared/aws-clients.ts
@@ -35,31 +35,17 @@ const getCredentials = () => {
const getCredentialsKey = () => JSON.stringify(getCredentials());
-type ServiceMapping =
- | {
- type: 'cloudwatch';
- client: CloudWatchLogsClient;
- }
- | {
- type: 'lambda';
- client: LambdaClient;
- }
- | {
- type: 's3';
- client: S3Client;
- }
- | {
- type: 'iam';
- client: IAMClient;
- };
-
-export const getServiceClient = <
- T extends ServiceMapping,
- V extends T['type'] = 's3'
->(
+export type ServiceMapping = {
+ s3: S3Client;
+ cloudwatch: CloudWatchLogsClient;
+ iam: IAMClient;
+ lambda: LambdaClient;
+};
+
+export const getServiceClient = <T extends keyof ServiceMapping>(
region: AwsRegion,
- service: V
-): T['client'] => {
+ service: T
+): ServiceMapping[T] => {
if (!_clients[region]) {
_clients[region] = {};
}
@@ -107,17 +93,17 @@ export const getServiceClient = <
export const getCloudWatchLogsClient = (
region: AwsRegion
): CloudWatchLogsClient => {
- return getServiceClient(region, 'cloudwatch') as CloudWatchLogsClient;
+ return getServiceClient(region, 'cloudwatch');
};
export const getS3Client = (region: AwsRegion): S3Client => {
- return getServiceClient(region, 's3') as S3Client;
+ return getServiceClient(region, 's3');
};
export const getLambdaClient = (region: AwsRegion): LambdaClient => {
- return getServiceClient(region, 'lambda') as LambdaClient;
+ return getServiceClient(region, 'lambda');
};
export const getIamClient = (region: AwsRegion): IAMClient => {
- return getServiceClient(region, 'iam') as IAMClient;
+ return getServiceClient(region, 'iam');
};
diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.ts
index 851b8df715..b023345cbb 100644
--- a/packages/lambda/src/shared/constants.ts
+++ b/packages/lambda/src/shared/constants.ts
@@ -368,6 +368,7 @@ export type RenderProgress = {
renderMetadata: RenderMetadata | null;
bucket: string;
outputFile: string | null;
+ outKey: string | null;
timeToFinish: number | null;
errors: EnhancedErrorInfo[];
fatalErrorEncountered: boolean;