Skip to content

Commit a9c898b

Browse files
committed
[PRMP-1048] the infrastructure
1 parent 14f55b5 commit a9c898b

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Concurrency control schedules
2+
# Office hour start
3+
resource "aws_cloudwatch_event_rule" "bulk_upload_concurrency_office_hours_start" {
4+
name = "bulk-upload-office-hours-start"
5+
schedule_expression = "cron(0 9 * * ? *)"
6+
}
7+
8+
resource "aws_cloudwatch_event_target" "bulk_upload_concurrency_office_hours_start" {
9+
rule = aws_cloudwatch_event_rule.bulk_upload_concurrency_office_hours_start.name
10+
target_id = "office-hours-start"
11+
arn = module.concurrency_controller.arn
12+
13+
input = jsonencode({
14+
targetFunction = var.bulk_upload_lambda_name
15+
reservedConcurrency = var.office_hours_start_concurrency
16+
})
17+
}
18+
19+
# Office hours stop
20+
resource "aws_cloudwatch_event_rule" "bulk_upload_concurrency_office_hours_stop" {
21+
name = "bulk-upload-office-hours-stop"
22+
schedule_expression = "cron(0 17 * * ? *)"
23+
}
24+
25+
resource "aws_cloudwatch_event_target" "bulk_upload_concurrency_office_hours_stop" {
26+
rule = aws_cloudwatch_event_rule.bulk_upload_office_hours_stop.name
27+
target_id = "office-hours-stop"
28+
arn = module.concurrency_controller.arn
29+
30+
input = jsonencode({
31+
targetFunction = var.bulk_upload_lambda_name
32+
reservedConcurrency = var.office_hours_end_concurrency
33+
})
34+
}
35+
36+
# Concurrency control triggers
37+
# Concurrency freeze during ECS deploy
38+
resource "aws_cloudwatch_event_rule" "bulk_upload_concurrency_deploy" {
39+
name = "bulk-upload-concurrency-deploy"
40+
event_pattern = jsonencode({
41+
source = ["deploy.pipeline"]
42+
detail-type = ["freeze-concurrency"]
43+
})
44+
}
45+
46+
resource "aws_cloudwatch_event_target" "bulk_upload_concurrency_deploy" {
47+
rule = aws_cloudwatch_event_rule.bulk_upload_concurrency_deploy.name
48+
target_id = "freeze-concurrency"
49+
arn = module.concurrency_controller.arn
50+
51+
input = jsonencode({
52+
targetFunction = var.bulk_upload_lambda_name
53+
reservedConcurrency = 0
54+
})
55+
}
56+
57+
# Restore concurrency after release
58+
resource "aws_cloudwatch_event_rule" "bulk_upload_concurrency_release_restore" {
59+
name = "bulk-upload-concurrency-release-restore"
60+
event_pattern = jsonencode({
61+
source = ["release.pipeline"]
62+
detail-type = ["restore-bulk-upload-concurrency"]
63+
})
64+
}
65+
66+
resource "aws_cloudwatch_event_target" "bulk_upload_concurrency_release_restore" {
67+
rule = aws_cloudwatch_event_rule.bulk_upload_concurrency_release_restore.name
68+
target_id = "restore-bulk-upload-concurrency"
69+
arn = module.concurrency_controller.arn
70+
71+
input = jsonencode({
72+
targetFunction = var.bulk_upload_lambda_name
73+
reservedConcurrency = local.bulk_upload_lambda_concurrent_limit
74+
})
75+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
data "aws_iam_policy_document" "concurrency_controller_policy" {
3+
statement {
4+
effect = "Allow"
5+
actions = [
6+
"lambda:PutFunctionConcurrency",
7+
"lambda:GetFunctionConcurrency"
8+
]
9+
resources = [
10+
module.bulk-upload-lambda.lambda_arn
11+
]
12+
}
13+
}
14+
15+
module "concurrency_controller" {
16+
source = "./modules/lambda"
17+
name = "ConcurrencyController"
18+
handler = "handlers.concurrency_controller_handler.lambda_handler"
19+
20+
#This lambda is an orchestrator so should have unlimited conc
21+
reserved_concurrent_executions = -1
22+
23+
is_gateway_integration_needed = false
24+
is_invoked_from_gateway = false
25+
26+
iam_role_policy_documents = [
27+
data.aws_iam_policy_document.concurrency_controller_policy.json
28+
]
29+
}
30+
31+
resource "aws_lambda_permission" "office_hours_start_permission" {
32+
statement_id = "AllowEventBridgeOfficeHoursStart"
33+
action = "lambda:InvokeFunction"
34+
function_name = module.concurrency_controller.lambda_function_name
35+
principal = "events.amazonaws.com"
36+
source_arn = aws_cloudwatch_event_rule.bulk_upload_concurrency_office_hours_start.arn
37+
}
38+
39+
resource "aws_lambda_permission" "office_hours_stop_permission" {
40+
statement_id = "AllowEventBridgeOfficeHoursStop"
41+
action = "lambda:InvokeFunction"
42+
function_name = module.concurrency_controller.lambda_function_name
43+
principal = "events.amazonaws.com"
44+
source_arn = aws_cloudwatch_event_rule.bulk_upload_concurrency_office_hours_stop.arn
45+
}
46+
47+
resource "aws_lambda_permission" "deploy_permission" {
48+
statement_id = "AllowEventBridgeDeploy"
49+
action = "lambda:InvokeFunction"
50+
function_name = module.concurrency_controller.lambda_function_name
51+
principal = "events.amazonaws.com"
52+
source_arn = aws_cloudwatch_event_rule.bulk_upload_concurrency_deploy.arn
53+
}
54+
55+
resource "aws_lambda_permission" "release_restore_permission" {
56+
statement_id = "AllowEventBridgeReleaseRestore"
57+
action = "lambda:InvokeFunction"
58+
function_name = module.concurrency_controller.lambda_function_name
59+
principal = "events.amazonaws.com"
60+
source_arn = aws_cloudwatch_event_rule.bulk_upload_concurrency_release_restore.arn
61+
}

infrastructure/schedules.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,6 @@ resource "aws_lambda_permission" "toggle_bulk_upload_disable_permission" {
167167
principal = "events.amazonaws.com"
168168
source_arn = aws_cloudwatch_event_rule.bulk_upload_disable_rule.arn
169169
}
170+
171+
172+

infrastructure/variable.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,21 @@ variable "kms_deletion_window" {
314314
type = number
315315
default = 30
316316
}
317+
318+
# Concurrency Controller
319+
320+
variable "bulk_upload_lambda_name" {
321+
type = string
322+
}
323+
324+
variable "office_hours_start_concurrency" {
325+
type = number
326+
default = 1
327+
}
328+
329+
variable "office_hours_end_concurrency" {
330+
type = number
331+
default = 3
332+
}
333+
334+

0 commit comments

Comments
 (0)