Skip to content

🎥 Make videos programmatically with React

add retries to validation

Jonny Burger2a62fc61649e
diff --git a/packages/lambda/src/api/iam-validation/simulate-rule.ts b/packages/lambda/src/api/iam-validation/simulate-rule.ts
index acb258b1a4..c6c721dacd 100644
--- a/packages/lambda/src/api/iam-validation/simulate-rule.ts
+++ b/packages/lambda/src/api/iam-validation/simulate-rule.ts
@@ -18,40 +18,54 @@ export const simulateRule = async (options: {
actionNames: string[];
arn: string;
resource: string[];
+ retries: number;
}): Promise<SimulationResult[]> => {
- if (options.actionNames.includes(iam.GetUser)) {
- try {
- await getIamClient(options.region).send(new GetUserCommand({}));
- const result: SimulationResult[] = [
- {
- decision: 'allowed',
- name: iam.GetUser,
- },
- ];
- return result;
- } catch (err) {
- const result: SimulationResult[] = [
- {
- decision: 'explicitDeny',
- name: iam.GetUser,
- },
- ];
- return result;
+ try {
+ if (options.actionNames.includes(iam.GetUser)) {
+ try {
+ await getIamClient(options.region).send(new GetUserCommand({}));
+ const result: SimulationResult[] = [
+ {
+ decision: 'allowed',
+ name: iam.GetUser,
+ },
+ ];
+ return result;
+ } catch (err) {
+ const result: SimulationResult[] = [
+ {
+ decision: 'explicitDeny',
+ name: iam.GetUser,
+ },
+ ];
+ return result;
+ }
}
- }
- const res = await getIamClient(options.region).send(
- new SimulatePrincipalPolicyCommand({
- ActionNames: options.actionNames,
- PolicySourceArn: options.arn,
- ResourceArns: options.resource,
- })
- );
+ const res = await getIamClient(options.region).send(
+ new SimulatePrincipalPolicyCommand({
+ ActionNames: options.actionNames,
+ PolicySourceArn: options.arn,
+ ResourceArns: options.resource,
+ })
+ );
+
+ return (res.EvaluationResults ?? []).map((pol): SimulationResult => {
+ return {
+ decision: pol.EvalDecision as EvalDecision,
+ name: pol.EvalActionName as string,
+ };
+ });
+ } catch (err) {
+ // Sometimes the AWS Rate limit hits. In that case we retry up to two times
+ // after waiting for 2 seconds.
+ if (options.retries <= 0) {
+ throw err;
+ }
- return (res.EvaluationResults ?? []).map((pol): SimulationResult => {
- return {
- decision: pol.EvalDecision as EvalDecision,
- name: pol.EvalActionName as string,
- };
- });
+ await new Promise((resolve) => {
+ setTimeout(() => resolve, 2000);
+ });
+ return simulateRule({...options, retries: options.retries - 1});
+ }
};