async function getSqsClientForComms()

in typescript/src/utils/aws.ts [85:120]


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

    const assumeRoleResult = await sts
      .assumeRole({
        RoleArn: `arn:aws:iam::${membershipAccountId}:role/comms-${Stage}-EmailQueueCrossAccountRole`,
        RoleSessionName: 'CrossAccountSession',
      })
      .promise();

    const credentials = assumeRoleResult.Credentials;

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

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

    lastAssumedComms = now;
  }

  return commsSqsClient;
}