Skip to content

🎥 Make videos programmatically with React

refactor clients to support changing accounts

Jonny Burger565f27a643c1
diff --git a/packages/lambda/src/shared/aws-clients.ts b/packages/lambda/src/shared/aws-clients.ts
index 9c68002110..ebde6cce4a 100644
--- a/packages/lambda/src/shared/aws-clients.ts
+++ b/packages/lambda/src/shared/aws-clients.ts
@@ -5,58 +5,93 @@ 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>> = {};
-
-const credentials =
- process.env.REMOTION_AWS_ACCESS_KEY_ID &&
- process.env.REMOTION_AWS_SECRET_ACCESS_KEY
+const _clients: Partial<
+ Record<
+ AwsRegion,
+ Record<
+ string,
+ Record<string, CloudWatchLogsClient | LambdaClient | S3Client | IAMClient>
+ >
+ >
+> = {};
+
+const getCredentials = () => {
+ return process.env.REMOTION_AWS_ACCESS_KEY_ID &&
+ process.env.REMOTION_AWS_SECRET_ACCESS_KEY
? {
accessKeyId: process.env.REMOTION_AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.REMOTION_AWS_SECRET_ACCESS_KEY,
}
- : undefined;
+ : {
+ accessKeyId: process.env.AWS_ACCESS_KEY_ID as string,
+ secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY as string,
+ };
+};
-export const getCloudWatchLogsClient = (
- region: AwsRegion
+const getCredentialsKey = () => JSON.stringify(getCredentials());
+
+export const getServiceClient = (
+ region: AwsRegion,
+ service: 'cloudwatch' | 'lambda' | 's3' | 'iam'
): CloudWatchLogsClient => {
- if (!_cloudWatchLogsClients[region]) {
- checkCredentials();
- _cloudWatchLogsClients[region] = new CloudWatchLogsClient({
- region,
- credentials,
- });
+ if (!_clients[region]) {
+ _clients[region] = {};
}
- return _cloudWatchLogsClients[region] as CloudWatchLogsClient;
-};
+ const Client = (() => {
+ if (service === 'cloudwatch') {
+ return CloudWatchLogsClient;
+ }
+
+ if (service === 'lambda') {
+ return LambdaClient;
+ }
+
+ if (service === 's3') {
+ return S3Client;
+ }
+
+ if (service === 'iam') {
+ return IAMClient;
+ }
+ })();
+
+ const key = getCredentialsKey();
+ // @ts-expect-error
+ if (!_clients[region][key]) {
+ // @ts-expect-error
+ _clients[region][key] = {};
+ }
-export const getS3Client = (region: AwsRegion) => {
- if (!_s3Clients[region]) {
+ // @ts-expect-error
+ if (!_clients[region][key][service]) {
checkCredentials();
- _s3Clients[region] = new S3Client({region, credentials});
+
+ // @ts-expect-error
+ _clients[region][key][service] = new Client({
+ region,
+ credentials: getCredentials(),
+ });
}
- return _s3Clients[region] as S3Client;
+ // @ts-expect-error
+ return _clients[region][key][service];
};
-export const getLambdaClient = (region: AwsRegion) => {
- if (!_lambdaClients[region]) {
- checkCredentials();
- _lambdaClients[region] = new LambdaClient({region, credentials});
- }
+export const getCloudWatchLogsClient = (
+ region: AwsRegion
+): CloudWatchLogsClient => {
+ return getServiceClient(region, 'cloudwatch') as CloudWatchLogsClient;
+};
- return _lambdaClients[region] as LambdaClient;
+export const getS3Client = (region: AwsRegion): S3Client => {
+ return getServiceClient(region, 's3') as S3Client;
};
-export const getIamClient = (region: AwsRegion) => {
- if (!_iamClients[region]) {
- checkCredentials();
- _iamClients[region] = new IAMClient({region, credentials});
- }
+export const getLambdaClient = (region: AwsRegion): LambdaClient => {
+ return getServiceClient(region, 'lambda') as LambdaClient;
+};
- return _iamClients[region] as IAMClient;
+export const getIamClient = (region: AwsRegion): IAMClient => {
+ return getServiceClient(region, 'iam') as IAMClient;
};