Skip to content

Commit 1156b2a

Browse files
Add Bedrock runtime classification support
- Add SSM params for classification model ID and prompt template - Add bedrock_runtime_invoke IAM policy - Rename bedrock_invoke to bedrock_data_automation_invoke - Remove document splitting override from BDA config
1 parent aec7268 commit 1156b2a

File tree

3 files changed

+79
-15
lines changed

3 files changed

+79
-15
lines changed

infra/app-docai/app-config/env-config/document_data_extraction.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ locals {
5252
}
5353
}
5454

55-
override_configuration = {
56-
document = {
57-
splitter = {
58-
state = "ENABLED"
59-
}
60-
}
61-
}
55+
# override_configuration = {
56+
# document = {
57+
# splitter = {
58+
# state = "ENABLED"
59+
# }
60+
# }
61+
# }
6262

6363
} : null
6464
}

infra/app-docai/service/document_data_extraction.tf

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ module "documentai" {
8787
source = "../../modules/document-data-extraction/resources"
8888

8989
standard_output_configuration = local.document_data_extraction_config.standard_output_configuration
90-
override_configuration = local.document_data_extraction_config.override_configuration
9190
tags = local.tags
9291

9392
blueprints = concat(
@@ -350,6 +349,44 @@ resource "aws_dynamodb_table" "document_batches" {
350349
tags = local.tags
351350
}
352351

352+
#-------------------
353+
# Bedrock Classification Config (SSM)
354+
#-------------------
355+
resource "aws_ssm_parameter" "bedrock_classification_model_id" {
356+
count = local.document_data_extraction_config != null ? 1 : 0
357+
358+
name = "/service/${local.service_name}/bedrock/classification-model-id"
359+
type = "String"
360+
value = "anthropic.claude-3-haiku-20240307-v1:0"
361+
362+
lifecycle {
363+
ignore_changes = [value]
364+
}
365+
}
366+
367+
# <<DOCUMENT_TYPES>> in the classification prompt needs to be dynamically
368+
# replaced with the document types that BDA is configured to extract. Store prompt
369+
# in SSM Parameter Store; application reads and update it at runtime.
370+
resource "aws_ssm_parameter" "bedrock_classification_prompt" {
371+
count = local.document_data_extraction_config != null ? 1 : 0
372+
373+
name = "/service/${local.service_name}/bedrock/classification-prompt"
374+
type = "String"
375+
value = <<-EOT
376+
Analyze this image. Respond in JSON only:
377+
{"document_type": "string", "confidence": float 0-1, "document_count": int}
378+
ONLY use one of these exact values for document_type: <<DOCUMENT_TYPES>>
379+
Do not create new categories. If unsure, use 'other_document'.
380+
If it's not a document, use 'not_a_document'.
381+
document_count: how many separate documents are visible in this image?
382+
EOT
383+
384+
lifecycle {
385+
ignore_changes = [value]
386+
}
387+
}
388+
389+
353390
#-------------------
354391
# IAM Policies
355392
#-------------------
@@ -385,7 +422,7 @@ resource "aws_iam_policy" "dynamodb_read_write" {
385422
})
386423
}
387424

388-
resource "aws_iam_policy" "bedrock_invoke" {
425+
resource "aws_iam_policy" "bedrock_data_automation_invoke" {
389426
count = local.document_data_extraction_config != null ? 1 : 0
390427

391428
name = "${local.prefix}bedrock-invoke"
@@ -403,4 +440,29 @@ resource "aws_iam_policy" "bedrock_invoke" {
403440
Effect = "Allow"
404441
}]
405442
})
443+
}
444+
445+
446+
resource "aws_iam_policy" "bedrock_runtime_invoke" {
447+
count = local.document_data_extraction_config != null ? 1 : 0
448+
449+
name = "${local.prefix}bedrock-runtime-invoke"
450+
policy = jsonencode({
451+
Version = "2012-10-17"
452+
Statement = [
453+
{
454+
Action = "bedrock:InvokeModel"
455+
Resource = "arn:aws:bedrock:${data.aws_region.current.name}::foundation-model/*"
456+
Effect = "Allow"
457+
},
458+
{
459+
Action = "ssm:GetParameter"
460+
Resource = [
461+
aws_ssm_parameter.bedrock_classification_model_id[0].arn,
462+
aws_ssm_parameter.bedrock_classification_prompt[0].arn,
463+
]
464+
Effect = "Allow"
465+
}
466+
]
467+
})
406468
}

infra/app-docai/service/main.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ module "service" {
134134
storage_access = module.storage.access_policy_arn
135135
},
136136
module.app_config.enable_document_data_extraction ? {
137-
documentai_input_bucket_access = module.documentai_input_bucket[0].access_policy_arn
138-
documentai_output_bucket_access = module.documentai_output_bucket[0].access_policy_arn,
139-
documentai_metrics_bucket_access = module.documentai_metrics_bucket[0].access_policy_arn,
140-
documentai_bedrock_access = module.documentai[0].access_policy_arn,
141-
documentai_dynamodb_access = aws_iam_policy.dynamodb_read_write[0].arn
142-
documentai_sqs_send_message = aws_iam_policy.sqs_send_message[0].arn
137+
documentai_input_bucket_access = module.documentai_input_bucket[0].access_policy_arn
138+
documentai_output_bucket_access = module.documentai_output_bucket[0].access_policy_arn,
139+
documentai_metrics_bucket_access = module.documentai_metrics_bucket[0].access_policy_arn,
140+
documentai_bedrock_access = module.documentai[0].access_policy_arn,
141+
documentai_dynamodb_access = aws_iam_policy.dynamodb_read_write[0].arn
142+
documentai_sqs_send_message = aws_iam_policy.sqs_send_message[0].arn
143+
documentai_bedrock_data_automation_invoke = aws_iam_policy.bedrock_data_automation_invoke[0].arn
144+
documentai_bedrock_runtime_invoke = aws_iam_policy.bedrock_runtime_invoke[0].arn
143145
} : {},
144146
module.app_config.enable_identity_provider ? {
145147
identity_provider_access = module.identity_provider_client[0].access_policy_arn,

0 commit comments

Comments
 (0)