Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 load custom models

For example:

session = boto3.Session()
client = session.client('custom-service')

I know that I can create a json with API definitions under ~/.aws/models and botocore will load it from there. The problem is that I need to get it done on the AWS Lambda function, which looks like impossible to do so.

Looking for a way to tell boto3 where are the custom json api definitions so it could load from the defined path.

Thanks

like image 927
misiek303 Avatar asked Apr 20 '26 05:04

misiek303


1 Answers

I have only a partial answer. There's a bit of documentation about botocore's loader module, which is what reads the model files. In a disscusion about loading models from ZIP archives, a monkey patch was offered up which extracts the ZIP to a temporary filesystem location and then extends the loader search path to that location. It doesn't seem like you can load model data directly from memory based on the API, but Lambda does give you some scratch space in /tmp.

Here's the important bits:

import boto3
session = boto3.Session()
session._loader.search_paths.extend(["/tmp/boto"])
client = session.client("custom-service")

The directory structure of /tmp/boto needs to follow the resource loader documentation. The main model file needs to be at /tmp/boto/custom-service/yyyy-mm-dd/service-2.json.


The issue also mentions that alternative loaders can be swapped in using Session.register_component so if you wanted to write a scrappy loader which returned a model straight from memory you could try that too. I don't have any info about how to go about doing that.

like image 115
Huckle Avatar answered Apr 22 '26 14:04

Huckle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!