-
Notifications
You must be signed in to change notification settings - Fork 541
/
aws.py
83 lines (66 loc) · 2.55 KB
/
aws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""AWS cloud adaptors"""
# pylint: disable=import-outside-toplevel
import functools
import threading
boto3 = None
botocore = None
_session_creation_lock = threading.RLock()
def import_package(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
global boto3, botocore
if boto3 is None or botocore is None:
try:
import boto3 as _boto3
import botocore as _botocore
boto3 = _boto3
botocore = _botocore
except ImportError:
raise ImportError('Fail to import dependencies for AWS.'
'Try pip install "skypilot[aws]"') from None
return func(*args, **kwargs)
return wrapper
# lru_cache() is thread-safe and it will return the same session object
# for different threads.
# Reference: https://docs.python.org/3/library/functools.html#functools.lru_cache # pylint: disable=line-too-long
@functools.lru_cache()
@import_package
def session():
"""Create an AWS session."""
# Creating the session object is not thread-safe for boto3,
# so we add a reentrant lock to synchronize the session creation.
# Reference: https://github.com/boto/boto3/issues/1592
# However, the session object itself is thread-safe, so we are
# able to use lru_cache() to cache the session object.
with _session_creation_lock:
return boto3.session.Session()
@functools.lru_cache()
@import_package
def resource(resource_name: str, **kwargs):
"""Create an AWS resource.
Args:
resource_name: AWS resource name (e.g., 's3').
kwargs: Other options.
"""
# Need to use the resource retrieved from the per-thread session
# to avoid thread-safety issues (Directly creating the client
# with boto3.resource() is not thread-safe).
# Reference: https://stackoverflow.com/a/59635814
return session().resource(resource_name, **kwargs)
@functools.lru_cache()
def client(service_name: str, **kwargs):
"""Create an AWS client of a certain service.
Args:
service_name: AWS service name (e.g., 's3', 'ec2').
kwargs: Other options.
"""
# Need to use the client retrieved from the per-thread session
# to avoid thread-safety issues (Directly creating the client
# with boto3.client() is not thread-safe).
# Reference: https://stackoverflow.com/a/59635814
return session().client(service_name, **kwargs)
@import_package
def botocore_exceptions():
"""AWS botocore exception."""
from botocore import exceptions
return exceptions