-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_publish_timestream_dump_files_list_to_SQS.py
88 lines (70 loc) · 2.47 KB
/
lambda_publish_timestream_dump_files_list_to_SQS.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
83
84
85
86
87
88
# Copyright 2010-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# This file is licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
"""
This lambda published the bucket name and object keys (*.json) to SQS. Each object is published in a separate message.
The bucket name and object key are placed in Record messageAttributes as:
{"bucket": "bucket name", "key": "object key"}
Configuration:
Declare the following environment variables:
:param bool TRACE: True for additional logs
:param str BUCKET_NAME: Destination bucket name
The Role allocated to the Lambda for execution must have the following policies (or less permissive equivalent):
* AWSLambdaBasicExecutionRole
* AmazonSQSFullAccess
* AmazonS3ReadOnlyAccess
"""
import boto3
import os
SQS_URL = os.environ.get("SQS_URL")
if not SQS_URL:
msg = "Missing environment variable 'SQS_URL"
print(msg)
raise RuntimeError(msg)
TRACE = os.environ.get("TRACE", True)
if TRACE in ("true", "True", "TRUE", 1, "Yes", "YES", True):
TRACE = True
else:
TRACE = False
BUCKET = os.environ.get("BUCKET_NAME")
if not BUCKET:
raise Exception("Environment variable BUCKET_NAME missing")
s3 = boto3.client('s3')
sqs = boto3.client('sqs')
def log_me(msg):
if TRACE is True:
print(msg)
def get_formatted_sqs_attributes(key, bucket=BUCKET):
return {
'bucket': {
'StringValue': str(bucket),
'DataType': 'String'
},
'key': {
'StringValue': str(key),
'DataType': 'String'
}
}
def lambda_handler(event, context):
response = s3.list_objects(
Bucket=str(BUCKET),
)
for content in response['Contents']:
if not content['Key'].endswith('.json'):
log_me("Skipping key '{}'".format(content['Key']))
continue
log_me("Publishing: {}".format(content['Key']))
response = sqs.send_message(
QueueUrl=SQS_URL,
MessageBody='See messageAttributes',
DelaySeconds=0,
MessageAttributes=get_formatted_sqs_attributes(content['Key'])
)
log_me(response)