Problem:
I have a Sagemaker Studio notebook that runs on its own end-to-end when this is scheduled using the “Schedule Notebook” feature. This notebook is NOT used for ML/inferencing input data. The output of the notebook is actually sending emails using AWS Simple Email Service. I’d like to build a AWS Lambda function where if the function is invoked, the function would invoke the Studio notebook to run. I wasn’t able to find the exact topic within SO and I wanted to ask the community if they could provide guidance/pointers on what needs to be included in my Lambda script (i.e.: lambda_handler(event, content) function). I know for inferencing, you could create a custom model and create an endpoint that the AWS Lambda function could tap into, but this isn’t a model and I’m hoping for a simpler way to do this.
In the past, I’ve used the sagemaker_run_notebook
library to invoke notebook runs from another .ipynb notebook, but not from AWS Lambda. I’m not an expert in python nor a DevOps engineer and would greatly appreciate it if someone could provide a simple Lambda function to run the below .ipynb inside my Sagemaker Studio.
As an example of my Studio notebook (.ipynb), this is basically the content minus the extensive information being attached to the email body:
import boto3
from botocore.exceptions import ClientError
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
# Replace [email protected] with your "From" address.
SENDER = "<[email protected]>"
# Replace [email protected] with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENT = ['[email protected]']
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-east-1"
# The subject line for the email.
SUBJECT = f"EMAIL SUBJECT"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ""
# The HTML body of the email.
BODY_HTML = """
<html>
<head></head>
<body>
</body>
</html>
"""
# The character encoding for the email.
CHARSET = "utf-8"
# Create a new SES resource and specify a region.
client = boto3.client('ses',region_name=AWS_REGION)
# Create a multipart/mixed parent container.
msg = MIMEMultipart('mixed')
msg['Subject'] = SUBJECT
msg['From'] = SENDER
msg['To'] = ', '.join(RECIPIENT)
# Create a multipart/alternative child container.
msg_body = MIMEMultipart('alternative')
# Encode the text and HTML content and set the character encoding. This step is
# necessary if you're sending a message with characters outside the ASCII range.
textpart = MIMEText(BODY_TEXT.encode(CHARSET), 'plain', CHARSET)
htmlpart = MIMEText(BODY_HTML.encode(CHARSET), 'html', CHARSET)
# Add the text and HTML parts to the child container.
msg_body.attach(textpart)
msg_body.attach(htmlpart)
# Attach the multipart/alternative child container to the multipart/mixed
# parent container.
msg.attach(msg_body)
try:
response = client.send_raw_email(
Source=SENDER,
Destinations = RECIPIENT,
RawMessage={
'Data':msg.as_string(),
}
#ConfigurationSetName=CONFIGURATION_SET
)
# Display an error if something goes wrong.
except ClientError as e:
print(e.response['Error']['Message'])
else:
print("Email sent! Message ID:"),
print(response['MessageId'])
I’ve tried going through the invoke Sagemaker endpoints documentations but I’m hoping for a simpler solution because this isn’t necessarily a ML model and inferencing is not involved. The output is also an email instead of a value being returned from the AWS Lambda function/endpoint.
slee12 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.