-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.py
65 lines (51 loc) · 1.49 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
handler.py
author: matt cliff
created: july 11, 2018
AWS lambda python 3.6 code
Lambda function will sync two S3 repos;
expected environment variaoles:
SOURCE_BUCKET
TARGET_BUCKET
"""
import os
import logging
LOGGER = logging.getLogger()
LOGGER.setLevel(logging.INFO)
from awscli.clidriver import create_clidriver
"""
from https://github.com/boto/boto3/issues/358
expects to have awscli installed `pip install awscli`
"""
def aws_cli(*cmd):
old_env = dict(os.environ)
try:
# Environment
env = os.environ.copy()
env['LC_CTYPE'] = u'en_US.UTF'
os.environ.update(env)
# Run awscli in the same process
exit_code = create_clidriver().main(*cmd)
# Deal with problems
if exit_code > 0:
raise RuntimeError('AWS CLI exited with code {}'.format(exit_code))
finally:
os.environ.clear()
os.environ.update(old_env)
def handle_service(event, context):
"""
used for debugging
"""
logging.info("debug: event: %s", event)
source = os.environ['SOURCE_BUCKET']
target = os.environ['TARGET_BUCKET']
logging.info("bucket properties source: %s, target: %s", source, target)
aws_cli(['s3', 'sync', source, target, '--delete'])
#operation = event['httpMethod']
#data = event['queryStringParameters'] if operation == 'GET' else json.loads(event['body'])
return {
"body": json.dumps(event),
"headers": {
"Content-Type" : "application/json",
}
}