Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(iac): add queue approxiate age of oldest message alarm (#375)

Will Hutchinson54fb72253c3b
diff --git a/infra/packages/resources/src/index.ts b/infra/packages/resources/src/index.ts
index 41d8ff2bf9..1d94976ea9 100644
--- a/infra/packages/resources/src/index.ts
+++ b/infra/packages/resources/src/index.ts
@@ -14,6 +14,7 @@ export {
export { createImage } from './resources/image';
export { serviceLoadBalancer } from './resources/load_balancer';
export { Queue } from './resources/queue';
+export { QueueAlarms } from './resources/queue_alarms';
export { Database } from './resources/rds';
export { Redis } from './resources/redis';
export {
diff --git a/infra/packages/resources/src/resources/queue.ts b/infra/packages/resources/src/resources/queue.ts
index 186de70852..de906d7321 100644
--- a/infra/packages/resources/src/resources/queue.ts
+++ b/infra/packages/resources/src/resources/queue.ts
@@ -1,6 +1,7 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
+import { QueueAlarms } from './queue_alarms';
type Args = {
// The maximum receive count of a message before it's sent to DLQ
@@ -65,7 +66,7 @@ export class Queue extends pulumi.ComponentResource {
queueName,
{
name: queueName,
- redrivePolicy: this.dlq.arn.apply((arn) =>
+ redrivePolicy: this.dlq.arn.apply((arn: string) =>
JSON.stringify({
deadLetterTargetArn: arn,
maxReceiveCount: maxReceiveCount ?? 5,
@@ -77,5 +78,11 @@ export class Queue extends pulumi.ComponentResource {
},
{ parent: this, dependsOn: [this.dlq] }
);
+
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
}
}
diff --git a/infra/packages/resources/src/resources/queue_alarms.ts b/infra/packages/resources/src/resources/queue_alarms.ts
new file mode 100644
index 0000000000..f9f310aaf5
--- /dev/null
+++ b/infra/packages/resources/src/resources/queue_alarms.ts
@@ -0,0 +1,61 @@
+import * as aws from '@pulumi/aws';
+import * as pulumi from '@pulumi/pulumi';
+import { CLOUD_TRAIL_SNS_TOPIC_ARN } from '@shared';
+
+type Args = {
+ // The queue to create alarms for
+ queue: aws.sqs.Queue;
+ // The tags to apply to the alarms
+ tags: { [key: string]: string };
+ // The evaluation periods for the alarm
+ // Defaults to 60s
+ approximateAgeOfOldestMessageEvaluationPeriods?: number;
+ // The threshold for the alarm
+ // Defaults to 120s
+ approximateAgeOfOldestMessageThreshold?: number;
+};
+
+/**
+ * @description Creates default queue alarms for a provided queue.
+ */
+export class QueueAlarms extends pulumi.ComponentResource {
+ queueApproximateAgeOfOldestMessageAlarm: aws.cloudwatch.MetricAlarm;
+ constructor(
+ name: string,
+ args: Args,
+ opts?: pulumi.ComponentResourceOptions
+ ) {
+ super('my:components:QueueAlarms', name, { tags: args.tags }, opts);
+
+ const { queue } = args;
+
+ const tags = { ...args.tags, queue: queue.name };
+
+ const approximateAgeOfOldestMessageEvaluationPeriods =
+ args.approximateAgeOfOldestMessageEvaluationPeriods ?? 60;
+ const approximateAgeOfOldestMessageThreshold =
+ args.approximateAgeOfOldestMessageThreshold ?? 120; // 2 minutes
+
+ // alarm for monitoring ApproximateAgeOfOldestMessage
+ this.queueApproximateAgeOfOldestMessageAlarm =
+ new aws.cloudwatch.MetricAlarm(
+ 'aaoom-alarm',
+ {
+ alarmDescription: `Alarm when ${queue.name} has approximate age of oldest message over ${approximateAgeOfOldestMessageThreshold}s for ${approximateAgeOfOldestMessageEvaluationPeriods}s.`,
+ comparisonOperator: 'GreaterThanThreshold',
+ evaluationPeriods: 1,
+ metricName: 'ApproximateAgeOfOldestMessage',
+ namespace: 'AWS/SQS',
+ period: approximateAgeOfOldestMessageEvaluationPeriods,
+ statistic: 'Average',
+ threshold: approximateAgeOfOldestMessageThreshold,
+ dimensions: {
+ QueueName: queue.name,
+ },
+ alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
+ tags,
+ },
+ { parent: this }
+ );
+ }
+}
diff --git a/infra/stacks/bulk-upload/upload-queue.ts b/infra/stacks/bulk-upload/upload-queue.ts
index 3faea8151d..fb6ad23fff 100644
--- a/infra/stacks/bulk-upload/upload-queue.ts
+++ b/infra/stacks/bulk-upload/upload-queue.ts
@@ -1,5 +1,6 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
import { UPLOAD_EXTRACTOR_LAMBDA_TIMEOUT_SECONDS } from './upload-extractor-lambda-handler';
@@ -64,5 +65,11 @@ export class BulkUploadQueue extends pulumi.ComponentResource {
},
{ parent: this, dependsOn: [this.dlq] }
);
+
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
}
}
diff --git a/infra/stacks/cloud-storage-service/delete-chat-handler.ts b/infra/stacks/cloud-storage-service/delete-chat-handler.ts
index f70a9f4c31..adfa89a673 100644
--- a/infra/stacks/cloud-storage-service/delete-chat-handler.ts
+++ b/infra/stacks/cloud-storage-service/delete-chat-handler.ts
@@ -1,11 +1,12 @@
import { Lambda } from '@lambda';
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
-const LAMBA_BASE_NAME = 'delete_chat_handler';
+const LAMBDA_BASE_NAME = 'delete_chat_handler';
const CLOUD_STORAGE_BASE = `../../../rust/cloud-storage`;
-const ZIP_LOCATION = `${CLOUD_STORAGE_BASE}/target/lambda/${LAMBA_BASE_NAME}/bootstrap.zip`;
+const ZIP_LOCATION = `${CLOUD_STORAGE_BASE}/target/lambda/${LAMBDA_BASE_NAME}/bootstrap.zip`;
export type DeleteChatHandlerEnvVars = {
DATABASE_URL: pulumi.Output<string> | string;
@@ -41,7 +42,7 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
// Create queue and DLQ
this.dlq = new aws.sqs.Queue(
- `${LAMBA_BASE_NAME}-dlq-${stack}`,
+ `${LAMBDA_BASE_NAME}-dlq-${stack}`,
{
name: `delete-chat-handler-dlq-${stack}`,
messageRetentionSeconds: 1209600,
@@ -53,7 +54,7 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
new aws.cloudwatch.MetricAlarm(
'dlq-alarm',
{
- name: `${LAMBA_BASE_NAME}-dlq-alarm-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-dlq-alarm-${stack}`,
comparisonOperator: 'GreaterThanThreshold',
evaluationPeriods: 1,
metricName: 'ApproximateNumberOfMessagesVisible',
@@ -71,7 +72,7 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
);
this.queue = new aws.sqs.Queue(
- `${LAMBA_BASE_NAME}-queue-${stack}`,
+ `${LAMBDA_BASE_NAME}-queue-${stack}`,
{
name: `delete-chat-handler-queue-${stack}`,
redrivePolicy: this.dlq.arn.apply((arn) =>
@@ -85,8 +86,14 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
{ parent: this, dependsOn: [this.dlq] }
);
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
+
const sqsPolicy = new aws.iam.Policy(
- `${LAMBA_BASE_NAME}-sqs-policy`,
+ `${LAMBDA_BASE_NAME}-sqs-policy`,
{
policy: pulumi.output({
Version: '2012-10-17',
@@ -108,9 +115,9 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
);
this.role = new aws.iam.Role(
- `${LAMBA_BASE_NAME}-role`,
+ `${LAMBDA_BASE_NAME}-role`,
{
- name: `${LAMBA_BASE_NAME}-role-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-role-${stack}`,
assumeRolePolicy: JSON.stringify({
Version: '2012-10-17',
Statement: [
@@ -136,9 +143,9 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
);
const deleteChatLambda = new Lambda<DeleteChatHandlerEnvVars>(
- `${LAMBA_BASE_NAME}-lambda`,
+ `${LAMBDA_BASE_NAME}-lambda`,
{
- baseName: LAMBA_BASE_NAME,
+ baseName: LAMBDA_BASE_NAME,
handlerBase: CLOUD_STORAGE_BASE,
zipLocation: ZIP_LOCATION,
vpc,
@@ -181,9 +188,9 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
setupLambdaAlarms() {
new aws.cloudwatch.MetricAlarm(
- `${LAMBA_BASE_NAME}-throttle-alarm`,
+ `${LAMBDA_BASE_NAME}-throttle-alarm`,
{
- name: `${LAMBA_BASE_NAME}-throttle-count-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-throttle-count-${stack}`,
metricName: 'Throttles',
namespace: 'AWS/Lambda',
statistic: 'Sum',
@@ -194,7 +201,7 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
dimensions: {
FunctionName: this.lambda.name,
},
- alarmDescription: `Alarm when ${LAMBA_BASE_NAME} lambda experiences throttling.`,
+ alarmDescription: `Alarm when ${LAMBDA_BASE_NAME} lambda experiences throttling.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
@@ -203,9 +210,9 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
);
new aws.cloudwatch.MetricAlarm(
- `${LAMBA_BASE_NAME}-error-alarm`,
+ `${LAMBDA_BASE_NAME}-error-alarm`,
{
- name: `${LAMBA_BASE_NAME}-error-count-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-error-count-${stack}`,
metricName: 'Errors',
namespace: 'AWS/Lambda',
statistic: 'Sum',
@@ -216,7 +223,7 @@ export class DeleteChatHandler extends pulumi.ComponentResource {
dimensions: {
FunctionName: this.lambda.name,
},
- alarmDescription: `Alarm when ${LAMBA_BASE_NAME} lambda experiences errors.`,
+ alarmDescription: `Alarm when ${LAMBDA_BASE_NAME} lambda experiences errors.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
diff --git a/infra/stacks/cloud-storage-service/delete-document-handler.ts b/infra/stacks/cloud-storage-service/delete-document-handler.ts
index 8f3a1ca8ae..24bfad8bea 100644
--- a/infra/stacks/cloud-storage-service/delete-document-handler.ts
+++ b/infra/stacks/cloud-storage-service/delete-document-handler.ts
@@ -3,6 +3,7 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
const BASE_NAME = 'delete-document-handler';
@@ -70,5 +71,11 @@ export class DeleteDocumentHandler extends pulumi.ComponentResource {
},
{ parent: this, dependsOn: [this.dlq] }
);
+
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
}
}
diff --git a/infra/stacks/document-text-extractor/document-text-extractor.ts b/infra/stacks/document-text-extractor/document-text-extractor.ts
index 32c3f77d77..0b8dbbd222 100644
--- a/infra/stacks/document-text-extractor/document-text-extractor.ts
+++ b/infra/stacks/document-text-extractor/document-text-extractor.ts
@@ -1,6 +1,7 @@
import { Lambda } from '@lambda';
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
const BASE_NAME = 'document-text-extractor';
@@ -95,6 +96,12 @@ export class DocumentTextExtractorLambda extends pulumi.ComponentResource {
{ parent: this }
);
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
+
this.role = new aws.iam.Role(
`${BASE_NAME}-role`,
{
diff --git a/infra/stacks/organization-retention/organization-retention-handler.ts b/infra/stacks/organization-retention/organization-retention-handler.ts
index de8b44c17c..5a37ebb8bf 100644
--- a/infra/stacks/organization-retention/organization-retention-handler.ts
+++ b/infra/stacks/organization-retention/organization-retention-handler.ts
@@ -1,11 +1,12 @@
import { Lambda } from '@lambda';
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
-const LAMBA_BASE_NAME = 'organization_retention_handler';
+const LAMBDA_BASE_NAME = 'organization_retention_handler';
const CLOUD_STORAGE_BASE = `../../../rust/cloud-storage`;
-const ZIP_LOCATION = `${CLOUD_STORAGE_BASE}/target/lambda/${LAMBA_BASE_NAME}/bootstrap.zip`;
+const ZIP_LOCATION = `${CLOUD_STORAGE_BASE}/target/lambda/${LAMBDA_BASE_NAME}/bootstrap.zip`;
export type OrganizationRetentionhandlerEnvVars = {
DATABASE_URL: pulumi.Output<string> | string;
@@ -46,7 +47,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
// Create queue and DLQ
this.dlq = new aws.sqs.Queue(
- `${LAMBA_BASE_NAME}-dlq-${stack}`,
+ `${LAMBDA_BASE_NAME}-dlq-${stack}`,
{
name: `organization-retention-handler-dlq-${stack}`,
messageRetentionSeconds: 1209600,
@@ -58,7 +59,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
new aws.cloudwatch.MetricAlarm(
'dlq-alarm',
{
- name: `${LAMBA_BASE_NAME}-dlq-alarm-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-dlq-alarm-${stack}`,
comparisonOperator: 'GreaterThanThreshold',
evaluationPeriods: 1,
metricName: 'ApproximateNumberOfMessagesVisible',
@@ -76,7 +77,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
);
this.queue = new aws.sqs.Queue(
- `${LAMBA_BASE_NAME}-queue-${stack}`,
+ `${LAMBDA_BASE_NAME}-queue-${stack}`,
{
name: `organization-retention-handler-queue-${stack}`,
redrivePolicy: this.dlq.arn.apply((arn) =>
@@ -90,8 +91,14 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
{ parent: this, dependsOn: [this.dlq] }
);
+ new QueueAlarms(
+ 'queue-alarms',
+ { queue: this.queue, tags },
+ { parent: this }
+ );
+
const sqsPolicy = new aws.iam.Policy(
- `${LAMBA_BASE_NAME}-sqs-policy`,
+ `${LAMBDA_BASE_NAME}-sqs-policy`,
{
policy: pulumi.output({
Version: '2012-10-17',
@@ -113,7 +120,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
);
const sqsSendPolicy = new aws.iam.Policy(
- `${LAMBA_BASE_NAME}-sqs-send-policy`,
+ `${LAMBDA_BASE_NAME}-sqs-send-policy`,
{
policy: pulumi.output({
Version: '2012-10-17',
@@ -134,9 +141,9 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
);
this.role = new aws.iam.Role(
- `${LAMBA_BASE_NAME}-role`,
+ `${LAMBDA_BASE_NAME}-role`,
{
- name: `${LAMBA_BASE_NAME}-role-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-role-${stack}`,
assumeRolePolicy: JSON.stringify({
Version: '2012-10-17',
Statement: [
@@ -164,9 +171,9 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
const organizationRetentionhandlerLambda =
new Lambda<OrganizationRetentionhandlerEnvVars>(
- `${LAMBA_BASE_NAME}-lambda`,
+ `${LAMBDA_BASE_NAME}-lambda`,
{
- baseName: LAMBA_BASE_NAME,
+ baseName: LAMBDA_BASE_NAME,
handlerBase: CLOUD_STORAGE_BASE,
zipLocation: ZIP_LOCATION,
vpc,
@@ -207,9 +214,9 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
setupLambdaAlarms() {
new aws.cloudwatch.MetricAlarm(
- `${LAMBA_BASE_NAME}-throttle-alarm`,
+ `${LAMBDA_BASE_NAME}-throttle-alarm`,
{
- name: `${LAMBA_BASE_NAME}-throttle-count-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-throttle-count-${stack}`,
metricName: 'Throttles',
namespace: 'AWS/Lambda',
statistic: 'Sum',
@@ -220,7 +227,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
dimensions: {
FunctionName: this.lambda.name,
},
- alarmDescription: `Alarm when ${LAMBA_BASE_NAME} lambda experiences throttling.`,
+ alarmDescription: `Alarm when ${LAMBDA_BASE_NAME} lambda experiences throttling.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
@@ -229,9 +236,9 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
);
new aws.cloudwatch.MetricAlarm(
- `${LAMBA_BASE_NAME}-error-alarm`,
+ `${LAMBDA_BASE_NAME}-error-alarm`,
{
- name: `${LAMBA_BASE_NAME}-error-count-${stack}`,
+ name: `${LAMBDA_BASE_NAME}-error-count-${stack}`,
metricName: 'Errors',
namespace: 'AWS/Lambda',
statistic: 'Sum',
@@ -242,7 +249,7 @@ export class OrganizationRetentionHandler extends pulumi.ComponentResource {
dimensions: {
FunctionName: this.lambda.name,
},
- alarmDescription: `Alarm when ${LAMBA_BASE_NAME} lambda experiences errors.`,
+ alarmDescription: `Alarm when ${LAMBDA_BASE_NAME} lambda experiences errors.`,
actionsEnabled: true,
alarmActions: [CLOUD_TRAIL_SNS_TOPIC_ARN],
tags: this.tags,
diff --git a/infra/stacks/search-text-extractor-queue/index.ts b/infra/stacks/search-text-extractor-queue/index.ts
index c4206d149f..504c6ea589 100644
--- a/infra/stacks/search-text-extractor-queue/index.ts
+++ b/infra/stacks/search-text-extractor-queue/index.ts
@@ -1,5 +1,6 @@
import * as aws from '@pulumi/aws';
import * as pulumi from '@pulumi/pulumi';
+import { QueueAlarms } from '@resources';
import { CLOUD_TRAIL_SNS_TOPIC_ARN, stack } from '@shared';
const tags = {
@@ -35,7 +36,6 @@ if (stack === 'prod') {
});
}
-// TODO: we may need to tweak the queues settings here
const queue = new aws.sqs.Queue(
'queue',
{
@@ -53,5 +53,7 @@ const queue = new aws.sqs.Queue(
{ dependsOn: [dlq] }
);
+new QueueAlarms('queue-alarms', { queue: queue, tags });
+
export const searchTextExtractorQueueArn = pulumi.interpolate`${queue.arn}`;
export const searchTextExtractorQueueName = pulumi.interpolate`${queue.name}`;
diff --git a/infra/stacks/static-file-service/static-file-service.ts b/infra/stacks/static-file-service/static-file-service.ts
index 279a8baabb..79ec95e28a 100644
--- a/infra/stacks/static-file-service/static-file-service.ts
+++ b/infra/stacks/static-file-service/static-file-service.ts
@@ -5,6 +5,7 @@ import {
DATADOG_API_KEY,
datadogAgentContainer,
fargateLogRouterSidecarContainer,
+ QueueAlarms,
serviceLoadBalancer,
} from '@resources';
import { ALLOWED_ORIGINS } from '@resources/resources/cors';
@@ -356,6 +357,7 @@ export class StaticFileService extends pulumi.ComponentResource {
},
],
});
+
const queueQueue = new aws.sqs.Queue('queue', {
name: queueName,
policy: queue.apply((queue) => queue.json),
@@ -363,6 +365,16 @@ export class StaticFileService extends pulumi.ComponentResource {
tags: this.tags,
});
+ new QueueAlarms(
+ 'queue-alarms',
+ {
+ queue: queueQueue,
+ approximateAgeOfOldestMessageThreshold: 240,
+ tags: this.tags,
+ },
+ { parent: this }
+ );
+
new aws.s3.BucketNotification('bucket_notification', {
bucket: staticFilesBucket.id,
queues: [