This gist is part of a tutorial by MakeOps on how to invoke Amazon Bedrock from an AWS Lambda function.
Created
September 11, 2025 16:06
-
-
Save jasonforte/8106e979ca19af75979b0b7652550914 to your computer and use it in GitHub Desktop.
Deploy AWS Lambda Python Webhook to Invoke Amazon Bedrock
This file contains hidden or 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
| import json | |
| import boto3 | |
| bedrock = boto3.client('bedrock-runtime') | |
| def lambda_handler(event, context): | |
| # TODO implement | |
| print(json.dumps(event)) | |
| prompt = json.loads(event['body'])['prompt'] | |
| system_list = [ | |
| { | |
| "text": "Act as a creative writing assistant. When the user provides you with a topic, write a short story about that topic." | |
| } | |
| ] | |
| message_list = [{"role": "user", "content": [{"text": prompt}]}] | |
| request_body = { | |
| "schemaVersion": "messages-v1", | |
| "messages": message_list, | |
| "system": system_list, | |
| "inferenceConfig": {"maxTokens": 500, "topP": 0.9, "topK": 20, "temperature": 0.7}, | |
| } | |
| response = bedrock.invoke_model( | |
| body=json.dumps(request_body), | |
| contentType='application/json', | |
| modelId='eu.amazon.nova-lite-v1:0', | |
| ) | |
| print(response) | |
| response_obj = { | |
| "status": response['ResponseMetadata']['HTTPStatusCode'], | |
| "body": json.loads(response['body'].read().decode('utf-8')) | |
| } | |
| return { | |
| 'statusCode': 200, | |
| 'body': response_obj | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment