- Newest
- Most votes
- Most comments
import com.amazonaws.services.lambda.AWSLambdaClientBuilder import com.amazonaws.services.lambda.model.InvokeRequest import com.amazonaws.auth.DefaultAWSCredentialsProviderChain import org.json.JSONObject // ... (in your activity or fragment) fun invokeLambdaWithUserInput(userPrompt: String) { Thread { try { val lambdaClient = AWSLambdaClientBuilder.standard() .withCredentials(DefaultAWSCredentialsProviderChain()) .withRegion("us-east-1") // replace with your Lambda's region .build() val payload = JSONObject() payload.put("userPrompt", userPrompt) val invokeRequest = InvokeRequest() .withFunctionName("myNovaReelFunction") .withPayload(payload.toString()) val invokeResult = lambdaClient.invoke(invokeRequest) // Handle the result val resultJson = JSONObject(String(invokeResult.payload.array())) // Process the result as needed } catch (e: Exception) { e.printStackTrace() } }.start() } // Usage val userPrompt = inputFieldvc.text.toString() invokeLambdaWithUserInput(userPrompt)
This code creates a Lambda client, constructs a JSON payload with the user's input, and invokes the Lambda function with this payload.
- AWS Lambda Function:
Modify your Lambda function to expect the user input in the event parameter instead of reading from environment variables:
import json import boto3 bedrock_runtime = boto3.client("bedrock-runtime") def lambda_handler(event, context): # Extract user prompt from the event user_prompt = event.get('userPrompt', '') model_input = { "taskType": "TEXT_VIDEO", "textToVideoParams": { "text": user_prompt }, "videoGenerationConfig": { "durationSeconds": 6, "fps": 24, "dimension": "1280x720", "seed": 33, }, } try: invocation = bedrock_runtime.start_async_invoke( modelId="amazon.nova-reel-v1:1", modelInput=model_input, outputDataConfig={ "s3OutputDataConfig": { "s3Uri": "s3://novabucket" } } ) 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}) }
This approach allows you to pass the user's input directly to the Lambda function as part of the event, eliminating the need to update environment variables. The Lambda function now extracts the user's prompt from the event parameter instead of reading it from an environment variable.
Remember to update your Lambda function's IAM role to ensure it has the necessary permissions to interact with Amazon Bedrock and S3.
Sources
AWS Lambda env. var. change. | AWS re:Post
Define Lambda function handler in Java - AWS Lambda
Define Lambda function handler in Python - AWS Lambda
Relevant content
- asked 9 months ago
- asked 10 months ago
- AWS OFFICIALUpdated 5 years ago
