-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
793f8b6
commit d712d43
Showing
6 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
] | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters