-
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
04a2d55
commit b86e642
Showing
6 changed files
with
201 additions
and
45 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,74 @@ | ||
locals { | ||
lambda_function_name = "levio-esta-bedrock-invoker" | ||
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:*:*:*" | ||
] | ||
} | ||
|
||
bedrock_invoke = { | ||
effect = "Allow" | ||
actions = [ | ||
"bedrock:InvokeModel" | ||
] | ||
resources = [ | ||
"arn:aws:bedrock:*:${data.aws_caller_identity.current.account_id}:model/*" | ||
] | ||
} | ||
|
||
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,48 @@ | ||
import boto3 | ||
import json | ||
from botocore.exceptions import BotoCoreError, ClientError | ||
|
||
s3 = boto3.client('s3') | ||
bedrock = boto3.client('bedrock') | ||
|
||
def lambda_handler(event, context): | ||
s3_arn = event['s3_arn'] | ||
bedrock_params = event['bedrock_params'] | ||
prompt = event['prompt'] | ||
|
||
# Parse the S3 ARN to get the bucket and key | ||
bucket, key = s3_arn.split(':::')[1].split('/') | ||
|
||
# Download the file from S3 | ||
try: | ||
s3_object = s3.get_object(Bucket=bucket, Key=key) | ||
except ClientError as e: | ||
return { | ||
'statusCode': 400, | ||
'body': str(e) | ||
} | ||
|
||
# Extract text from the S3 object | ||
extracted_text = s3_object['Body'].read().decode('utf-8') | ||
|
||
# Invoke the Bedrock model with the extracted text and the provided parameters | ||
try: | ||
response = bedrock.invoke_model( | ||
ModelName=bedrock_params['model_name'], | ||
Payload=json.dumps({ | ||
'master': bedrock_params['master'], | ||
'prompt': prompt, | ||
'message': extracted_text | ||
}) | ||
) | ||
except BotoCoreError as e: | ||
return { | ||
'statusCode': 400, | ||
'body': str(e) | ||
} | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': 'Successfully processed the S3 ARN', | ||
'bedrockResponse': response | ||
} |
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
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