Skip to content

Commit

Permalink
[CAI-242] - Keep lambda warm by invoking it every 3 minutes (#1215)
Browse files Browse the repository at this point in the history
* feat: keep lambda warm by invoking it every 3 minutes

* fix: add missing permissions to lambda

* chore: add changeset

* fix: eslint cloudfront functions
  • Loading branch information
christian-calabrese authored Oct 29, 2024
1 parent bcba266 commit 30fbfe5
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-items-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"infrastructure": patch
---

Chatbot lambda is now kept warm by invoking it every 3 minutes
12 changes: 8 additions & 4 deletions apps/cloudfront-functions/src/viewer-request-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ const handler = (
const isHomepage = uri === '/';

// List of special cases (add more as needed)
const specialCases: string[] = ['.bollo'];
const specialCases: readonly string[] = ['.bollo'];

// Function to check if URI ends with any of the special cases
const isSpecialCase = specialCases.some(caseExt => uri.endsWith(caseExt));
const isSpecialCase = specialCases.some((caseExt) => uri.endsWith(caseExt));

if (!isHomepage) {
if (uriEndsWithSlash) {
request.uri = uri.replace(/\/$/, '');
}
// Always add .html if there's no file extension, including special cases
if (!isGitbookAssets && !isWoff2 && (!/\.[a-zA-Z]+$/.test(uri) || isSpecialCase)) {
if (
!isGitbookAssets &&
!isWoff2 &&
(!/\.[a-zA-Z]+$/.test(uri) || isSpecialCase)
) {
request.uri += '.html';
}
}
Expand All @@ -40,4 +44,4 @@ const handler = (
// do nothing
return event.request;
}
};
};
7 changes: 6 additions & 1 deletion apps/infrastructure/src/modules/chatbot/data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ data "aws_iam_policy_document" "lambda_dynamodb_policy" {
"dynamodb:UpdateItem",
"dynamodb:GetRecords"
]
resources = [module.dynamodb_chatbot_queries.dynamodb_table_arn, module.dynamodb_chatbot_sessions.dynamodb_table_arn]
resources = [
module.dynamodb_chatbot_queries.dynamodb_table_arn,
"${module.dynamodb_chatbot_queries.dynamodb_table_arn}/*",
module.dynamodb_chatbot_sessions.dynamodb_table_arn,
"${module.dynamodb_chatbot_sessions.dynamodb_table_arn}/*"
]
}
}

Expand Down
27 changes: 27 additions & 0 deletions apps/infrastructure/src/modules/chatbot/lambda_chatbot.tf
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,30 @@ module "index_id_ssm_parameter" {
secure_type = true
ignore_value_changes = true
}

# Invoke the lambda function every 3 minutes from 6:00 am to 11:00 pm to keep it warm
resource "aws_cloudwatch_event_rule" "lambda_invocation_rule" {
name = "${local.prefix}-lambda-invocation-rule"
schedule_expression = "cron(0/3 6-23 * * ? *)"
}

resource "aws_cloudwatch_event_target" "lambda_target" {
rule = aws_cloudwatch_event_rule.lambda_invocation_rule.name
target_id = "keep-chatbot-lambda-warm"
arn = module.lambda_function.lambda_function_arn
input = jsonencode({
resource = "/",
path = "/",
httpMethod = "OPTIONS",
requestContext = {},
multiValueQueryStringParameters = null
})
}

resource "aws_lambda_permission" "allow_eventbridge" {
action = "lambda:InvokeFunction"
function_name = module.lambda_function.lambda_function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.lambda_invocation_rule.arn
statement_id = "AllowExecutionFromEventBridge"
}

0 comments on commit 30fbfe5

Please sign in to comment.