On boto3, how can I extend ResourceModel
? What I wan't to do is subclass boto3.resources.factory.ec2.Instance
and add a run
method to it. That method would be used to remotely run commands on the EC2 instance represented by the Python object, via SSH. I wish to do this in a clean way, i.e., without resorting to monkey patches or other obscure techniques.
Update
Based on Daniel's answer, I came up with the following code. Requires a recent version of Boto 3, and Spur for the SSH connection (pip install spur boto3
).
from boto3 import session
from shlex import split
from spur import SshShell
# Customize here.
REGION = 'AWS-REGION'
INSTID = 'AWS-INSTANCE-ID'
USERID = 'SSH-USER'
def hook_ssh(class_attributes, **kwargs):
def run(self, command):
'''Run a command on the EC2 instance via SSH.'''
# Create the SSH client.
if not hasattr(self, '_ssh_client'):
self._ssh_client = SshShell(self.public_ip_address, USERID)
print(self._ssh_client.run(split(command)).output.decode())
class_attributes['run'] = run
if __name__ == '__main__':
b3s = session.Session()
ec2 = b3s.resource('ec2', region_name=REGION)
# Hook the "run" method to the "ec2.Instance" resource class.
b3s.events.register('creating-resource-class.ec2.Instance', hook_ssh)
# Run some commands.
ec2.Instance(INSTID).run('uname -a')
ec2.Instance(INSTID).run('uptime')
00:00 Boto3's primary function is to make AWS API calls for you. It extracts these APIs in two main ways: clients and resources. Clients give you low-level service access, while resources provide an object-oriented way of working with these services. 00:43 and set that equal to boto3.
The token (and the access and secret keys) generated using this API is valid for a specific duration (minimum 900 seconds). The maximum duration of the validity of the token is 12 hours (provided it is configured in the role).
put_object` does not overwrite the existing data in the bucket.
Boto3 resource is a high-level object-oriented API service you can use to connect and access your AWS resource. It has actions() defined which can be used to make calls to the AWS service.
The short answer is that this isn't yet possible but it is planned to allow customizations of this sort. You can already see them in action with the new upload_file
and download_file
customization available on the S3 client. The plan is to use that same sort of mechanism for Boto 3 resources.
Take a look here:
https://github.com/boto/boto3/blob/develop/boto3/session.py#L314-L318 https://github.com/boto/boto3/tree/develop/boto3/s3
Boto 3 extensibility is definitely on our radar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With