Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

aws_connector.py 4.2 KB

You have to be logged in to leave a comment. Sign In
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  1. import sys
  2. import boto3
  3. import logging
  4. from botocore.exceptions import ClientError, ProfileNotFound
  5. from super_gradients.common import explicit_params_validation
  6. class AWSConnector:
  7. """
  8. AWSConnector - Connects to AWS using Credentials File or IAM Role
  9. """
  10. @staticmethod
  11. def __create_boto_3_session(profile_name: str):
  12. """
  13. __create_boto_3_session
  14. :param profile_name:
  15. :return:
  16. """
  17. current_class_name = __class__.__name__
  18. logger = logging.getLogger(current_class_name)
  19. try:
  20. try:
  21. if profile_name and boto3.session.Session(profile_name=profile_name).get_credentials():
  22. # TRY USING A SPECIFIC PROFILE_NAME (USING A CREDENTIALS FILE)
  23. logger.info('Trying to connect to AWS using Credentials File with profile_name: ' + profile_name)
  24. session = boto3.Session(profile_name=profile_name)
  25. return session
  26. except ProfileNotFound as profileNotFoundException:
  27. logger.debug(
  28. '[' + current_class_name + '] - Could not find profile name - Trying using Default Profile/IAM Role' + str(
  29. profileNotFoundException))
  30. # TRY USING AN IAM ROLE (OR *DEFAULT* CREDENTIALS - USING A CREDENTIALS FILE)
  31. logger.info('Trying to connect to AWS using IAM role or Default Credentials')
  32. session = boto3.Session()
  33. return session
  34. except Exception as ex:
  35. logger.critical(
  36. '[' + current_class_name + '] - Caught Exception while trying to connect to AWS Credentials Manager ' + str(
  37. ex))
  38. return None
  39. @staticmethod
  40. def get_aws_session(profile_name: str) -> boto3.Session:
  41. """
  42. get_aws_session - Connects to AWS to retrieve an AWS Session
  43. :param profile_name: The Config Profile (Environment Name in Credentials file)
  44. :return: boto3 Session
  45. """
  46. current_class_name = __class__.__name__
  47. logger = logging.getLogger(current_class_name)
  48. aws_session = AWSConnector.__create_boto_3_session(profile_name=profile_name)
  49. if aws_session is None:
  50. logger.error('Failed to initiate an AWS Session')
  51. return aws_session
  52. @staticmethod
  53. def get_aws_client_for_service_name(profile_name: str, service_name: str) -> boto3.Session.client:
  54. """
  55. get_aws_client_for_service_name - Connects to AWS to retrieve the relevant Client
  56. :param profile_name: The Config Profile (Environment Name in Credentials file)
  57. :param service_name: The AWS Service name to get the Client for
  58. :return: Service client instance
  59. """
  60. current_class_name = __class__.__name__
  61. logger = logging.getLogger(current_class_name)
  62. aws_session = AWSConnector.__create_boto_3_session(profile_name=profile_name)
  63. if aws_session is None:
  64. logger.error('Failed to connect to AWS client: ' + str(service_name))
  65. return aws_session.client(service_name=service_name)
  66. @staticmethod
  67. def get_aws_resource_for_service_name(profile_name: str, service_name: str) -> boto3.Session.resource:
  68. """
  69. Connects to AWS to retrieve the relevant Resource (More functionality then Client)
  70. :param profile_name: The Config Profile (Environment Name in Credentials file)
  71. :param service_name: The AWS Service name to get the Client for
  72. :return: Service client instance
  73. """
  74. current_class_name = __class__.__name__
  75. logger = logging.getLogger(current_class_name)
  76. aws_session = AWSConnector.__create_boto_3_session(profile_name=profile_name)
  77. if aws_session is None:
  78. logger.error('Failed to connect to AWS client: ' + str(service_name))
  79. return aws_session.resource(service_name=service_name)
  80. @staticmethod
  81. def is_client_error(code):
  82. e = sys.exc_info()[1]
  83. if isinstance(e, ClientError) and e.response["Error"]["Code"] == code:
  84. return ClientError
  85. return type("NeverEverRaisedException", (Exception,), {})
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...