Skip to content

Commit 9d35e72

Browse files
[PRMT-462] Schedule start and stop for bulk upload ingestion (#349)
* PRMT-462- added infrastructure to allow enabling/disabling of bulk upload ingestion * PRMT-462- run pre commit * PRMT-462- updated call to iam * PRMT-462- run pre commit * PRMT-462- added code for debugging * PRMT-462- precommit * PRMT-462- updated setup * PRMT-462- fixed typo * PRMT-462- cleaned code * PRMT-462- renaming * PRMT-463- removed TODO * PRMT-462- merged and formated lines
1 parent 980b843 commit 9d35e72

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

infrastructure/iam.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ resource "aws_iam_policy" "s3_document_data_policy_for_ods_report_lambda" {
176176
})
177177
}
178178

179+
data "aws_iam_policy_document" "lambda_toggle_bulk_upload_document" {
180+
statement {
181+
effect = "Allow"
182+
183+
actions = [
184+
"lambda:UpdateEventSourceMapping",
185+
"lambda:GetEventSourceMapping"
186+
]
187+
188+
resources = [
189+
aws_lambda_event_source_mapping.bulk_upload_lambda.arn
190+
]
191+
}
192+
}
193+
194+
resource "aws_iam_policy" "lambda_toggle_bulk_upload_policy" {
195+
name = "${terraform.workspace}_lambda_toggle_bulk_upload_policy"
196+
policy = data.aws_iam_policy_document.lambda_toggle_bulk_upload_document.json
197+
}
198+
199+
179200
data "aws_iam_policy_document" "assume_role_policy_for_ods_report_lambda" {
180201
statement {
181202
actions = ["sts:AssumeRole"]

infrastructure/lambda-bulk-upload.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ module "bulk-upload-lambda" {
5555
]
5656
}
5757

58-
5958
resource "aws_lambda_event_source_mapping" "bulk_upload_lambda" {
60-
event_source_arn = module.sqs-lg-bulk-upload-metadata-queue.endpoint
59+
event_source_arn = module.sqs-lg-bulk-upload-metadata-queue.sqs_arn
6160
function_name = module.bulk-upload-lambda.lambda_arn
62-
61+
enabled = false # Disabled by default; scheduler lambda will control
62+
batch_size = 10
6363
scaling_config {
6464
maximum_concurrency = local.bulk_upload_lambda_concurrent_limit
6565
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module "toggle-bulk-upload-lambda" {
2+
source = "./modules/lambda"
3+
name = "ToggleBulkUploadLambda"
4+
handler = "handlers.toggle_bulk_upload_handler.lambda_handler"
5+
lambda_timeout = 60
6+
memory_size = 128
7+
8+
iam_role_policy_documents = [
9+
data.aws_iam_policy_document.lambda_toggle_bulk_upload_document.json
10+
]
11+
12+
lambda_environment_variables = {
13+
ESM_UUID = aws_lambda_event_source_mapping.bulk_upload_lambda.uuid
14+
}
15+
16+
is_gateway_integration_needed = false
17+
is_invoked_from_gateway = false
18+
}
19+

infrastructure/modules/lambda/variable.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ variable "lambda_environment_variables" {
2222
variable "rest_api_id" {
2323
description = "ID of the associated API Gateway REST API."
2424
type = string
25+
default = ""
2526
}
2627

2728
variable "resource_id" {
@@ -51,6 +52,7 @@ variable "http_methods" {
5152
variable "api_execution_arn" {
5253
description = "Execution ARN of the API Gateway used for granting invoke permissions."
5354
type = string
55+
default = ""
5456
}
5557

5658
variable "iam_role_policy_documents" {

infrastructure/schedules.tf

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,44 @@ resource "aws_lambda_permission" "nhs_oauth_token_generator_schedule" {
184184
principal = "events.amazonaws.com"
185185
source_arn = aws_cloudwatch_event_rule.nhs_oauth_token_generator_schedule.arn
186186
}
187+
resource "aws_cloudwatch_event_rule" "bulk_upload_enable_rule" {
188+
name = "${terraform.workspace}_bulk_upload_enable"
189+
description = "Enable Bulk Upload ingestion"
190+
schedule_expression = "cron(0 19 ? * MON-FRI *)"
191+
}
192+
193+
resource "aws_cloudwatch_event_rule" "bulk_upload_disable_rule" {
194+
name = "${terraform.workspace}_bulk_upload_disable"
195+
description = "Disable Bulk Upload ingestion"
196+
schedule_expression = "cron(0 7 ? * TUE-SAT *)"
197+
}
198+
199+
resource "aws_cloudwatch_event_target" "bulk_upload_enable_target" {
200+
rule = aws_cloudwatch_event_rule.bulk_upload_enable_rule.name
201+
target_id = "toggle-bulk-upload-enable"
202+
arn = module.toggle-bulk-upload-lambda.lambda_arn
203+
input = jsonencode({ action = "enable" })
204+
}
205+
206+
resource "aws_cloudwatch_event_target" "bulk_upload_disable_target" {
207+
rule = aws_cloudwatch_event_rule.bulk_upload_disable_rule.name
208+
target_id = "toggle-bulk-upload-disable"
209+
arn = module.toggle-bulk-upload-lambda.lambda_arn
210+
input = jsonencode({ action = "disable" })
211+
}
212+
213+
resource "aws_lambda_permission" "toggle_bulk_upload_enable_permission" {
214+
statement_id = "AllowExecutionFromCloudWatchEnable"
215+
action = "lambda:InvokeFunction"
216+
function_name = module.toggle-bulk-upload-lambda.function_name
217+
principal = "events.amazonaws.com"
218+
source_arn = aws_cloudwatch_event_rule.bulk_upload_enable_rule.arn
219+
}
220+
221+
resource "aws_lambda_permission" "toggle_bulk_upload_disable_permission" {
222+
statement_id = "AllowExecutionFromCloudWatchDisable"
223+
action = "lambda:InvokeFunction"
224+
function_name = module.toggle-bulk-upload-lambda.function_name
225+
principal = "events.amazonaws.com"
226+
source_arn = aws_cloudwatch_event_rule.bulk_upload_disable_rule.arn
227+
}

0 commit comments

Comments
 (0)