🎥 Make videos programmatically with React
Add capability to create CloudWatch LogGroup with proper retention policy when deploying Lambda function
Evan Cloutierf4ffa6182f91
diff --git a/packages/lambda/package-lock.json b/packages/lambda/package-lock.jsonindex ebdf5c48fb..195ac2b2eb 100644--- a/packages/lambda/package-lock.json+++ b/packages/lambda/package-lock.json@@ -1,14 +1,15 @@{"name": "@remotion/lambda",- "version": "2.3.4",+ "version": "2.4.0","lockfileVersion": 2,"requires": true,"packages": {"": {"name": "@remotion/lambda",- "version": "2.3.4",+ "version": "2.4.0","license": "MIT","dependencies": {+ "@aws-sdk/client-cloudwatch-logs": "^3.18.0","@aws-sdk/client-iam": "^3.18.0","@aws-sdk/client-lambda": "^3.18.0","@aws-sdk/client-s3": "^3.18.0",@@ -150,6 +151,47 @@"tslib": "^2.0.0"}},+ "node_modules/@aws-sdk/client-cloudwatch-logs": {+ "version": "3.18.0",+ "resolved": "https://registry.npmjs.org/@aws-sdk/client-cloudwatch-logs/-/client-cloudwatch-logs-3.18.0.tgz",+ "integrity": "sha512-DVuyQ2Vye1/psWqOalmmqAxYqZQmeqfpsUiPGZdvUmzCnntSH2A+lntWIGdhLB3JX6wPZ5HMvvlka8WwO5prqw==",+ "dependencies": {+ "@aws-crypto/sha256-browser": "^1.0.0",+ "@aws-crypto/sha256-js": "^1.0.0",+ "@aws-sdk/client-sts": "3.18.0",+ "@aws-sdk/config-resolver": "3.18.0",+ "@aws-sdk/credential-provider-node": "3.18.0",+ "@aws-sdk/fetch-http-handler": "3.18.0",+ "@aws-sdk/hash-node": "3.18.0",+ "@aws-sdk/invalid-dependency": "3.18.0",+ "@aws-sdk/middleware-content-length": "3.18.0",+ "@aws-sdk/middleware-host-header": "3.18.0",+ "@aws-sdk/middleware-logger": "3.18.0",+ "@aws-sdk/middleware-retry": "3.18.0",+ "@aws-sdk/middleware-serde": "3.18.0",+ "@aws-sdk/middleware-signing": "3.18.0",+ "@aws-sdk/middleware-stack": "3.18.0",+ "@aws-sdk/middleware-user-agent": "3.18.0",+ "@aws-sdk/node-config-provider": "3.18.0",+ "@aws-sdk/node-http-handler": "3.18.0",+ "@aws-sdk/protocol-http": "3.18.0",+ "@aws-sdk/smithy-client": "3.18.0",+ "@aws-sdk/types": "3.18.0",+ "@aws-sdk/url-parser": "3.18.0",+ "@aws-sdk/util-base64-browser": "3.18.0",+ "@aws-sdk/util-base64-node": "3.18.0",+ "@aws-sdk/util-body-length-browser": "3.18.0",+ "@aws-sdk/util-body-length-node": "3.18.0",+ "@aws-sdk/util-user-agent-browser": "3.18.0",+ "@aws-sdk/util-user-agent-node": "3.18.0",+ "@aws-sdk/util-utf8-browser": "3.18.0",+ "@aws-sdk/util-utf8-node": "3.18.0",+ "tslib": "^2.0.0"+ },+ "engines": {+ "node": ">=10.0.0"+ }+ },"node_modules/@aws-sdk/client-iam": {"version": "3.18.0","resolved": "https://registry.npmjs.org/@aws-sdk/client-iam/-/client-iam-3.18.0.tgz",@@ -7915,7 +7957,22 @@"resolved": "https://registry.npmjs.org/lambdafs/-/lambdafs-2.0.3.tgz","integrity": "sha512-5YWwZA/QKk09GdfcJ/ABVO+bpFoGlnTBa5jmyM8Kt9yIzl2lDDVBPLK+Aenq2UEcuDpqxXIYI5zLB7VZNepTrg==","bundleDependencies": [- "tar-fs"+ "tar-fs",+ "base64-js",+ "bl",+ "buffer",+ "chownr",+ "end-of-stream",+ "fs-constants",+ "ieee754",+ "inherits",+ "mkdirp-classic",+ "once",+ "pump",+ "string_decoder",+ "tar-stream",+ "util-deprecate",+ "wrappy"],"dependencies": {"tar-fs": "^2.1.1"@@ -13196,6 +13253,44 @@"tslib": "^2.0.0"}},+ "@aws-sdk/client-cloudwatch-logs": {+ "version": "3.18.0",+ "resolved": "https://registry.npmjs.org/@aws-sdk/client-cloudwatch-logs/-/client-cloudwatch-logs-3.18.0.tgz",+ "integrity": "sha512-DVuyQ2Vye1/psWqOalmmqAxYqZQmeqfpsUiPGZdvUmzCnntSH2A+lntWIGdhLB3JX6wPZ5HMvvlka8WwO5prqw==",+ "requires": {+ "@aws-crypto/sha256-browser": "^1.0.0",+ "@aws-crypto/sha256-js": "^1.0.0",+ "@aws-sdk/client-sts": "3.18.0",+ "@aws-sdk/config-resolver": "3.18.0",+ "@aws-sdk/credential-provider-node": "3.18.0",+ "@aws-sdk/fetch-http-handler": "3.18.0",+ "@aws-sdk/hash-node": "3.18.0",+ "@aws-sdk/invalid-dependency": "3.18.0",+ "@aws-sdk/middleware-content-length": "3.18.0",+ "@aws-sdk/middleware-host-header": "3.18.0",+ "@aws-sdk/middleware-logger": "3.18.0",+ "@aws-sdk/middleware-retry": "3.18.0",+ "@aws-sdk/middleware-serde": "3.18.0",+ "@aws-sdk/middleware-signing": "3.18.0",+ "@aws-sdk/middleware-stack": "3.18.0",+ "@aws-sdk/middleware-user-agent": "3.18.0",+ "@aws-sdk/node-config-provider": "3.18.0",+ "@aws-sdk/node-http-handler": "3.18.0",+ "@aws-sdk/protocol-http": "3.18.0",+ "@aws-sdk/smithy-client": "3.18.0",+ "@aws-sdk/types": "3.18.0",+ "@aws-sdk/url-parser": "3.18.0",+ "@aws-sdk/util-base64-browser": "3.18.0",+ "@aws-sdk/util-base64-node": "3.18.0",+ "@aws-sdk/util-body-length-browser": "3.18.0",+ "@aws-sdk/util-body-length-node": "3.18.0",+ "@aws-sdk/util-user-agent-browser": "3.18.0",+ "@aws-sdk/util-user-agent-node": "3.18.0",+ "@aws-sdk/util-utf8-browser": "3.18.0",+ "@aws-sdk/util-utf8-node": "3.18.0",+ "tslib": "^2.0.0"+ }+ },"@aws-sdk/client-iam": {"version": "3.18.0","resolved": "https://registry.npmjs.org/@aws-sdk/client-iam/-/client-iam-3.18.0.tgz",@@ -17071,8 +17166,7 @@"dev": true,"requires": {"@typescript-eslint/experimental-utils": "^4.12.0",- "ts-prune": "https://firebasestorage.googleapis.com/v0/b/start-react-native.appspot.com/o/ts-prune-v0.0.3-wcandillon.tgz?alt=media&token=f5c337f6-c8bb-41dd-a958-cb9c52e2f6d1",- "typescript": "^4.1.3"+ "ts-prune": "https://firebasestorage.googleapis.com/v0/b/start-react-native.appspot.com/o/ts-prune-v0.0.3-wcandillon.tgz?alt=media&token=f5c337f6-c8bb-41dd-a958-cb9c52e2f6d1"}},"eslint-scope": {diff --git a/packages/lambda/package.json b/packages/lambda/package.jsonindex 5a033c9424..e0cc5b6e76 100644--- a/packages/lambda/package.json+++ b/packages/lambda/package.json@@ -20,6 +20,7 @@"url": "https://github.com/JonnyBurger/remotion"},"dependencies": {+ "@aws-sdk/client-cloudwatch-logs": "^3.18.0","@aws-sdk/client-iam": "^3.18.0","@aws-sdk/client-lambda": "^3.18.0","@aws-sdk/client-s3": "^3.18.0",@@ -45,9 +46,9 @@},"devDependencies": {"@jonny/eslint-config": "^2.1.210",+ "@types/jest": "^26.0.23","@types/node": "^14.14.14","@types/prompt": "^1.1.0",- "@types/jest": "^26.0.23","eslint": "^7.4.0","jest": "^26.6.3","prettier": "^2.0.5",diff --git a/packages/lambda/src/api/create-function.ts b/packages/lambda/src/api/create-function.tsindex 7352aeca66..83212989fa 100644--- a/packages/lambda/src/api/create-function.ts+++ b/packages/lambda/src/api/create-function.ts@@ -1,9 +1,19 @@+import {+ CreateLogGroupCommand,+ PutRetentionPolicyCommand,+} from '@aws-sdk/client-cloudwatch-logs';import {CreateFunctionCommand} from '@aws-sdk/client-lambda';+import {Log} from '@remotion/cli/dist/log';import {readFileSync} from 'fs';import {AwsRegion} from '..';-import {getLambdaClient} from '../shared/aws-clients';+import {+ DEFAULT_CLOUDWATCH_RETENTION_PERIOD,+ LOG_GROUP_PREFIX,+} from '../defaults';+import {getCloudWatchLogsClient, getLambdaClient} from '../shared/aws-clients';export const createFunction = async ({+ createCloudWatchLogGroup = false,region,zipFile,functionName,@@ -12,6 +22,7 @@ export const createFunction = async ({timeoutInSeconds,layerArn,}: {+ createCloudWatchLogGroup?: boolean;region: AwsRegion;zipFile: string;functionName: string;@@ -20,6 +31,23 @@ export const createFunction = async ({timeoutInSeconds: number;layerArn: string;}) => {+ if (createCloudWatchLogGroup) {+ Log.info(`Creating CloudWatch Log Group...`);++ await getCloudWatchLogsClient(region).send(+ new CreateLogGroupCommand({+ logGroupName: `${LOG_GROUP_PREFIX}${functionName}`,+ })+ );++ await getCloudWatchLogsClient(region).send(+ new PutRetentionPolicyCommand({+ logGroupName: `${LOG_GROUP_PREFIX}${functionName}`,+ retentionInDays: DEFAULT_CLOUDWATCH_RETENTION_PERIOD,+ })+ );+ }+const {FunctionName} = await getLambdaClient(region).send(new CreateFunctionCommand({Code: {diff --git a/packages/lambda/src/api/deploy-function.ts b/packages/lambda/src/api/deploy-function.tsindex 7d2569251b..d2d826f64e 100644--- a/packages/lambda/src/api/deploy-function.ts+++ b/packages/lambda/src/api/deploy-function.ts@@ -11,6 +11,7 @@ import {createFunction} from './create-function';/*** @description Creates an AWS Lambda function in your account that will be able to render a video in the cloud.* @link https://remotion.dev/docs/lambda/deployfunction+ * @param options.createCloudWatchLogGroup Whether you'd like to create a CloudWatch Log Group to store the logs for this function.* @param options.region The region you want to deploy your function to.* @param options.layerArn The resource identifier of the Lambda layer. See documentation for this function on how to retrieve it.* @param options.timeoutInSeconds After how many seconds the lambda function should be killed if it does not end itself.@@ -18,6 +19,7 @@ import {createFunction} from './create-function';* @returns An object that contains the `functionName` property*/export const deployFunction = async (options: {+ createCloudWatchLogGroup?: boolean;region: AwsRegion;layerArn: string;timeoutInSeconds: number;@@ -34,6 +36,7 @@ export const deployFunction = async (options: {]);const created = await createFunction({+ createCloudWatchLogGroup: options.createCloudWatchLogGroup,region: options.region,zipFile: renderOut,functionName: fnNameRender,diff --git a/packages/lambda/src/api/iam-validation/required-permissions.ts b/packages/lambda/src/api/iam-validation/required-permissions.tsindex 8fbb45274b..136cc85bbb 100644--- a/packages/lambda/src/api/iam-validation/required-permissions.ts+++ b/packages/lambda/src/api/iam-validation/required-permissions.ts@@ -1,4 +1,4 @@-import {iam, lambda, s3} from 'aws-policies';+import {iam, lambda, logs, s3} from 'aws-policies';import {BINARIES_BUCKET_PREFIX,REMOTION_BUCKET_PREFIX,@@ -7,7 +7,7 @@ import {// TODO: Update docs before releaseexport const requiredPermissions: {- actions: (s3 | iam | lambda)[];+ actions: (s3 | iam | lambda | logs)[];resource: string[];}[] = [{@@ -72,4 +72,13 @@ export const requiredPermissions: {actions: [lambda.ListLayers, lambda.ListFunctions],resource: ['*'],},+ {+ actions: [+ logs.CreateLogGroup,+ logs.CreateLogStream,+ logs.PutLogEvents,+ logs.PutRetentionPolicy,+ ],+ resource: ['arn:aws:logs:*:*:log-group:*'],+ },];diff --git a/packages/lambda/src/cli/args.ts b/packages/lambda/src/cli/args.tsindex 381e10010a..6666dda363 100644--- a/packages/lambda/src/cli/args.ts+++ b/packages/lambda/src/cli/args.ts@@ -2,6 +2,7 @@ import minimist from 'minimist';import {AwsRegion} from '../pricing/aws-regions';type LambdaCommandLineOptions = {+ cloudwatch: boolean;help: boolean;region: AwsRegion;memory: number;diff --git a/packages/lambda/src/cli/commands/functions/deploy.ts b/packages/lambda/src/cli/commands/functions/deploy.tsindex 2340455f43..5e2b8ab3b9 100644--- a/packages/lambda/src/cli/commands/functions/deploy.ts+++ b/packages/lambda/src/cli/commands/functions/deploy.ts@@ -19,6 +19,7 @@ export const functionsDeploySubcommand = async () => {const region = getAwsRegion();const timeoutInSeconds = parsedLambdaCli.timeout ?? DEFAULT_TIMEOUT;const memorySizeInMb = parsedLambdaCli.memory ?? DEFAULT_MEMORY_SIZE;+ const createCloudWatchLogGroup = parsedLambdaCli.cloudwatch ?? false;validateMemorySize(memorySizeInMb);validateTimeout(timeoutInSeconds);Log.info(@@ -32,6 +33,7 @@ export const functionsDeploySubcommand = async () => {const {layerArn} = await ensureLambdaBinaries(getAwsRegion());output.update('Bundling lambda and deploying...');const {functionName} = await deployFunction({+ createCloudWatchLogGroup,region,timeoutInSeconds,layerArn,diff --git a/packages/lambda/src/shared/aws-clients.ts b/packages/lambda/src/shared/aws-clients.tsindex 4a8d5cd3b9..6abda2ad23 100644--- a/packages/lambda/src/shared/aws-clients.ts+++ b/packages/lambda/src/shared/aws-clients.ts@@ -1,13 +1,27 @@+import {CloudWatchLogsClient} from '@aws-sdk/client-cloudwatch-logs';import {IAMClient} from '@aws-sdk/client-iam';import {LambdaClient} from '@aws-sdk/client-lambda';import {S3Client} from '@aws-sdk/client-s3';import {AwsRegion} from '../pricing/aws-regions';import {checkCredentials} from './check-credentials';+const _cloudWatchLogsClients: Partial<Record<AwsRegion, CloudWatchLogsClient>> =+ {};const _s3Clients: Partial<Record<AwsRegion, S3Client>> = {};const _lambdaClients: Partial<Record<AwsRegion, LambdaClient>> = {};const _iamClients: Partial<Record<AwsRegion, IAMClient>> = {};+export const getCloudWatchLogsClient = (+ region: AwsRegion+): CloudWatchLogsClient => {+ if (!_cloudWatchLogsClients[region]) {+ checkCredentials();+ _cloudWatchLogsClients[region] = new CloudWatchLogsClient({region});+ }++ return _cloudWatchLogsClients[region] as CloudWatchLogsClient;+};+export const getS3Client = (region: AwsRegion) => {if (!_s3Clients[region]) {checkCredentials();diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.tsindex f6b4037dad..6a1aa67875 100644--- a/packages/lambda/src/shared/constants.ts+++ b/packages/lambda/src/shared/constants.ts@@ -24,9 +24,12 @@ export const BINARY_NAME = 'remotion lambda';export const COMMAND_NOT_FOUND = 'Command not found';export const DEFAULT_REGION: AwsRegion = 'us-east-1';+export const DEFAULT_CLOUDWATCH_RETENTION_PERIOD = 14;+// TODO: Rename other buckets in Jonnys accoudn firstexport const REMOTION_BUCKET_PREFIX = 'remotionlambda-';export const RENDER_FN_PREFIX = 'remotion-render-';+export const LOG_GROUP_PREFIX = '/aws/lambda/';export const rendersPrefix = (renderId: string) => `renders/${renderId}`;export const encodingProgressKey = (renderId: string) =>`${rendersPrefix(renderId)}/encoding-progress.json`;