From d712d43c589173376e2348d2dfa294a247407747 Mon Sep 17 00:00:00 2001 From: Joel Balcaen Date: Fri, 26 Apr 2024 11:02:37 -0300 Subject: [PATCH] new lamda --- lambdas/document_filler/lambda.tf | 64 ++++++++++++++++++++ lambdas/document_filler/output.tf | 7 +++ lambdas/document_filler/src/index.py | 37 +++++++++++ lambdas/document_filler/src/requirements.txt | 1 + lambdas/document_filler/variables.tf | 15 +++++ terraform/modules.tf | 8 +++ 6 files changed, 132 insertions(+) create mode 100644 lambdas/document_filler/lambda.tf create mode 100644 lambdas/document_filler/output.tf create mode 100644 lambdas/document_filler/src/index.py create mode 100644 lambdas/document_filler/src/requirements.txt create mode 100644 lambdas/document_filler/variables.tf diff --git a/lambdas/document_filler/lambda.tf b/lambdas/document_filler/lambda.tf new file mode 100644 index 0000000..49ea866 --- /dev/null +++ b/lambdas/document_filler/lambda.tf @@ -0,0 +1,64 @@ +locals { + lambda_function_name = "levio-esta-document-filler" + timeout = 30 + runtime = "python3.11" + powertools_layer_arn = "arn:aws:lambda:${var.aws_region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:67" +} + +data "aws_caller_identity" "current" {} + + +module "lambda_function_container_image" { + source = "terraform-aws-modules/lambda/aws" + function_name = local.lambda_function_name + handler = "index.lambda_handler" + publish = true + runtime = local.runtime + timeout = local.timeout + layers = [local.powertools_layer_arn] + source_path = "${path.module}/src" + s3_bucket = var.lambda_storage_bucket + memory_size = 256 + role_name = "${local.lambda_function_name}-role" + attach_policy_statements = true + + policy_statements = { + log_group = { + effect = "Allow" + actions = [ + "logs:CreateLogGroup" + ] + resources = [ + "arn:aws:logs:*:*:*" + ] + } + + s3 = { + effect = "Allow" + actions = [ + "s3:Get*", + "s3:List*", + "s3:Describe*", + "s3:PutObject", + "s3-object-lambda:Get*", + "s3-object-lambda:List*", + "s3-object-lambda:WriteGetObjectResponse" + ] + resources = var.allowed_s3_resources + } + + log_write = { + effect = "Allow" + + resources = [ + "arn:aws:logs:*:*:log-group:/aws/${local.lambda_function_name}/*:*" + ] + + actions = [ + "logs:CreateLogStream", + "logs:PutLogEvents", + ] + } + + } +} diff --git a/lambdas/document_filler/output.tf b/lambdas/document_filler/output.tf new file mode 100644 index 0000000..cd2e3af --- /dev/null +++ b/lambdas/document_filler/output.tf @@ -0,0 +1,7 @@ +output "lambda_function_arn" { + value = module.lambda_function_container_image.lambda_function_arn +} + +output "lambda_function_name" { + value = module.lambda_function_container_image.lambda_function_name +} diff --git a/lambdas/document_filler/src/index.py b/lambdas/document_filler/src/index.py new file mode 100644 index 0000000..7393037 --- /dev/null +++ b/lambdas/document_filler/src/index.py @@ -0,0 +1,37 @@ +import boto3 +from docx import Document +from io import BytesIO + +s3 = boto3.client('s3') + +def lambda_handler(event, context): + """ + Downloads the given docx and for each replacement item it replaces the matching document_key with the text_to_fill + """ + s3_arn = event['doc_s3_arn'] + replacements = event['replacements'] + + bucket_name = s3_arn.split(':')[5].split('/')[0] + key = '/'.join(s3_arn.split(':')[5].split('/')[1:]) + + print(f"Download bucket: {bucket_name}, key: {key}") + + file_obj = s3.get_object(Bucket=bucket_name, Key=key) + file_content = file_obj['Body'].read() + + doc = Document(BytesIO(file_content)) + + for paragraph in doc.paragraphs: + for replacement in replacements: + if replacement['document_key'] in paragraph.text: + paragraph.text = paragraph.text.replace(replacement['document_key'], replacement['text_to_fill']) + + output_stream = BytesIO() + doc.save(output_stream) + + s3.put_object(Bucket=bucket_name, Key=key, Body=output_stream.getvalue()) + + return { + 'statusCode': 200, + 'body': f'Successfully modified {key} and uploaded to {bucket_name}' + } \ No newline at end of file diff --git a/lambdas/document_filler/src/requirements.txt b/lambdas/document_filler/src/requirements.txt new file mode 100644 index 0000000..7a6646f --- /dev/null +++ b/lambdas/document_filler/src/requirements.txt @@ -0,0 +1 @@ +docx \ No newline at end of file diff --git a/lambdas/document_filler/variables.tf b/lambdas/document_filler/variables.tf new file mode 100644 index 0000000..45ce8a5 --- /dev/null +++ b/lambdas/document_filler/variables.tf @@ -0,0 +1,15 @@ +variable "lambda_storage_bucket" { + type = string + nullable = false +} + +variable "aws_region" { + type = string + nullable = false +} + +variable "allowed_s3_resources" { + type = list(string) + nullable = false + description = "values for the s3 resources that the lambda function can access" +} \ No newline at end of file diff --git a/terraform/modules.tf b/terraform/modules.tf index ecae810..d966afb 100644 --- a/terraform/modules.tf +++ b/terraform/modules.tf @@ -275,4 +275,12 @@ module "bedrock_invoker" { allowed_s3_resources = [module.s3_bucket.s3_bucket_arn, "${module.s3_bucket.s3_bucket_arn}/*"] } +module "levio_esta_document_filler" { + source = "../lambdas/document_filler" + lambda_storage_bucket = aws_s3_bucket.lambda_storage.id + aws_region = var.aws_region + allowed_s3_resources = [module.s3_bucket.s3_bucket_arn, "${module.s3_bucket.s3_bucket_arn}/*"] +} + +