From 6f7f9f7517020f4d44f9a9522fbb47c50d197bcb Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Thu, 30 Jan 2025 00:07:52 +0100 Subject: [PATCH] chore(aws): intro to apis --- knowledge base/cloud computing/aws/README.md | 115 +++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/knowledge base/cloud computing/aws/README.md b/knowledge base/cloud computing/aws/README.md index 22e8667..285d1a7 100644 --- a/knowledge base/cloud computing/aws/README.md +++ b/knowledge base/cloud computing/aws/README.md @@ -18,6 +18,8 @@ 1. [Access control](#access-control) 1. [Savings plans](#savings-plans) 1. [Resource tagging](#resource-tagging) +1. [API](#api) + 1. [Python](#python) 1. [Further readings](#further-readings) 1. [Sources](#sources) @@ -338,6 +340,105 @@ Suggested: [Create tag policies][creating organization policies with aws organizations] to enforce values, and to prevent the creation of non-compliant resources. +## API + +Refer [Tools to Build on AWS]. + +### Python + +Refer [Boto3 documentation].
+Also see [Difference in Boto3 between resource, client, and session?]. + +_Clients_ and _Resources_ are different abstractions for service requests within the Boto3 SDK.
+When making API calls to an AWS service with Boto3, one does so via a _Client_ or a _Resource_. + +_Sessions_ are fundamental to both Clients and Resources and how both get access to AWS credentials. + +
+ Client + +Provides low-level access to AWS services by exposing the `botocore` client to the developer. + +Typically maps 1:1 with the related service's API and supports all operations for the called service.
+Exposes Python-fashioned method names (e.g. ListBuckets API => list_buckets method). + +Typically yields primitive, non-marshalled AWS data.
+E.g. DynamoDB attributes are dictionaries representing primitive DynamoDB values. + +Limited to listing at most 1000 objects, requiring the developer to deal with result pagination in code.
+Use a [paginator][boto3 paginators] or implement one's own loop. + +
+ Example + +```py +import boto3 + +client = boto3.client('s3') +response = client.list_objects_v2(Bucket='mybucket') +for content in response['Contents']: + obj_dict = client.get_object(Bucket='mybucket', Key=content['Key']) + print(content['Key'], obj_dict['LastModified']) +``` + +
+
+ +
+ Resource + +Refer [Boto3 resources]. + +Provides high-level, object-oriented code. + +Does **not** provide 100% API coverage of AWS services. + +Uses identifiers and attributes, has actions (operations on resources), and exposes sub-resources and collections of +AWS resources. + +Typically yields marshalled data, **not** primitive AWS data.
+E.g. DynamoDB attributes are native Python values representing primitive DynamoDB values. + +Takes care of result pagination.
+The resulting collections of sub-resources are lazily-loaded. + +Resources are **not** thread safe and should **not** be shared across threads or processes.
+Create a new Resource for each thread or process instead. + +Since January 2023 the AWS Python SDK team stopped adding new features to the resources interface in Boto3.
+Newer service features can be accessed through the Client interface.
+Refer [More info about resource deprecation?] for more information. + +
+ Example + +```py +import boto3 + +s3 = boto3.resource('s3') +bucket = s3.Bucket('mybucket') +for obj in bucket.objects.all(): + print(obj.key, obj.last_modified) +``` + +
+
+ +
+ Session + +Refer [Boto3 sessions]. + +Stores configuration information (primarily credentials and selected AWS Region).
+Initiates the connectivity to AWS services. + +Leveraged by service Clients and Resources.
+boto3 creates a default session automatically when needed, using the default credential profile.
+The default credentials profile uses the `~/.aws/credentials` file if found, or tries assuming the role of the executing +machine if not. + +
+ ## Further readings - [EC2] @@ -345,6 +446,9 @@ creation of non-compliant resources. - [Best Practices for Tagging AWS Resources] - [Automating DNS-challenge based LetsEncrypt certificates with AWS Route 53] - AWS' [CLI] +- [Tools to Build on AWS] +- [Boto3 documentation] +- [More info about resource deprecation?] ### Sources @@ -371,6 +475,10 @@ creation of non-compliant resources. - [Creating organization policies with AWS Organizations] - [AWS re:Invent 2022 - Advanced VPC design and new Amazon VPC capabilities (NET302)] - [Enable or disable AWS Regions in your account] +- [Difference in Boto3 between resource, client, and session?] +- [Boto3 resources] +- [Boto3 sessions] +- [Boto3 paginators]