Purpose: This role allows the Lambda function to invoke the SageMaker endpoint.
✅ Screenshot Tip:\
✅ Screenshot the Create role page → Select Lambda as trusted entity
Purpose: Grant the Lambda function permission to invoke the SageMaker endpoint.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sagemaker:InvokeEndpoint"
],
"Resource": [
"arn:aws:sagemaker:us-east-2:YOUR_ACCOUNT_ID:endpoint/lamini-t5-gpu-endpoint"
]
}
]
}
✅ Note: Replace the
Resource
value with the endpoint ARN you obtained in Part 1.
SageMakerInvokePolicy
, and click Create Policy.✅ Screenshot Tip:\
✅ Screenshot the JSON policy creation step showing the custom policy\
✅ Screenshot the attached policies before creating the role
generate-text-lamini-role
and create it.✅ Screenshot Tip:\
✅ Screenshot the Role ARN after creation
Purpose: Lambda serves as the backend to accept requests and forward them to the SageMaker endpoint.
generate-text-lamini
Python 3.10
generate-text-lamini-role
.✅ Screenshot Tip:\
✅ Screenshot the Create Lambda Function screen showing name, runtime, and role
In the Function code section of your Lambda function, replace the default code with the following:
import boto3
import json
client = boto3.client('sagemaker-runtime')
ENDPOINT_NAME = 'lamini-t5-gpu-endpoint' # Name of your SageMaker endpoint
def lambda_handler(event, context):
try:
prompt = event.get('prompt', '')
if not prompt:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Prompt not provided'})
}
response = client.invoke_endpoint(
EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=json.dumps({"inputs": prompt})
)
result = json.loads(response['Body'].read().decode())
return {
'statusCode': 200,
'body': json.dumps(result)
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}
✅ Screenshot Tip:\
✅ Screenshot the Function code editor with the pasted code
✅ Screenshot Tip:\
✅ Screenshot after clicking Deploy
✅ Summary Checklist:
✅ Next Step: Proceed to Part 3 to configure API Gateway and expose the Lambda function via a public HTTP endpoint.