-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
74 lines (67 loc) · 2.28 KB
/
lambda_function.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
from __future__ import print_function
import urllib
import boto3
import os.path
import uuid
import sys
sys.path.insert(0, "./lib")
from sftp import *
from base64 import b64decode
host = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['host']))['Plaintext']
remote_path = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['remote_path']))['Plaintext']
username = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['username']))['Plaintext']
password = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['password']))['Plaintext']
s3 = boto3.client('s3')
def object_created(bucket, key):
try:
ssh = SSHConnection(host, username, password)
try:
file='/tmp/'+str(uuid.uuid4())
try:
s3.download_file(bucket, key, file)
except Exception as e:
print(e)
print('ERROR: download')
raise
try:
ssh.put(file, '/'+bucket+'-'+remote_path, key)
os.remove(file)
except Exception as e:
print(e)
print('ERROR: put')
raise
except Exception as e:
print(e)
print('ERROR: params')
raise
except Exception as e:
print(e)
print('ERROR: connection')
raise
ssh.close()
def object_removed(bucket, key):
try:
ssh = SSHConnection(host, username, password)
try:
ssh.remove('/'+bucket+'-'+remote_path, key)
except Exception as e:
print(e)
print('ERROR: params')
raise
except Exception as e:
print(e)
print('ERROR: connection')
raise
ssh.close()
def main (event, context):
#print("Received event: " + json.dumps(event['Records'][0]['eventName'], indent=2))
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
eventName = event['Records'][0]['eventName']
print('Received ['+bucket+'] event')
if "ObjectCreated" in eventName:
object_created(bucket, key)
elif "ObjectRemoved" in eventName:
object_removed(bucket, key)
else:
print('ERROR: Unknown eventName')