-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
205 lines (156 loc) · 5.39 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Name: AWS Generative AI Backed IDP Solution
Author: HK Transfield, 2024
This configuration deploys an Intelligent Document Processing (IDP) solution
within in an AWS environment. The object of an IDP solution is to reduce the
overall costs associated with typical document workflows.
At a high level, the solution performs the following functions:
- Upload documents to an S3 bucket, triggering an asynchronous
Textract detection job
- Classify and enrich extracted text using AI/ML
- Store results in another S3 bucket
- Automate validation and review steps
- Faciliate human reviews through A2I when necessary
- Store verified data in a fully managed NoSQL database service
made available for downstream apps
*/
provider "aws" {
region = "us-east-1"
}
resource "random_string" "this" {
length = 16
special = false
upper = false
}
locals {
project_org = "hkt"
project_tag = "idp"
project_name = "${local.project_org}-${local.project_tag}"
}
################################################################################
# DOCUMENT INGESTION AND TEXT EXTRACTION
################################################################################
locals {
iam_entity_name = "AllowLambdaTextractAsyncJob"
updates_name = "${local.project_name}-textract-job"
}
module "input_documents" {
source = "./modules/document-storage"
bucket_name = "hkt-idp-input-documents"
force_destroy = true
}
# allow lambda to start textract jobs and access input documents bucket
data "aws_iam_policy_document" "allow_lambda_textract_async_job" {
statement {
sid = "EnableCloudWatchLogs"
effect = "Allow"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
]
resources = ["arn:aws:logs:*:*:*"]
}
statement {
sid = "AllowTextractAsynchronousDetectionJobs"
effect = "Allow"
actions = [
"textract:StartDocumentTextDetection",
"textract:GetDocumentTextDetection",
"s3:GetObject",
"s3:PutObject"
]
resources = ["${module.input_documents.bucket_arn}/*"]
}
}
module "textract_events" {
source = "./modules/textract-event-handler"
sns_topic_name = "${local.updates_name}-topic"
sqs_queue_name = "${local.updates_name}-queue"
}
# Lamda function to start Textract async detection job
module "textract_updates_lambda_function" {
source = "./modules/lambda-function-builder"
lambda_filename = "1-start-textract-async-detection-job"
environment_variables = {
SNS_TOPIC_ARN = module.textract_events.sns_topic_arn
SNS_ROLE_ARN = module.textract_events.sns_iam_role_arn
}
iam_role_name = local.iam_entity_name
iam_policy_name = local.iam_entity_name
iam_policy_json = data.aws_iam_policy_document.allow_lambda_textract_async_job.json
}
################################################################################
# DOCUMENT CLASSIFICATION
################################################################################
resource "aws_lambda_event_source_mapping" "textract_output" {
event_source_arn = module.textract_events.sqs_queue_arn
function_name = module.process_documents_lambda.function_arn
batch_size = 10
}
data "aws_iam_policy_document" "lambda_exec_policy" {
statement {
sid = "InvokeLambdaFunction"
effect = "Allow"
actions = [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
]
resources = [module.textract_events.sqs_queue_arn]
}
statement {
sid = "ReadTextractOutput"
effect = "Allow"
actions = [
"textract:DetectDocumentText",
"s3:PutObject",
"s3:GetObject"
]
resources = ["*"]
}
statement {
sid = "InvokeBedrockModel"
effect = "Allow"
actions = [
"bedrock:InvokeModel"
]
resources = ["*"]
}
}
module "classify_textract_output_lambda_function" {
source = "./modules/lambda-function-builder"
lambda_filename = "2-classify-textract-output"
environment_variables = {
OUTPUT_BUCKET = aws_s3_bucket.classified_documents.bucket
}
iam_role_name = "AllowLambdaEnrichDocumentContent"
iam_policy_name = "AllowLambdaEnrichDocumentContent"
iam_policy_json = data.aws_iam_policy_document.lambda_exec_policy.json
}
module "classified_documents" {
source = "./modules/document-storage"
bucket_name = "${local.project_name}-classfied-documents"
force_destroy = true
}
################################################################################
# ENTITY EXTRACTION AND CONTENT ENRICHMENT
################################################################################
module "entity_extraction_and_content_enrichment" {
source = "./modules/lambda-function-builder"
lambda_filename = "3-entity-extraction-and-content-enrichment"
environment_variables = {
OUTPUT_BUCKET = aws_s3_bucket.enriched_documents.bucket
}
iam_role_name = "AllowLambdaEnrichDocumentContent"
iam_policy_name = "AllowLambdaEnrichDocumentContent"
iam_policy_json = data.aws_iam_policy_document.lambda_exec_policy.json
}
################################################################################
# RESULTS VALIDATION
################################################################################
module "extracted_data_and_enriched_documents" {
source = "./modules/document-storage"
bucket_name = "${local.project_name}-extracted-data-and-enriched-documents"
force_destroy = true
}