Boto3 Resources

Boto3 Resources

Boto3 resources

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to configure Python Boto3 SDK for AWS.

https://cloudaffaire.com/how-to-configure-python-boto3-sdk-for-aws/

In this blog post, we will discuss boto3 resources.

What are resources?

Resources represent an object-oriented interface to Amazon Web Services (AWS). They provide a higher-level abstraction than the raw, low-level calls made by service clients. Every resource instance has a number of attributes and methods. These can conceptually be split up into identifiers, attributes, actions, references, sub-resources, and collections.

Resources themselves can also be conceptually split into service resources (like sqs, s3, ec2, etc) and individual resources (like sqs.Queue or s3.Bucket). Service resources do not have identifiers or attributes. The two share the same components otherwise.

Resource components:

  • Identifier: An identifier is a unique value that is used to call actions on the resource. Resources must have at least one identifier, except for the top-level service resources (e.g. ec2 or s3). An identifier is set at instance creation-time, and failing to provide all necessary identifiers during instantiation will result in an exception.
  • Attributes: Resources may also have attributes, which are lazy-loaded properties on the instance. They may be set at creation time from the response of an action on another resource, or they may be set when accessed or via an explicit call to the load or reload action.
  • Actions: An action is a method which makes a call to the service. Actions may return a low-level response, a new resource instance or a list of new resource instances. Actions automatically set the resource identifiers as parameters, but allow you to pass additional parameters via keyword arguments.
  • References: A reference is an attribute which may be None or a related resource instance. The resource instance does not share identifiers with its reference resource, that is, it is not a strict parent to child relationship. In relational terms, these can be considered many-to-one or one-to-one.
  • Sub-resources: A sub-resource is similar to a reference, but is a related class rather than an instance. Sub-resources, when instantiated, share identifiers with their parent. It is a strict parent-child relationship. In relational terms, these can be considered one-to-many.
  • Waiters: A waiter is similar to an action. A waiter will poll the status of a resource and suspend execution until the resource reaches the state that is being polled for or a failure occurs while polling. Waiters automatically set the resource identifiers as parameters, but allow you to pass additional parameters via keyword arguments.
  • Collections: A collection provides an iterable interface to a group of resources. Collections behave similarly to Django QuerySets and expose a similar API. A collection seamlessly handles pagination for you, making it possible to easily iterate over all items from all pages of data.

Let us explain the different component of a resource with a demo.

Prerequisites for this demo:

  • One EC2 AWS Linux 2 instance with boto3 installed and configured.

Demo:

Resource components with respect to the code:

  • Service Resource: ec2
  • Individual Resources: vpc, subnet
  • Identifier: vpc (id) and subnet (id).
  • Attributes: vpc (CidrBlock=’10.0.0.0/16′) and subnet (AvailabilityZone=’ap-south-1a’,CidrBlock=’10.0.1.0/24′)
  • Actions: ec2 (create_vpc()), vpc (create_subnet(), create_tags()), subnet (create_tags())
  • References: vpc (dhcp_options), subnet (vpc)
  • Sub-resources: subnet (print(subnet.vpc.id))
  • Waiters: vpc (wait_until_available())

Hope you have enjoyed this article. In the next blog post, we will discuss boto3 clients.

To get more details on Python Boto3, please refer below AWS documentation

https://boto3.amazonaws.com/v1/documentation/api/latest/index.html

 

Leave a Reply

Close Menu