-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathhandler.py
44 lines (34 loc) · 1.13 KB
/
handler.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
import json
import logging
import os
import boto3
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
QUEUE_URL = os.getenv('QUEUE_URL')
SQS = boto3.client('sqs')
def producer(event, context):
status_code = 200
message = ''
if not event.get('body'):
return {'statusCode': 400, 'body': json.dumps({'message': 'No body was found'})}
try:
message_attrs = {
'AttributeName': {'StringValue': 'AttributeValue', 'DataType': 'String'}
}
SQS.send_message(
QueueUrl=QUEUE_URL,
MessageBody=event['body'],
MessageAttributes=message_attrs,
)
message = 'Message accepted!'
except Exception as e:
logger.exception('Sending message to SQS queue failed!')
message = str(e)
status_code = 500
return {'statusCode': status_code, 'body': json.dumps({'message': message})}
def consumer(event, context):
for record in event['Records']:
logger.info(f'Message body: {record["body"]}')
logger.info(
f'Message attribute: {record["messageAttributes"]["AttributeName"]["stringValue"]}'
)