1- """Custom Resource to setup SRA Lambda resources in the organization.
1+ """Lambda module to setup SRA SNS resources in the organization.
22
33Version: 0.1
44
2929 from mypy_boto3_sns .type_defs import PublishBatchResponseTypeDef
3030
3131
32- class sra_sns :
32+ class SRASNS :
33+ """Class to setup SRA SNS resources in the organization."""
34+
3335 # Setup Default Logger
3436 LOGGER = logging .getLogger (__name__ )
3537 log_level : str = os .environ .get ("LOG_LEVEL" , "INFO" )
@@ -40,7 +42,6 @@ class sra_sns:
4042
4143 SNS_PUBLISH_BATCH_MAX = 10
4244
43-
4445 try :
4546 MANAGEMENT_ACCOUNT_SESSION = boto3 .Session ()
4647 SNS_CLIENT : SNSClient = MANAGEMENT_ACCOUNT_SESSION .client ("sns" , config = BOTO3_CONFIG )
@@ -51,11 +52,23 @@ class sra_sns:
5152 sts = sra_sts .sra_sts ()
5253
5354 def find_sns_topic (self , topic_name : str , region : str = "default" , account : str = "default" ) -> str | None :
54- """Find SNS Topic ARN."""
55+ """Find SNS Topic ARN.
56+
57+ Args:
58+ topic_name (str): SNS Topic Name
59+ region (str): AWS Region
60+ account (str): AWS Account
61+
62+ Raises:
63+ ValueError: Error finding SNS topic
64+
65+ Returns:
66+ str: SNS Topic ARN
67+ """
5568 if region == "default" :
5669 region = self .sts .HOME_REGION
5770 if account == "default" :
58- account = self .sts .MANAGEMENT_ACCOUNT
71+ account = self .sts .MANAGEMENT_ACCOUNT
5972 try :
6073 response = self .SNS_CLIENT .get_topic_attributes (
6174 TopicArn = f"arn:{ self .sts .PARTITION } :sns:{ region } :{ account } :{ topic_name } "
@@ -65,24 +78,34 @@ def find_sns_topic(self, topic_name: str, region: str = "default", account: str
6578 if e .response ["Error" ]["Code" ] == "NotFoundException" :
6679 self .LOGGER .info (f"SNS Topic '{ topic_name } ' not found exception." )
6780 return None
68- elif e .response ["Error" ]["Code" ] == "NotFound" :
81+ if e .response ["Error" ]["Code" ] == "NotFound" :
6982 self .LOGGER .info (f"SNS Topic '{ topic_name } ' not found." )
7083 return None
71- else :
72- raise ValueError (f"Error finding SNS topic: { e } " ) from None
84+ raise ValueError (f"Error finding SNS topic: { e } " ) from None
7385
7486 def create_sns_topic (self , topic_name : str , solution_name : str , kms_key : str = "default" ) -> str :
75- """Create SNS Topic."""
87+ """Create SNS Topic.
88+
89+ Args:
90+ topic_name (str): SNS Topic Name
91+ solution_name (str): Solution Name
92+ kms_key (str): KMS Key ARN
93+
94+ Raises:
95+ ValueError: Error creating SNS topic
96+
97+ Returns:
98+ str: SNS Topic ARN
99+ """
76100 if kms_key == "default" :
77101 self .LOGGER .info ("Using default KMS key for SNS topic." )
78102 kms_key = f"arn:{ self .sts .PARTITION } :kms:{ self .sts .HOME_REGION } :{ self .sts .MANAGEMENT_ACCOUNT } :alias/aws/sns"
79103 else :
80104 self .LOGGER .info (f"Using provided KMS key '{ kms_key } ' for SNS topic." )
81105 try :
82106 response = self .SNS_CLIENT .create_topic (
83- Name = topic_name ,
84- Attributes = {"DisplayName" : topic_name ,
85- "KmsMasterKeyId" : kms_key },
107+ Name = topic_name ,
108+ Attributes = {"DisplayName" : topic_name , "KmsMasterKeyId" : kms_key },
86109 Tags = [{"Key" : "sra-solution" , "Value" : solution_name }]
87110 )
88111 topic_arn = response ["TopicArn" ]
@@ -92,42 +115,75 @@ def create_sns_topic(self, topic_name: str, solution_name: str, kms_key: str = "
92115 raise ValueError (f"Error creating SNS topic: { e } " ) from None
93116
94117 def delete_sns_topic (self , topic_arn : str ) -> None :
95- """Delete SNS Topic."""
118+ """Delete SNS Topic.
119+
120+ Args:
121+ topic_arn (str): SNS Topic ARN
122+
123+ Raises:
124+ ValueError: Error deleting SNS topic
125+ """
96126 try :
97127 self .SNS_CLIENT .delete_topic (TopicArn = topic_arn )
98128 self .LOGGER .info (f"SNS Topic '{ topic_arn } ' deleted" )
99- return None
100129 except ClientError as e :
101130 raise ValueError (f"Error deleting SNS topic: { e } " ) from None
102131
103132 def find_sns_subscription (self , topic_arn : str , protocol : str , endpoint : str ) -> bool :
104- """Find SNS Subscription."""
133+ """Find SNS Subscription.
134+
135+ Args:
136+ topic_arn (str): SNS Topic ARN
137+ protocol (str): SNS Subscription Protocol
138+ endpoint (str): SNS Subscription Endpoint
139+
140+ Raises:
141+ ValueError: Error finding SNS subscription
142+
143+ Returns:
144+ bool: True if SNS Subscription exists, False otherwise.
145+ """
105146 try :
106- response = self .SNS_CLIENT .get_subscription_attributes (
147+ self .SNS_CLIENT .get_subscription_attributes (
107148 SubscriptionArn = f"arn:{ self .sts .PARTITION } :sns:{ self .sts .HOME_REGION } :{ self .sts .MANAGEMENT_ACCOUNT } :{ topic_arn } :{ protocol } :{ endpoint } "
108149 )
109150 return True
110151 except ClientError as e :
111152 if e .response ["Error" ]["Code" ] == "NotFoundException" :
112153 self .LOGGER .info (f"SNS Subscription for { endpoint } not found on topic { topic_arn } ." )
113154 return False
114- else :
115- raise ValueError (f"Error finding SNS subscription: { e } " ) from None
155+ raise ValueError (f"Error finding SNS subscription: { e } " ) from None
116156
117157 def create_sns_subscription (self , topic_arn : str , protocol : str , endpoint : str ) -> None :
118- """Create SNS Subscription."""
158+ """Create SNS Subscription.
159+
160+ Args:
161+ topic_arn (str): SNS Topic ARN
162+ protocol (str): SNS Subscription Protocol
163+ endpoint (str): SNS Subscription Endpoint
164+
165+ Raises:
166+ ValueError: Error creating SNS subscription
167+ """
119168 try :
120169 self .SNS_CLIENT .subscribe (TopicArn = topic_arn , Protocol = protocol , Endpoint = endpoint )
121170 self .LOGGER .info (f"SNS Subscription created for { endpoint } on topic { topic_arn } " )
122171 sleep (5 ) # Wait for subscription to be created
123- return None
124172 except ClientError as e :
125173 raise ValueError (f"Error creating SNS subscription: { e } " ) from None
126174
127175 def set_topic_access_for_alarms (self , topic_arn : str , source_account : str ) -> None :
128- """Set SNS Topic Policy to allow access for alarm."""
176+ """Set SNS Topic Policy to allow access for alarm.
177+
178+ Args:
179+ topic_arn (str): SNS Topic ARN
180+ source_account (str): Source AWS Account
181+
182+ Raises:
183+ ValueError: Error setting SNS topic policy
184+ """
129185 try :
130- policy = {
186+ policy = { # noqa: ECE001
131187 "Version" : "2012-10-17" ,
132188 "Statement" : [
133189 {
@@ -151,7 +207,6 @@ def set_topic_access_for_alarms(self, topic_arn: str, source_account: str) -> No
151207 AttributeValue = json .dumps (policy )
152208 )
153209 self .LOGGER .info (f"SNS Topic Policy set for { topic_arn } to allow access for CloudWatch alarms in the { source_account } account" )
154- return None
155210 except ClientError as e :
156211 raise ValueError (f"Error setting SNS topic policy: { e } " ) from None
157212
0 commit comments