-
Notifications
You must be signed in to change notification settings - Fork 17
/
lambda_function_postgres.py
76 lines (63 loc) · 2.75 KB
/
lambda_function_postgres.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
###############################################################################
# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this
# software and associated documentation files (the "Software"), to deal in the Software
# without restriction, including without limitation the rights to use, copy, modify,
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
#
# For more information, see:
# https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html
#
# Prerequisites:
# - Required permissions to Secrets Manager and RDS Data API
# - Latest boto3 SDK in local package. Run the following in Cloud9 shell:
# - `pip install boto3 -t boto3`
#
###############################################################################
import os
import os.path
import sys
# Use latest boto3 from local environment
LAMBDA_TASK_ROOT = os.environ["LAMBDA_TASK_ROOT"]
sys.path.insert(0, LAMBDA_TASK_ROOT+"/boto3")
# Required imports
import botocore
import boto3
# Imports for your app
from datetime import datetime
# Update your cluster and secret ARNs
cluster_arn = 'arn:aws:rds:us-east-1:0000000000:cluster:my-cluster'
secret_arn = 'arn:aws:secretsmanager:us-east-1:0000000000:secret:my-secret'
def lambda_handler(event, context):
if 'Records' not in event or 'Sns' not in event['Records'][0]:
print('Not an SNS event!')
print(str(event))
return
for record in event['Records']:
call_rds_data_api(record['Sns']['Timestamp'], record['Sns']['Message'])
def call_rds_data_api(timestamp, message):
rds_data = boto3.client('rds-data')
sql = """
INSERT INTO sample_table(received_at, message)
VALUES(TO_TIMESTAMP(:time, 'YYYY-MM-DD HH24:MI:SS'), :message)
"""
param1 = {'name':'time', 'value':{'stringValue': timestamp}}
param2 = {'name':'message', 'value':{'stringValue': message}}
param_set = [param1, param2]
response = rds_data.execute_statement(
resourceArn = cluster_arn,
secretArn = secret_arn,
database = 'tutorial',
sql = sql,
parameters = param_set)
print(str(response));