async function getSqsClientForSoftOptIns()

in typescript/src/utils/aws.ts [46:83]


async function getSqsClientForSoftOptIns(): Promise<Sqs> {
  const now = new Date();
  if (
    !SOISqsClient ||
    !lastAssumedSOI ||
    now.getTime() - lastAssumedSOI.getTime() >= 1800000
  ) {
    // refresh every 30 minutes
    const membershipAccountId = await getMembershipAccountId();
    const sts = new STS();

    const softOptInConsentSetterStage = Stage === 'PROD' ? 'PROD' : 'CODE';

    const assumeRoleResult = await sts
      .assumeRole({
        RoleArn: `arn:aws:iam::${membershipAccountId}:role/membership-${softOptInConsentSetterStage}-soft-opt-in-consent-setter-QueueCrossAccountRole`,
        RoleSessionName: 'CrossAccountSession',
      })
      .promise();

    const credentials = assumeRoleResult.Credentials;

    if (!credentials) {
      throw Error('credentials undefined in getSqsClientForSoftOptIns');
    }

    SOISqsClient = new Sqs({
      accessKeyId: credentials.AccessKeyId,
      secretAccessKey: credentials.SecretAccessKey,
      sessionToken: credentials.SessionToken,
      region: Region,
    });

    lastAssumedSOI = now;
  }

  return SOISqsClient;
}