🎥 Make videos programmatically with React
multiregion
Jonny Burgerd80a98f6f71e
diff --git a/packages/lambda/.gitignore b/packages/lambda/.gitignoreindex afffc3595e..d0695a9fb8 100644--- a/packages/lambda/.gitignore+++ b/packages/lambda/.gitignore@@ -7,3 +7,4 @@ function-stitcher.zipffmpegchromium.zipchromium+src/admin/remotion.zipdiff --git a/packages/lambda/src/admin/README.md b/packages/lambda/src/admin/README.mdnew file mode 100644index 0000000000..4db2fd8380--- /dev/null+++ b/packages/lambda/src/admin/README.md@@ -0,0 +1 @@+Admin folder - these functions are only meant for the Remotion team to handle the Lambda infrastructure.diff --git a/packages/lambda/src/admin/replicate-binaries.ts b/packages/lambda/src/admin/replicate-binaries.tsnew file mode 100644index 0000000000..34d67ed373--- /dev/null+++ b/packages/lambda/src/admin/replicate-binaries.ts@@ -0,0 +1,67 @@+import {+ CreateBucketCommand,+ GetBucketLocationCommand,+ PutObjectCommand,+} from '@aws-sdk/client-s3';+import {CliInternals} from '@remotion/cli';+import {Log} from '@remotion/cli/dist/log';+import {createReadStream} from 'fs';+import path from 'path';+import {AwsRegion, AWS_REGIONS} from '../pricing/aws-regions';+import {getS3Client} from '../shared/aws-clients';+import {getBinariesBucketName} from '../shared/constants';++const bucketExistsInRegion = async ({+ bucketName,+ region,+}: {+ bucketName: string;+ region: AwsRegion;+}) => {+ try {+ const bucket = await getS3Client(region).send(+ new GetBucketLocationCommand({+ Bucket: bucketName,+ })+ );++ return (bucket.LocationConstraint ?? 'us-east-1') === region;+ } catch (err) {+ if (err.Code === 'NoSuchBucket') {+ return false;+ }++ throw err;+ }+};++CliInternals.xns(async () => {+ for (const region of AWS_REGIONS) {+ Log.info('Region:', region);+ const client = getS3Client(region);+ const bucketName = getBinariesBucketName(region);++ const bucketExists = await bucketExistsInRegion({bucketName, region});+ if (!bucketExists) {+ await client.send(+ new CreateBucketCommand({+ ACL: 'public-read',+ Bucket: bucketName,+ })+ );+ Log.info(`Bucket ${bucketName} created.`);+ }++ const Body = createReadStream(path.join(__dirname, 'remotion.zip'));++ Log.info(`Uploading ${region}...`);+ await client.send(+ new PutObjectCommand({+ Key: 'remotion.zip',+ Bucket: bucketName,+ Body,+ ACL: 'public-read',+ })+ );+ }+});diff --git a/packages/lambda/src/api/ensure-lambda-binaries.ts b/packages/lambda/src/api/ensure-lambda-binaries.tsindex e2f62dc786..c6542d207b 100644--- a/packages/lambda/src/api/ensure-lambda-binaries.ts+++ b/packages/lambda/src/api/ensure-lambda-binaries.ts@@ -4,23 +4,26 @@ import {ListLayersCommand,PublishLayerVersionCommand,} from '@aws-sdk/client-lambda';+import {AwsRegion} from '../pricing/aws-regions';+import {getLambdaClient} from '../shared/aws-clients';+import {getBinariesBucketName} from '../shared/constants';const runtimes: string[] = ['nodejs14.x', 'nodejs12.x', 'nodejs10.x'];const LAYER_NAME = 'remotion-binaries';const createLayer = ({- lambdaClient,+ region,name,key,sourceS3Bucket,}: {- lambdaClient: LambdaClient;+ region: AwsRegion;name: string;key: string;sourceS3Bucket: string;}) => {- return lambdaClient.send(+ return getLambdaClient(region).send(new PublishLayerVersionCommand({Content: {S3Bucket: sourceS3Bucket,@@ -50,10 +53,10 @@ const hasLayer = (name: string, layers: LayersListItem[]) => {const ensureLayer = async ({layers,- lambdaClient,+ region,}: {+ region: AwsRegion;layers: LayersListItem[];- lambdaClient: LambdaClient;}): Promise<string> => {const existingLayer = hasLayer(LAYER_NAME, layers);if (existingLayer) {@@ -61,17 +64,17 @@ const ensureLayer = async ({}const layer = await createLayer({- lambdaClient,name: LAYER_NAME,key: 'remotion.zip',- sourceS3Bucket: 'remotion-binaries',+ sourceS3Bucket: getBinariesBucketName(region),+ region,});return layer.LayerVersionArn as string;};-export const ensureLambdaBinaries = async (lambdaClient: LambdaClient) => {- const layers = await getLayers(lambdaClient);- const layerArn = await ensureLayer({layers, lambdaClient});+export const ensureLambdaBinaries = async (region: AwsRegion) => {+ const layers = await getLayers(getLambdaClient(region));+ const layerArn = await ensureLayer({layers, region});return {layerArn};};diff --git a/packages/lambda/src/api/get-buckets.ts b/packages/lambda/src/api/get-buckets.tsnew file mode 100644index 0000000000..f08f4e62e3--- /dev/null+++ b/packages/lambda/src/api/get-buckets.ts@@ -0,0 +1,51 @@+import {+ Bucket,+ GetBucketLocationCommand,+ ListBucketsCommand,+} from '@aws-sdk/client-s3';+import {AwsRegion} from '../pricing/aws-regions';+import {getS3Client} from '../shared/aws-clients';+import {REMOTION_BUCKET_PREFIX} from '../shared/constants';++type BucketWithLocation = Bucket & {+ region: AwsRegion;+};++export const getRemotionS3Buckets = async (region: AwsRegion) => {+ const {Buckets} = await getS3Client(region).send(new ListBucketsCommand({}));+ if (!Buckets) {+ return {remotionBuckets: []};+ }++ const remotionBuckets = Buckets.filter(+ (b) =>+ b.Name?.startsWith(REMOTION_BUCKET_PREFIX) &&+ // TODO: Rename other buckets in Jonnys account bucket first+ !b.Name.startsWith('remotion-binaries')+ );++ const locations = await Promise.all(+ remotionBuckets.map((bucket) => {+ return getS3Client(region).send(+ new GetBucketLocationCommand({+ Bucket: bucket.Name as string,+ })+ );+ })+ );++ const bucketsWithLocation = remotionBuckets.map(+ (bucket, i): BucketWithLocation => {+ return {+ ...bucket,+ // AWS docs: Buckets in Region us-east-1 have a LocationConstraint of null!!+ region: (locations[i].LocationConstraint ?? 'us-east-1') as AwsRegion,+ };+ }+ );+ return {+ remotionBuckets: bucketsWithLocation.filter((bucket) => {+ return bucket.region === region;+ }),+ };+};diff --git a/packages/lambda/src/api/iam-validation/required-permissions.ts b/packages/lambda/src/api/iam-validation/required-permissions.tsindex af3e8864d5..12a7c1a569 100644--- a/packages/lambda/src/api/iam-validation/required-permissions.ts+++ b/packages/lambda/src/api/iam-validation/required-permissions.ts@@ -29,6 +29,7 @@ export const requiredPermissions: {s3.DeleteBucketWebsite,s3.PutObjectAcl,s3.PutObject,+ s3.GetBucketLocation,],resource: [`arn:aws:s3:::${REMOTION_BUCKET_PREFIX}*`],},diff --git a/packages/lambda/src/cleanup/cleanup-lambdas.ts b/packages/lambda/src/cleanup/cleanup-lambdas.tsindex b559216342..31e9631de4 100644--- a/packages/lambda/src/cleanup/cleanup-lambdas.ts+++ b/packages/lambda/src/cleanup/cleanup-lambdas.ts@@ -7,7 +7,6 @@ import {RENDER_FN_PREFIX} from '../shared/constants';export const getRemotionLambdas = async (lambdaClient: LambdaClient) => {const lambdas = await lambdaClient.send(new ListFunctionsCommand({}));-const remotionLambdas = (lambdas.Functions || []).filter((f) =>f.FunctionName?.startsWith(RENDER_FN_PREFIX));diff --git a/packages/lambda/src/cleanup/s3-buckets.ts b/packages/lambda/src/cleanup/s3-buckets.tsindex c698be04b7..f680a03542 100644--- a/packages/lambda/src/cleanup/s3-buckets.ts+++ b/packages/lambda/src/cleanup/s3-buckets.ts@@ -1,27 +1,27 @@import {DeleteBucketCommand,DeleteObjectCommand,- ListBucketsCommand,ListObjectsCommand,- S3Client,} from '@aws-sdk/client-s3';import pLimit from 'p-limit';-import {REMOTION_BUCKET_PREFIX} from '../shared/constants';+import {getRemotionS3Buckets} from '../api/get-buckets';+import {AwsRegion} from '../pricing/aws-regions';+import {getS3Client} from '../shared/aws-clients';const limit = pLimit(10);const cleanItems = async ({- s3client,bucket,onAfterItemDeleted,onBeforeItemDeleted,+ region,}: {- s3client: S3Client;bucket: string;+ region: AwsRegion;onBeforeItemDeleted: (data: {bucketName: string; itemName: string}) => void;onAfterItemDeleted: (data: {bucketName: string; itemName: string}) => void;}) => {- const list = await s3client.send(+ const list = await getS3Client(region).send(new ListObjectsCommand({Bucket: bucket,})@@ -34,7 +34,7 @@ const cleanItems = async ({bucketName: bucket,itemName: object.Key as string,});- await s3client.send(+ await getS3Client(region).send(new DeleteObjectCommand({Bucket: bucket,Key: object.Key,@@ -49,7 +49,7 @@ const cleanItems = async ({);await cleanItems({- s3client,+ region,bucket,onAfterItemDeleted,onBeforeItemDeleted,@@ -58,19 +58,19 @@ const cleanItems = async ({};const cleanBucket = async ({- s3client,+ region,bucket,onAfterItemDeleted,onBeforeItemDeleted,}: {- s3client: S3Client;+ region: AwsRegion;bucket: string;onAfterItemDeleted: (data: {bucketName: string; itemName: string}) => void;onBeforeItemDeleted: (data: {bucketName: string; itemName: string}) => void;}) => {- await cleanItems({s3client, bucket, onAfterItemDeleted, onBeforeItemDeleted});+ await cleanItems({region, bucket, onAfterItemDeleted, onBeforeItemDeleted});- await s3client.send(+ await getS3Client(region).send(new DeleteBucketCommand({Bucket: bucket,})@@ -78,19 +78,19 @@ const cleanBucket = async ({};export const cleanUpBuckets = async ({- s3client,+ region,onBeforeBucketDeleted,onAfterBucketDeleted,onAfterItemDeleted,onBeforeItemDeleted,}: {- s3client: S3Client;+ region: AwsRegion;onBeforeBucketDeleted?: (bucketName: string) => void;onAfterItemDeleted?: (data: {bucketName: string; itemName: string}) => void;onBeforeItemDeleted?: (data: {bucketName: string; itemName: string}) => void;onAfterBucketDeleted?: (bucketName: string) => void;}) => {- const {remotionBuckets} = await getRemotionS3Buckets(s3client);+ const {remotionBuckets} = await getRemotionS3Buckets(region);if (remotionBuckets.length === 0) {return;}@@ -98,7 +98,7 @@ export const cleanUpBuckets = async ({for (const bucket of remotionBuckets) {onBeforeBucketDeleted?.(bucket.Name as string);await cleanBucket({- s3client,+ region,bucket: bucket.Name as string,onAfterItemDeleted: onAfterItemDeleted ?? (() => undefined),onBeforeItemDeleted: onBeforeItemDeleted ?? (() => undefined),@@ -107,25 +107,8 @@ export const cleanUpBuckets = async ({}await cleanUpBuckets({- s3client,+ region,onAfterBucketDeleted,onBeforeBucketDeleted,});};--export const getRemotionS3Buckets = async (s3Client: S3Client) => {- const {Buckets} = await s3Client.send(new ListBucketsCommand({}));- if (!Buckets) {- return {remotionBuckets: []};- }-- const remotionBuckets = Buckets.filter(- (b) =>- b.Name?.startsWith(REMOTION_BUCKET_PREFIX) &&- // TODO: Rename other buckets in Jonnys account bucket first- !b.Name.startsWith('remotion-binaries')- );- return {- remotionBuckets,- };-};diff --git a/packages/lambda/src/cli/cleanup.ts b/packages/lambda/src/cli/cleanup.tsindex d6d380dd7f..deb2abcb14 100644--- a/packages/lambda/src/cli/cleanup.ts+++ b/packages/lambda/src/cli/cleanup.ts@@ -1,17 +1,18 @@import {LambdaClient} from '@aws-sdk/client-lambda';-import {S3Client} from '@aws-sdk/client-s3';import {CliInternals} from '@remotion/cli';import {BINARY_NAME} from '../api/bundle-remotion';+import {getRemotionS3Buckets} from '../api/get-buckets';import {cleanupLambdas, getRemotionLambdas} from '../cleanup/cleanup-lambdas';-import {cleanUpBuckets, getRemotionS3Buckets} from '../cleanup/s3-buckets';-import {getLambdaClient, getS3Client} from '../shared/aws-clients';+import {cleanUpBuckets} from '../cleanup/s3-buckets';+import {AwsRegion} from '../pricing/aws-regions';+import {getLambdaClient} from '../shared/aws-clients';import {chunk} from '../shared/chunk';import {getAwsRegion} from './get-aws-region';import {Log} from './log';export const CLEANUP_COMMAND = 'cleanup';export const CLEANUP_LAMBDAS_SUBCOMMAND = 'lambdas';-const S3_BUCKETS_SUBCOMMAND = 'buckets';+const CLEANUP_S3_BUCKETS_SUBCOMMAND = 'buckets';const cleanupLambdaCommand = async (client: LambdaClient) => {await cleanupLambdas({@@ -22,9 +23,9 @@ const cleanupLambdaCommand = async (client: LambdaClient) => {Log.info('All Remotion-related lambdas deleted.');};-const cleanupBucketsCommand = async (client: S3Client) => {+const cleanupBucketsCommand = async (region: AwsRegion) => {await cleanUpBuckets({- s3client: client,+ region,onBeforeBucketDeleted: (bucketName) => {Log.info(`Deleting items of ${bucketName}`);},@@ -44,8 +45,8 @@ export const cleanupCommand = async (args: string[]) => {return;}- if (args[0] === S3_BUCKETS_SUBCOMMAND) {- await cleanupBucketsCommand(getS3Client(getAwsRegion()));+ if (args[0] === CLEANUP_S3_BUCKETS_SUBCOMMAND) {+ await cleanupBucketsCommand(getAwsRegion());return;}@@ -53,8 +54,8 @@ export const cleanupCommand = async (args: string[]) => {fetching.update('Fetching AWS account...');const [{remotionBuckets}, lambdas] = await Promise.all([- getRemotionS3Buckets(getS3Client(region)),- getRemotionLambdas(getS3Client(region)),+ getRemotionS3Buckets(region),+ getRemotionLambdas(getLambdaClient(region)),]);if (remotionBuckets.length === 0 && lambdas.length === 0) {fetching.update(@@ -84,7 +85,7 @@ export const cleanupCommand = async (args: string[]) => {Log.info(CliInternals.chalk.gray(- `Run \`${BINARY_NAME} ${CLEANUP_COMMAND} ${S3_BUCKETS_SUBCOMMAND}\` to permanently delete S3 buckets`+ `Run \`${BINARY_NAME} ${CLEANUP_COMMAND} ${CLEANUP_S3_BUCKETS_SUBCOMMAND}\` to permanently delete S3 buckets`));Log.info();diff --git a/packages/lambda/src/run.ts b/packages/lambda/src/run.tsindex ff92f0bbd3..9d757cfb4a 100644--- a/packages/lambda/src/run.ts+++ b/packages/lambda/src/run.ts@@ -3,15 +3,15 @@ import {writeFileSync} from 'fs';import path from 'path';import {deployLambda} from './api/deploy-lambda';import {deployProject} from './api/deploy-project';+import {getRemotionS3Buckets} from './api/get-buckets';import {getOrCreateBucket} from './api/get-or-create-bucket';import {getRenderProgress} from './api/get-render-progress';import {cleanupLambdas, getRemotionLambdas} from './cleanup/cleanup-lambdas';-import {getRemotionS3Buckets} from './cleanup/s3-buckets';import {getAwsRegion} from './cli/get-aws-region';import {Log} from './cli/log';import {lambdaLs, lambdaReadFile} from './functions/helpers/io';import {AwsRegion} from './pricing/aws-regions';-import {getLambdaClient, getS3Client} from './shared/aws-clients';+import {getLambdaClient} from './shared/aws-clients';import {callLambda} from './shared/call-lambda';import {getSitesKey,@@ -49,9 +49,7 @@ const getFnName = async (options: {}const lambdas = await getRemotionLambdas(getLambdaClient(options.region));- const {remotionBuckets} = await getRemotionS3Buckets(- getS3Client(options.region)- );+ const {remotionBuckets} = await getRemotionS3Buckets(options.region);const websiteBuckets = remotionBuckets.filter((b) =>(b.Name as string).startsWith(REMOTION_BUCKET_PREFIX));diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.tsindex db0ecb0613..d71b1fc855 100644--- a/packages/lambda/src/shared/constants.ts+++ b/packages/lambda/src/shared/constants.ts@@ -1,4 +1,5 @@import {Codec, ImageFormat, PixelFormat, ProResProfile} from 'remotion';+import {AwsRegion} from '../pricing/aws-regions';export const MEMORY_SIZE = 2048;// TODO: Rename other buckets in Jonnys accoudn first@@ -25,6 +26,9 @@ export const getSitesKey = (siteId: string) => `sites/${siteId}`;// TODO: adapt file extensionexport const outName = (renderId: string) =>`${rendersPrefix(renderId)}/out.mp4`;+export const getBinariesBucketName = (region: AwsRegion) => {+ return 'lambda-remotion-binaries-' + region;+};export enum LambdaRoutines {start = 'start',