📌 Part 2: Creating AWS Lambda Function and Role for SageMaker Endpoint Invocation (with Screenshot Guides)

1. Create IAM Role for Lambda

Purpose: This role allows the Lambda function to invoke the SageMaker endpoint.

Steps:

✅ Screenshot Tip:\

✅ Screenshot the Create role page → Select Lambda as trusted entity


2. Attach SageMaker Invoke Permission Policy

Purpose: Grant the Lambda function permission to invoke the SageMaker endpoint.

Steps:

{
    "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.

✅ Screenshot Tip:\

✅ Screenshot the JSON policy creation step showing the custom policy\

✅ Screenshot the attached policies before creating the role

✅ Screenshot Tip:\

✅ Screenshot the Role ARN after creation


3. Create AWS Lambda Function

Purpose: Lambda serves as the backend to accept requests and forward them to the SageMaker endpoint.

Steps:

✅ Screenshot Tip:\

✅ Screenshot the Create Lambda Function screen showing name, runtime, and role


4. Lambda Function Code

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.