-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.py
82 lines (58 loc) · 1.83 KB
/
lambda.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""SmartSheet-to-DynamoDB Sync Lambda Function."""
# Add vendor directory to module search path
import os
parent_dir = os.path.abspath(os.path.dirname(__file__))
vendor_dir = os.path.join(parent_dir, 'vendor')
sys.path.append(vendor_dir)
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
import logging
import sys
from base64 import b64decode
import boto3
import ssdbsync
__author__ = "Chris Lunsford"
__author_email__ = "chrlunsf@cisco.com"
__copyright__ = "Copyright (c) 2017 Cisco Systems, Inc."
__license__ = "MIT"
# Constants
ENCRYPTED_SMARTSHEET_ACCESS_TOKEN = os.getenv('ENCRYPTED_SMARTSHEET_ACCESS_TOKEN')
SHEET_ID = os.getenv('SHEET_ID')
COLUMN_TITLES_ID = '0'
# Initialize logging
ssdbsync.initialize_logging()
logger = logging.getLogger(__name__)
# Initialize AWS Key Management System (KMS) interface
kms = boto3.client('kms')
# Initialize SmartSheet interface
smartsheet_access_token = kms.decrypt(
CiphertextBlob=b64decode(ENCRYPTED_SMARTSHEET_ACCESS_TOKEN))['Plaintext']
smartsheet = ssdbsync.SmartSheetInterface(smartsheet_access_token)
# Initialize DynamoDB interface
dynamodb = ssdbsync.DynamoDBInterface()
# Main
def main():
data = smartsheet.extract_data(SHEET_ID)
dynamodb.update_table(SHEET_ID, data)
# AWS Lambda Function Handler
def handler(event, context):
try:
main()
except Exception as e:
logger.critical(e, exc_info=True)
return {"statusCode": 500}
else:
return {"statusCode": 200}
# Running the script on a developer workstation
if __name__ == "__main__":
ssdbsync.enable_console_logging()
try:
main()
except Exception as e:
logger.critical(e, exc_info=True)
sys.exit(1)
else:
sys.exit(0)