Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbalcaen committed May 2, 2024
1 parent eddf75e commit 86d7ca8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
1 change: 0 additions & 1 deletion lambdas/bedrock_invoker/src/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def lambda_handler(event, context):
]
}

print(f"Invoke bedrock with this body: ", claude_body)
bedrock_model = 'anthropic.claude-3-sonnet-20240229-v1:0'
print(f"Invoke bedock with this model: ", bedrock_model)
print(f"Invoke claude with system prompt: ", system_prompt)
Expand Down
1 change: 0 additions & 1 deletion lambdas/document_filler/src/requirements.txt

This file was deleted.

8 changes: 7 additions & 1 deletion lambdas/email_sender/src/index.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import boto3
from botocore.exceptions import NoCredentialsError
from email.mime.multipart import MIMEMultipart
Expand All @@ -7,6 +8,7 @@
def lambda_handler(event, context):
"""
Downloads the attachment_s3_arns into memory and add them as attachments before sending the email with the given params.
body is an array of json strings with the following format: {type: "plain/html/xml/csv", message: "Hello World"}
attachment_s3_arns can be in URI or ARN format
"""
sender_email = event['sender_email']
Expand All @@ -22,7 +24,11 @@ def lambda_handler(event, context):
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = destination_email
msg.attach(MIMEText(body, 'plain'))

for mime_text_json in body:
mime_text_dict = json.loads(mime_text_json)
mime_text = MIMEText(mime_text_dict['message'], mime_text_dict['type'])
msg.attach(mime_text)

for attachment in attachment_s3_arns:
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
locals {
lambda_function_name = "levio-esta-document-filler"
lambda_function_name = "levio-esta-text-replacer"
timeout = 30
runtime = "python3.11"
powertools_layer_arn = "arn:aws:lambda:${var.aws_region}:017000801446:layer:AWSLambdaPowertoolsPythonV2:67"
Expand All @@ -21,17 +21,17 @@ module "lambda_function_container_image" {
role_name = "${local.lambda_function_name}-role"
attach_policy_statements = true
# see https://github.com/terraform-aws-modules/terraform-aws-lambda/issues/346 on why this custom path is necessary
source_path = [
{
path = "${path.module}/src"
commands = [
":zip",
"cd `mktemp -d`",
"python3 -m pip install --no-compile --only-binary=:all: --platform=manylinux2014_x86_64 --target=. -r ${abspath(path.module)}/src/requirements.txt",
":zip .",
]
}
]
# source_path = [
# {
# path = "${path.module}/src"
# commands = [
# ":zip",
# "cd `mktemp -d`",
# "python3 -m pip install --no-compile --only-binary=:all: --platform=manylinux2014_x86_64 --target=. -r ${abspath(path.module)}/src/requirements.txt",
# ":zip .",
# ]
# }
# ]

policy_statements = {
log_group = {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
import boto3
from docx import Document
import re
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 replacement_key with the replacement_text
Downloads the given text file (txt, html, json, csv, xml, js, py, md etc) and for each replacement item it replaces the matching replacement_key with the replacement_text
"""
try:
s3_arn = event['doc_s3_arn']
replacements = event['replacements']

s3_bucket, s3_key = s3_arn.replace("s3://", "").replace("arn:aws:s3:::", "").split("/", 1)


print(f"Download bucket: {s3_bucket}, key: {s3_key}")

file_obj = s3.get_object(Bucket=s3_bucket, Key=s3_key)

file_content = file_obj['Body'].read()
file_content = file_obj['Body'].read().decode('utf-8')

doc = Document(BytesIO(file_content))

for paragraph in doc.paragraphs:
for replacement in replacements:
if replacement['replacement_key'] in paragraph.text:
print(f"replacing {replacement['replacement_key']} with {replacement['replacement_text'][:10]}")
paragraph.text = paragraph.text.replace(replacement['replacement_key'], replacement['replacement_text'])
for replacement in replacements:
print(f"replacing {replacement['replacement_key']} with {replacement['replacement_text'][:10]}")
file_content = re.sub(re.escape(replacement['replacement_key']), replacement['replacement_text'], file_content)

output_stream = BytesIO()
doc.save(output_stream)
output_stream.write(file_content.encode('utf-8'))

s3.put_object(Bucket=s3_bucket, Key=s3_key, Body=output_stream.getvalue())

Expand All @@ -44,4 +38,4 @@ def lambda_handler(event, context):
return {
'statusCode': 400,
'body': str(e)
}
}
File renamed without changes.
4 changes: 2 additions & 2 deletions terraform/modules.tf
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ 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"
module "levio_esta_text_replacer" {
source = "../lambdas/text_replacer"
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}/*"]
Expand Down

0 comments on commit 86d7ca8

Please sign in to comment.