Skip to content

Commit 37164f9

Browse files
authored
Merge pull request #4 from isovalent/pr/bkrasko/update-webhook-lambda-to-set-region-from-sqs-url
feat: Update webhook lambda to set the region from sqs url
2 parents 1aa3722 + d39375e commit 37164f9

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

  • lambdas/functions/webhook/src/sqs

lambdas/functions/webhook/src/sqs/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { createChildLogger, getTracedAWSV3Client } from '@aws-github-runner/aws-
44

55
const logger = createChildLogger('sqs');
66

7+
const sqsClientsByRegion = new Map<string, SQS>();
8+
79
export interface ActionRequestMessage {
810
id: number;
911
eventType: string;
@@ -32,7 +34,8 @@ export interface GithubWorkflowEvent {
3234
}
3335

3436
export const sendActionRequest = async (message: ActionRequestMessage): Promise<void> => {
35-
const sqs = getTracedAWSV3Client(new SQS({ region: process.env.AWS_REGION }));
37+
const region = getRegionFromQueueUrl(message.queueId) ?? process.env.AWS_REGION;
38+
const sqs = getSqsClient(region);
3639

3740
const sqsMessage: SendMessageCommandInput = {
3841
QueueUrl: message.queueId,
@@ -43,3 +46,30 @@ export const sendActionRequest = async (message: ActionRequestMessage): Promise<
4346

4447
await sqs.sendMessage(sqsMessage);
4548
};
49+
50+
function getSqsClient(region: string | undefined): SQS {
51+
if (!region) {
52+
return getTracedAWSV3Client(new SQS({}));
53+
}
54+
55+
const cached = sqsClientsByRegion.get(region);
56+
if (cached) {
57+
return cached;
58+
}
59+
60+
const client = getTracedAWSV3Client(new SQS({ region }));
61+
sqsClientsByRegion.set(region, client);
62+
return client;
63+
}
64+
65+
function getRegionFromQueueUrl(queueUrl: string): string | undefined {
66+
try {
67+
const url = new URL(queueUrl);
68+
const parts = url.hostname.split('.');
69+
if (parts.length >= 3 && parts[0] === 'sqs') {
70+
return parts[1];
71+
}
72+
} catch {}
73+
74+
return undefined;
75+
}

0 commit comments

Comments
 (0)