Using an AWS Access Key and Secret Key
You can access AWS resources in Modelbit by storing your AWS Access Key ID and AWS Secret Access Key as Modelbit Secrets.
Create a keypair and store them as Modelbit secrets.
Begin by creating a keypair for Modelbit.
Next, go to Settings in Modelbit and click the "Secrets" tab. Add both your AWS Access Key ID and AWS Secret Access Key here as secrets.
Name your secrets AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
and Modelbit will automatically configure them for use with boto3
:
remote_client = boto3.client("my-service")
If you use other names for the secrets you will need to pass them into a boto3
client:
remote_client = boto3.client(
"my-service",
aws_access_key_id = mb.get_secret('SPECIAL_AWS_ACCESS_KEY_ID'),
aws_secret_access_key = mb.get_secret('SPECIAL_AWS_SECRET_ACCESS_KEY'))
Reading from AWS S3
The following is example code from your Notebook you can use to access S3 in a deployment:
import boto3
def predict_with_s3_data():
client = boto3.client("s3")
obj = client.get_object(Bucket='<YOUR BUCKET>', Key='path/to/your/file')
return f"The S3 file contents are: {obj['Body'].read().decode()}"}
# Test your S3 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!
Calling AWS Lambda
The following is example code from your Notebook you can use to access Lambda in a deployment:
import json
import boto3
def predict_with_remote_lambda(input_values):
remote_client = boto3.client("lambda")
response = remote_client.invoke(
FunctionName = '<Your Function ARN>',
InvocationType = 'RequestResponse',
Payload = json.dumps(input_values))
return response['Payload'].read().decode()
# 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!