1818from cryptography .x509 .oid import NameOID
1919from cryptography .hazmat .backends import default_backend
2020from cryptography .hazmat .primitives import hashes
21+ from aws_lambda_powertools .utilities .typing import LambdaContext
22+ from aws_lambda_powertools .utilities .data_classes import SQSEvent
2123
2224logger = logging .getLogger ()
2325logger .setLevel ("INFO" )
@@ -58,7 +60,8 @@ def get_thing(thing_name):
5860 response = iot_client .describe_thing (thingName = thing_name )
5961 return response .get ("thingArn" )
6062 except ClientError as error :
61- assert error .response ['Error' ]['Code' ] == 'ResourceNotFoundException'
63+ error_code = error .response ['Error' ]['Code' ]
64+ assert error_code == 'ResourceNotFoundException'
6265 return None
6366
6467def get_policy (policy_name ):
@@ -67,12 +70,13 @@ def get_policy(policy_name):
6770 try :
6871 response = iot_client .get_policy (policyName = policy_name )
6972 return response .get ('policyArn' )
70- except botocore .exceptions .ClientError as error :
71- if error .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
72- print ("ERROR: You need to configure the policy [" + policy_name +
73- "] in your target region first." )
74- if error .response ['Error' ]['Code' ] == 'UnauthorizedException' :
75- print ("ERROR: There is a deployment problem with the attached Role."
73+ except ClientError as error :
74+ error_code = error .response ['Error' ]['Code' ]
75+ if error_code == 'ResourceNotFoundException' :
76+ logger .error ("ERROR: You need to configure the policy {policy_name} "
77+ "in your target region first." )
78+ if error_code == 'UnauthorizedException' :
79+ logger .error ("There is a deployment problem with the attached Role."
7680 "Unable to reach IoT Core object." )
7781 return None
7882
@@ -83,13 +87,14 @@ def get_thing_group(thing_group_name):
8387 try :
8488 response = iot_client .describe_thing_group (thingGroupName = thing_group_name )
8589 return response .get ('thingGroupArn' )
86- except botocore .exceptions .ClientError as error :
87- if error .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
88- print ("ERROR: You need to configure the Thing Group [" + thing_group_name +
89- "] in your target region first." )
90- if error .response ['Error' ]['Code' ] == 'UnauthorizedException' :
91- print ("ERROR: There is a deployment problem with the attached Role. Unable"
92- "to reach IoT Core object." )
90+ except ClientError as error :
91+ error_code = error .response ['Error' ]['Code' ]
92+ if 'ResourceNotFoundException' == error_code :
93+ logger .error ("You need to configure the Thing Group {thing_group_name} "
94+ "in your target region first." )
95+ if 'UnauthorizedException' == error_code :
96+ logger .error ("There is a deployment problem with the attached Role. Unable"
97+ "to reach IoT Core object." )
9398 return None
9499
95100def get_thing_type (type_name ):
@@ -99,12 +104,13 @@ def get_thing_type(type_name):
99104 response = iot_client .describeThingType (thingTypeName = type_name )
100105 return response .get ('thingTypeArn' )
101106 except ClientError as error :
102- if error .response ['Error' ]['Code' ] == 'ResourceNotFoundException' :
103- print ("ERROR: You need to configure the Thing Type [" + type_name +
104- "] in your target region first." )
105- if error .response ['Error' ]['Code' ] == 'UnauthorizedException' :
106- print ("ERROR: There is a deployment problem with the attached Role."
107- "Unable to reach IoT Core object." )
107+ error_code = error .response ['Error' ]['Code' ]
108+ if 'ResourceNotFoundException' == error_code :
109+ logger .error ("You need to configure the Thing Type {type_name} "
110+ "in your target region first." )
111+ if 'UnauthorizedException' == error_code :
112+ logger .error ("There is a deployment problem with the attached Role."
113+ "Unable to reach IoT Core object." )
108114 return None
109115
110116def process_policy (policy_name , certificate_id ):
@@ -121,7 +127,8 @@ def process_thing(thing_name, certificate_id, thing_type_name):
121127 iot_client .describe_thing (thingName = thing_name )
122128 return None
123129 except ClientError as error :
124- print ("Thing not found (" + error .response ['Error' ]['Code' ] + "). Creating." )
130+ error_code = error .response ['Error' ]['Code' ]
131+ logger .info ("Thing not found {error_code}. Creating." )
125132
126133 # Create thing
127134 try :
@@ -132,8 +139,8 @@ def process_thing(thing_name, certificate_id, thing_type_name):
132139 thingTypeName = thing_type_name )
133140
134141 except ClientError as error :
135- print ( "ERROR Thing creation failed." )
136- print ( error )
142+ error_code = error . response [ 'Error' ][ 'Code' ]
143+ logger . error ( "ERROR Thing creation failed: {error_code}" )
137144 return None
138145
139146 try :
@@ -182,11 +189,9 @@ def process_certificate(config, requeue_cb):
182189 fingerprint , error .response ['Error' ]['Code' ])
183190
184191 try :
185- print ("Importing certificate." )
186192 response = iot_client .register_certificate_without_ca (
187193 certificatePem = certificate_text .decode ('ascii' ),
188194 status = 'ACTIVE' )
189- print ("Certificate imported." )
190195 return response .get ("certificateId" )
191196 except botocore .exceptions .ClientError as e :
192197 if e .response ['Error' ]['Code' ] == 'ThrottlingException' :
@@ -248,7 +253,7 @@ def process_sqs(config):
248253 process_policy (policy_name , certificate_id )
249254 process_thing_group (thing_group_name , thing_name )
250255
251- def lambda_handler (event , context ):
256+ def lambda_handler (event : SQSEvent , context : LambdaContext ):
252257 """Lambda function main entry point"""
253258 if event .get ('Records' ) is None :
254259 print ("ERROR: Configuration incorrect: no event record on invoke" )
@@ -264,9 +269,4 @@ def lambda_handler(event, context):
264269 config = json .loads (record ["body" ])
265270 process_sqs (config )
266271
267- return {
268- "statusCode" : 204 ,
269- "body" : json .dumps ({
270- "message" : "Processing succeeded."
271- })
272- }
272+ return event
0 commit comments