Core¶
Core AWS utilities and common functions used across the library.
AWSServiceAndResourceProvider
dataclass
¶
AWSServiceAndResourceProvider(service_name)
Bases: AWSServiceProvider[ClientType], Generic[ClientType, ResourceType]
Provider for creating typed AWS service clients and resources.
Extends AWSServiceProvider to also support resource-based access patterns.
Attributes:
| Name | Type | Description |
|---|---|---|
service_name |
Resources
|
The name of the AWS service (must support resources). |
get_resource ¶
get_resource(region=None, **kwargs)
Get a typed resource for this AWS service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
**kwargs
|
Additional arguments passed to boto3 resource creation. |
{}
|
Returns:
| Type | Description |
|---|---|
ResourceType
|
A typed boto3 resource for the service. |
Source code in src/aibs_informatics_aws_utils/core.py
399 400 401 402 403 404 405 406 407 408 409 | |
AWSServiceProvider
dataclass
¶
AWSServiceProvider(service_name)
Bases: Generic[ClientType]
Provider for creating typed AWS service clients.
Attributes:
| Name | Type | Description |
|---|---|---|
service_name |
Services
|
The name of the AWS service. |
get_client ¶
get_client(region=None, **kwargs)
Get a typed client for this AWS service.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
Optional[str]
|
AWS region. Defaults to None (uses default region). |
None
|
**kwargs
|
Additional arguments passed to boto3 client creation. |
{}
|
Returns:
| Type | Description |
|---|---|
ClientType
|
A typed boto3 client for the service. |
Source code in src/aibs_informatics_aws_utils/core.py
372 373 374 375 376 377 378 379 380 381 382 | |
client_error_code_check ¶
client_error_code_check(client_error, *error_codes)
Check if a ClientError matches any of the specified error codes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_error
|
ClientError
|
The ClientError exception to check. |
required |
*error_codes
|
str
|
One or more error codes to check against. |
()
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the error code matches any of the specified codes. |
Source code in src/aibs_informatics_aws_utils/core.py
220 221 222 223 224 225 226 227 228 229 230 | |
get_account_id ¶
get_account_id()
Get the AWS account ID from the current credentials.
Returns:
| Type | Description |
|---|---|
str
|
The AWS account ID as a string. |
Source code in src/aibs_informatics_aws_utils/core.py
155 156 157 158 159 160 161 | |
get_caller_identity ¶
get_caller_identity()
Get the caller identity from AWS STS.
Returns:
| Type | Description |
|---|---|
GetCallerIdentityResponseTypeDef
|
The caller identity response containing Account, Arn, and UserId. |
Source code in src/aibs_informatics_aws_utils/core.py
182 183 184 185 186 187 188 | |
get_client ¶
get_client(service, session=None, region=None, **kwargs)
Get a boto3 client object Notes:
- If session is not provided, then the default session is used
- Order of priority for region is
- region parameter
- "region_name" in **kwargs
- fallback to whatever is fetched in get_region
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Clients
|
The intended service to build client for |
required |
session
|
Session
|
A customized session object. Defaults to None. |
None
|
region
|
Optional[str]
|
An explicit region. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
|
botocore.client.BaseClient: A boto3 Client object |
Source code in src/aibs_informatics_aws_utils/core.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
get_client_error_code ¶
get_client_error_code(client_error)
Extract the error code from a boto3 ClientError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_error
|
ClientError
|
The ClientError exception. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The error code string, or "Unknown" if not found. |
Source code in src/aibs_informatics_aws_utils/core.py
196 197 198 199 200 201 202 203 204 205 | |
get_client_error_message ¶
get_client_error_message(client_error)
Extract the error message from a boto3 ClientError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_error
|
ClientError
|
The ClientError exception. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The error message string, or "Unknown" if not found. |
Source code in src/aibs_informatics_aws_utils/core.py
208 209 210 211 212 213 214 215 216 217 | |
get_iam_arn ¶
get_iam_arn()
Get the IAM ARN from the current credentials.
Returns:
| Type | Description |
|---|---|
IAMArn
|
The IAM ARN of the current identity. |
Source code in src/aibs_informatics_aws_utils/core.py
173 174 175 176 177 178 179 | |
get_region ¶
get_region(region=None)
Get and sanitize region value
Will retrieve the current region from a newly created boto3 session. This is helpful for getting the region dynamically within a Lambda function or from the environment if that is not available.
Logic of priority for resolving region
- User input
- boto3.Session
- ENV VAR "AWS_REGION"
- ENV VAR "REGION"
Subsequently, the region value is validated against a regex
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
region
|
str
|
user provided region. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
ApplicationException
|
|
Returns:
| Type | Description |
|---|---|
str
|
AWS Region |
Source code in src/aibs_informatics_aws_utils/core.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
get_resource ¶
get_resource(service, session=None, region=None, **kwargs)
Get a boto3 resource object Notes:
- If session is not provided, then the default session is used
- Order of priority for region is
- region parameter
- "region_name" in **kwargs
- fallback to whatever is fetched in get_region
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service
|
Resources
|
The intended service to build resource from |
required |
session
|
Session
|
A customized session object. Defaults to None. |
None
|
region
|
Optional[str]
|
An explicit region. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
ServiceResource
|
boto3.resources.base.ServiceResource: A ServiceResource object |
Source code in src/aibs_informatics_aws_utils/core.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
get_session ¶
get_session(session=None)
Get or create a boto3 Session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
Optional[Union[Session, Session]]
|
An existing Session or BotocoreSession to use. If None, creates a new default Session. |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
A boto3 Session object. |
Source code in src/aibs_informatics_aws_utils/core.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
get_user_id ¶
get_user_id()
Get the IAM user ID from the current credentials.
Returns:
| Type | Description |
|---|---|
UserId
|
The IAM user ID. |
Source code in src/aibs_informatics_aws_utils/core.py
164 165 166 167 168 169 170 | |