Skip to content

Not sure why my lambda function wont work..

0

import json import boto3

bedrock_runtime = boto3.client("bedrock-runtime")

def lambda_handler(event, context): # Parse the input prompts from the event body body = json.loads(event['body']) prompts = body.get('prompts', [])

Prepare the model input for multi-shot manual mode

model_input = { "taskType": "MULTI_SHOT_MANUAL", "multiShotManualParams": { "shots": [ {"text": "aerial view of a city with tall glass and metal skyscrapers"}, {"text": "closeup of a vehicle wheel in motion as the pavement speeds by with motion blur"}, {"text": "tracking shot, the vehicle drives through the city, trees and buildings line the street"}, ] }, "videoGenerationConfig": { "seed": 1234, "fps": 24, "dimension": "1280x720", }, }

try:
    # Start the asynchronous video generation job
    invocation = bedrock_runtime.start_async_invoke(
        modelId="amazon.nova-reel-v1:1",
        #modelInput=model_input,
        modelInput=json.dumps(model_input),
        outputDataConfig={
            "s3OutputDataConfig": {
                "s3Uri": "s3://novabucket"
            }
        }
    )

    # Extract the invocation ID and construct the future object key
    invocation_id = invocation['invocationArn'].split('/')[-1]
    future_object_key = f"{invocation_id}/output.mp4"

    return {
        "statusCode": 200,
        "body": json.dumps({
            "invocationArn": invocation.get('invocationArn'),
            "requestId": invocation.get('requestId'),
            "futureObjectKey": future_object_key
        })
    }

except Exception as e:
    message = str(e)
    return {
        "statusCode": 500,
        "body": json.dumps({"error": message})
    }

here is an error code. Function Logs: [ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': unexpected indent (lambda_function.py, line 21) Traceback (most recent call last):   File "/var/task/lambda_function.py" Line 21         try:

asked a year ago153 views

1 Answer
0
Accepted Answer

Here's the corrected version of your Lambda function:

import json
import boto3

bedrock_runtime = boto3.client("bedrock-runtime")

def lambda_handler(event, context):
    # Parse the input prompts from the event body
    body = json.loads(event['body'])
    prompts = body.get('prompts', [])
    
    # Prepare the model input for multi-shot manual mode
    model_input = {
        "taskType": "MULTI_SHOT_MANUAL",
        "multiShotManualParams": {
            "shots": [
                {"text": "aerial view of a city with tall glass and metal skyscrapers"},
                {"text": "closeup of a vehicle wheel in motion as the pavement speeds by with motion blur"},
                {"text": "tracking shot, the vehicle drives through the city, trees and buildings line the street"},
            ]
        },
        "videoGenerationConfig": {
            "seed": 1234,
            "fps": 24,
            "dimension": "1280x720",
        },
    }

    try:
        # Start the asynchronous video generation job
        invocation = bedrock_runtime.start_async_invoke(
            modelId="amazon.nova-reel-v1:1",
            modelInput=json.dumps(model_input),
            outputDataConfig={
                "s3OutputDataConfig": {
                    "s3Uri": "s3://novabucket"
                }
            }
        )

        # Extract the invocation ID and construct the future object key
        invocation_id = invocation['invocationArn'].split('/')[-1]
        future_object_key = f"{invocation_id}/output.mp4"

        return {
            "statusCode": 200,
            "body": json.dumps({
                "invocationArn": invocation.get('invocationArn'),
                "requestId": invocation.get('requestId'),
                "futureObjectKey": future_object_key
            })
        }

    except Exception as e:
        message = str(e)
        return {
            "statusCode": 500,
            "body": json.dumps({"error": message})
        }

The main issue was with the indentation of the model_input dictionary and the subsequent code. I've corrected the indentation to ensure all elements are properly aligned.

Also, note that I've uncommented the modelInput=json.dumps(model_input) line and removed the commented-out modelInput=model_input line. This is because the start_async_invoke method expects the modelInput to be a JSON string.

Make sure to copy this corrected version into your Lambda function. Pay close attention to the indentation, especially for nested structures like dictionaries and lists. In Python, consistent indentation is crucial for the code to work correctly.

If you encounter any other issues after fixing the indentation, please provide the new error message, and I'll be happy to help further.

Sources
Lambda function returns constructed key properly, but cant Parse it. | AWS re:Post
Accessing Amazon Bedrock Claude-3-Haiku and Claude-3-Sonnet models via Lambda Functions | AWS re:Post

answered a year ago

EXPERT

reviewed a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.