Skip to content

🎥 Make videos programmatically with React

cleanup command shows buckets

Jonny Burger541adeb86254
diff --git a/packages/lambda/src/cleanup-lambdas.ts b/packages/lambda/src/cleanup/cleanup-lambdas.ts
similarity index 94%
rename from packages/lambda/src/cleanup-lambdas.ts
rename to packages/lambda/src/cleanup/cleanup-lambdas.ts
index 755a9c7980..6a5e19a3c7 100644
--- a/packages/lambda/src/cleanup-lambdas.ts
+++ b/packages/lambda/src/cleanup/cleanup-lambdas.ts
@@ -4,7 +4,7 @@ import {
ListFunctionsCommand,
} from '@aws-sdk/client-lambda';
import {CliInternals} from '@remotion/cli';
-import {REGION, RENDER_FN_PREFIX} from './constants';
+import {REGION, RENDER_FN_PREFIX} from '../constants';
export const cleanupLambdas = CliInternals.xns(
async (
diff --git a/packages/lambda/src/cleanup-buckets.ts b/packages/lambda/src/cleanup/s3-buckets.ts
similarity index 52%
rename from packages/lambda/src/cleanup-buckets.ts
rename to packages/lambda/src/cleanup/s3-buckets.ts
index 603417f2a4..949011771a 100644
--- a/packages/lambda/src/cleanup-buckets.ts
+++ b/packages/lambda/src/cleanup/s3-buckets.ts
@@ -8,7 +8,7 @@ import {
import {CliInternals} from '@remotion/cli';
import pLimit from 'p-limit';
import {Internals} from 'remotion';
-import {LAMBDA_BUCKET_PREFIX, REGION, RENDERS_BUCKET_PREFIX} from './constants';
+import {LAMBDA_BUCKET_PREFIX, RENDERS_BUCKET_PREFIX} from '../constants';
const limit = pLimit(10);
@@ -47,27 +47,36 @@ const cleanBucket = async (s3client: S3Client, bucket: string) => {
);
};
-export const cleanUpBuckets = CliInternals.xns(
- async (
- s3client: S3Client = new S3Client({
- region: REGION,
- })
- ) => {
- // TODO: pagination
- const {Buckets} = await s3client.send(new ListBucketsCommand({}));
- if (!Buckets) {
- console.log('No buckets available.');
- return;
- }
+export const cleanUpBuckets = CliInternals.xns(async (s3client: S3Client) => {
+ const {remotionBuckets} = await getRemotionS3Buckets(s3client);
+ if (remotionBuckets.length === 0) {
+ return;
+ }
- const remotionBuckets = Buckets.filter(
- (b) =>
- b.Name?.startsWith(RENDERS_BUCKET_PREFIX) ||
- b.Name?.startsWith(LAMBDA_BUCKET_PREFIX)
- );
- const names = remotionBuckets.map((b) => b.Name).filter(Internals.truthy);
- for (const bucket of names) {
- await cleanBucket(s3client, bucket);
- }
+ for (const bucket of remotionBuckets) {
+ await cleanBucket(s3client, bucket);
}
-);
+
+ await cleanUpBuckets(s3client);
+});
+
+export const getRemotionS3Buckets = async (
+ s3Client: S3Client
+): Promise<{
+ remotionBuckets: string[];
+}> => {
+ const {Buckets} = await s3Client.send(new ListBucketsCommand({}));
+ if (!Buckets) {
+ return {remotionBuckets: []};
+ }
+
+ const remotionBuckets = Buckets.filter(
+ (b) =>
+ b.Name?.startsWith(RENDERS_BUCKET_PREFIX) ||
+ b.Name?.startsWith(LAMBDA_BUCKET_PREFIX)
+ );
+ const names = remotionBuckets.map((b) => b.Name).filter(Internals.truthy);
+ return {
+ remotionBuckets: names,
+ };
+};
diff --git a/packages/lambda/src/cli/cleanup.ts b/packages/lambda/src/cli/cleanup.ts
new file mode 100644
index 0000000000..ff0a53614a
--- /dev/null
+++ b/packages/lambda/src/cli/cleanup.ts
@@ -0,0 +1,26 @@
+import {CliInternals} from '@remotion/cli';
+import {s3Client} from '../aws-clients';
+import {chunk} from '../chunk';
+import {getRemotionS3Buckets} from '../cleanup/s3-buckets';
+import {REGION} from '../constants';
+import {Log} from './log';
+
+export const cleanupCommand = async () => {
+ Log.info('Fetching AWS account...');
+ const {remotionBuckets} = await getRemotionS3Buckets(s3Client);
+ Log.info(
+ `Your AWS account in ${REGION} contains the following Remotion-related resources:`
+ );
+ Log.info(
+ `${remotionBuckets.length} ${
+ remotionBuckets.length === 1 ? 'bucket' : 'buckets'
+ }:`
+ );
+ Log.info(
+ `${chunk(remotionBuckets, 2)
+ .map((b) =>
+ CliInternals.chalk.blue(b.map((a) => a.padEnd(40, ' ')).join(' '))
+ )
+ .join('\n')}`
+ );
+};
diff --git a/packages/lambda/src/cli/help.ts b/packages/lambda/src/cli/help.ts
index 9977ec7572..f8130e6ccf 100644
--- a/packages/lambda/src/cli/help.ts
+++ b/packages/lambda/src/cli/help.ts
@@ -15,6 +15,14 @@ export const printHelp = () => {
Log.info('');
Log.info(`${BINARY_NAME} deploy <index-file.ts>`);
Log.info(chalk.gray('Deploy a lambda.'));
+ Log.info();
+
Log.info(`${BINARY_NAME} render <index-file.ts>`);
Log.info(chalk.gray('Render a video on the cloud.'));
+
+ Log.info();
+ Log.info(`${BINARY_NAME} cleanup`);
+ Log.info(
+ chalk.gray('Delete Remotion-related infrastructure from your AWS account.')
+ );
};
diff --git a/packages/lambda/src/cli/index.ts b/packages/lambda/src/cli/index.ts
index e3e71f4774..9b08189ed5 100644
--- a/packages/lambda/src/cli/index.ts
+++ b/packages/lambda/src/cli/index.ts
@@ -1,4 +1,5 @@
import {parsedCli} from './args';
+import {cleanupCommand} from './cleanup';
import {deployCommand} from './deploy';
import {printHelp} from './help';
import {Log} from './log';
@@ -18,6 +19,10 @@ export const cli = async () => {
return renderCommand();
}
+ if (parsedCli._[0] === 'cleanup') {
+ return cleanupCommand();
+ }
+
Log.error(`Command ${parsedCli._[0]} not found.`);
printHelp();
process.exit(1);
diff --git a/packages/lambda/src/run.ts b/packages/lambda/src/run.ts
index 32dbe0171e..523db62e0e 100644
--- a/packages/lambda/src/run.ts
+++ b/packages/lambda/src/run.ts
@@ -1,15 +1,16 @@
import {CliInternals} from '@remotion/cli';
+import {s3Client} from './aws-clients';
import {callLambda} from './call-lambda';
import {checkLambdaStatus} from './check-lambda-status';
-import {cleanUpBuckets} from './cleanup-buckets';
-import {cleanupLambdas} from './cleanup-lambdas';
+import {cleanupLambdas} from './cleanup/cleanup-lambdas';
+import {cleanUpBuckets} from './cleanup/s3-buckets';
import {LambdaRoutines} from './constants';
import {createLambda} from './create-lambda';
import {sleep} from './sleep';
CliInternals.xns(async () => {
await cleanupLambdas();
- await cleanUpBuckets();
+ await cleanUpBuckets(s3Client);
const {functionName, bucketUrl} = await createLambda();
const res = await callLambda({