Skip to content

Commit e0df139

Browse files
giom-laknysh
authored andcommitted
Fix/sns (#11)
* add permission to publish to sns topic * fix issue #10 * fix issue #9 * fix terraform fmt
1 parent b48ad30 commit e0df139

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

lambda/es-cleanup.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,17 @@ def send_error(self, msg):
140140
Returns:
141141
None
142142
"""
143-
_msg = "[%s][%s] %s" % (self.name, self.cur_account, msg)
144-
print(_msg)
143+
145144
if self.cfg["sns_arn"] != "":
146-
sns_region = self.cfg["sns_arn"].split(":")[4]
145+
cur_account = self.cfg["sns_arn"].split(":")[4]
146+
_msg = "[%s][%s] %s" % (self.name, self.cur_account, msg)
147+
print(_msg)
148+
sns_region = self.cfg["sns_arn"].split(":")[3]
147149
sns = boto3.client("sns", region_name=sns_region)
148150
sns.publish(TopicArn=self.cfg["sns_arn"], Message=_msg)
151+
else:
152+
_msg = "No SNS topic provided. Just printing..."
153+
print(_msg)
149154

150155
def delete_index(self, index_name):
151156
"""ES DELETE specific index
@@ -176,24 +181,27 @@ def lambda_handler(event, context):
176181
None
177182
"""
178183
es = ES_Cleanup(event, context)
179-
# Index cutoff definition, remove older than this date
180-
earliest_to_keep = datetime.date.today() - datetime.timedelta(
181-
days=int(es.cfg["delete_after"]))
182-
for index in es.get_indices():
183-
if index["index"] == ".kibana":
184-
# ignore .kibana index
185-
print("Found .kibana index - ignoring")
186-
continue
187-
188-
idx_name = '-'.join(word for word in index["index"].split("-")[:-1])
189-
idx_date = index["index"].split("-")[-1]
190-
print("Found index: %s - %s" % (idx_name, idx_date))
191-
if idx_name in es.cfg["index"] or "all" in es.cfg["index"]:
192-
193-
if idx_date <= earliest_to_keep.strftime(es.cfg["index_format"]):
194-
print("Deleting index: %s" % index["index"])
195-
es.delete_index(index["index"])
196-
184+
try:
185+
# Index cutoff definition, remove older than this date
186+
earliest_to_keep = datetime.date.today() - datetime.timedelta(
187+
days=int(es.cfg["delete_after"]))
188+
for index in es.get_indices():
189+
if index["index"] == ".kibana":
190+
# ignore .kibana index
191+
print("Found .kibana index - ignoring")
192+
continue
193+
194+
idx_name = '-'.join(word for word in index["index"].split("-")[:-1])
195+
idx_date = index["index"].split("-")[-1]
196+
print("Found index: %s - %s" % (idx_name, idx_date))
197+
if idx_name in es.cfg["index"] or "all" in es.cfg["index"]:
198+
199+
if idx_date <= earliest_to_keep.strftime(es.cfg["index_format"]):
200+
print("Deleting index: %s" % index["index"])
201+
es.delete_index(index["index"])
202+
except Exception as e:
203+
print(str(e))
204+
es.send_error(str(e))
197205

198206
if __name__ == '__main__':
199207
event = {

main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ data "aws_iam_policy_document" "default" {
5757
"${var.es_domain_arn}/*",
5858
]
5959
}
60+
61+
statement {
62+
actions = [
63+
"sns:Publish",
64+
]
65+
66+
effect = "Allow"
67+
68+
resources = [
69+
"${var.sns_arn}",
70+
]
71+
}
6072
}
6173

6274
# Modules

0 commit comments

Comments
 (0)