-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_posts.py
65 lines (49 loc) · 1.84 KB
/
new_posts.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
import os
import uuid
import boto3
def lambda_handler(event, context):
recordId = str(uuid.uuid4())
text = event['text']
voice = event['voice']
print('*Input Text*=', text)
print('*Selected Voice*=', voice)
# Generate record in DynamoDB
try :
tableName = os.environ['DB_TABLE_NAME']
print('*DynamoDB TableName*=', tableName)
# Need client just to access the Exception
dynamodb_client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')
try :
# table = dynamodb_client.list_tables()[tableName]
table = dynamodb.Table(tableName)
table.put_item (
Item= {
'id': recordId,
'text': text,
'voice': voice,
'status': 'PROCESSING'
}
)
print('Generated new DynamoDB record, ID: ' + recordId)
except dynamodb_client.exceptions.ResourceNotFoundException as tableNotFoundEx:
return ('ERROR: Unable to locate DynamoDB table: ', tableName)
except KeyError as dynamoDBKeyError:
msg = 'ERROR: Need DynamoDB Environment Var: DB_TABLE_NAME'
print(dynamoDBKeyError)
return msg;
# Send Notificaiton to SNS
try :
topicName = os.environ['SNS_TOPIC_ARN']
print('*SNS TopicName*=', topicName)
sns_client = boto3.client('sns')
sns_client.publish(
TopicArn = topicName,
Message = recordId
)
print('Generated new SNS message, ID: ' + recordId)
except KeyError as snsKeyError:
msg = 'ERROR: Need SNS Environment Var: SNS_TOPIC_NAME'
print(snsKeyError)
return msg;
return recordId