Accessing Lambda and S3 in deployments
You can configure Modelbit deployments to have access to your AWS resources. First, set
up a Trust relationship
with Modelbit by following the instructions
in the Integrations tab within Settings. Then return here to use this sample code.
Calling AWS Lambda
Copy-paste the following sample code into your notebook:
import boto3
def invoke_remote_lambda(remote_role_arn, remote_func_arn, request_payload):
sts_client = boto3.client('sts')
remote_role = sts_client.assume_role(
RoleArn=remote_role_arn,
RoleSessionName="modelbit_deployment")
remote_creds = remote_role['Credentials']
remote_client = boto3.client(
"lambda",
region_name=remote_func_arn.split(":")[3],
aws_access_key_id=remote_creds['AccessKeyId'],
aws_secret_access_key=remote_creds['SecretAccessKey'],
aws_session_token=remote_creds['SessionToken'])
response = remote_client.invoke(
FunctionName=remote_func_arn,
InvocationType='RequestResponse',
Payload=request_payload)
return response['Payload'].read().decode()
Then write a deployment function, like what follows, to call your Lambda. Change
the remote_role_arn
to the ARN of the role with the Trust relationship
,
and remote_func_arn
to the ARN of your Lambda.
import json
def predict_with_remote_lambda(input_values):
lambda_response = invoke_remote_lambda(
remote_role_arn="arn:aws:iam::111111111111:role/my-lambda-role",
remote_func_arn="arn:aws:lambda:us-east-2:111111111111:function:my-lambda-func",
request_payload=json.dumps(input_values))
return json.loads(lambda_response)
# Test your lambda call
predict_with_remote_lambda({ "foo": 1, "bar": 2 })
# Deploy to Modelbit!
mb.deploy(predict_with_remote_lambda)
Now the predict_with_remote_lambda
deployment is able to call your Lambda function! Pro tip: Set up
your notebook to have access to AWS and allowed to assume the same role with the
Trust relationship
. That will make development and debugging much easier.
Reading from AWS S3
Copy-paste the following sample code into your notebook:
import boto3
def get_s3_data(remote_role_arn, s3bucket, s3key):
sts_client = boto3.client('sts')
remote_role = sts_client.assume_role(
RoleArn=remote_role_arn,
RoleSessionName="modelbit_deployment")
remote_creds = remote_role['Credentials']
remote_client = boto3.client(
"s3",
aws_access_key_id=remote_creds['AccessKeyId'],
aws_secret_access_key=remote_creds['SecretAccessKey'],
aws_session_token=remote_creds['SessionToken'])
response = remote_client.get_object(
Bucket=s3bucket,
Key=s3key)
return response['Body'].read().decode()
Then write a deployment function, like what follows, to read your S3 data. Change
the remote_role_arn
to the ARN of the role with the Trust relationship
,
and s3bucket
and s3key
to the values of your S3 data file.
def predict_with_s3_data():
s3data = get_s3_data(
remote_role_arn="arn:aws:iam::111111111111:role/my-s3-reading-role",
s3bucket="my-s3-bucket",
s3key="my-s3-key")
return f"The s3 file's length is {len(s3data)}"
# Test your lambda call
predict_with_s3_data()
# Deploy to Modelbit!
mb.deploy(predict_with_s3_data)
Now the predict_with_s3_data
deployment is able to read your S3 data! Pro tip: Set up
your notebook to have access to AWS and allowed to assume the same role with the
Trust relationship
. That will make development and debugging much easier.