-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.rb
More file actions
70 lines (65 loc) · 1.79 KB
/
lambda_function.rb
File metadata and controls
70 lines (65 loc) · 1.79 KB
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
#
# # Ekiden: AWS EventBridge to Slack webhook integration
#
# v.20221024
#
require 'aws-sdk-ssm'
require 'json'
require 'time'
require_relative './neko-http'
require_relative './neko-logger'
# Configure logger
NekoLogger.logger = Logger.new($stdout, formatter: proc { |s, d, n, m| "#{s} : #{m}\n" })
L = NekoLogger.logger
lvl = ENV['NEKO_LOG_LEVEL']
if String === lvl && ['DEBUG', 'INFO', 'WARN', 'ERROR'].include?(lvl.upcase)
L.level = eval("Logger::#{lvl.upcase}")
end
EB = Aws::EventBridge::Client.new
EB_EKIDEN_BUS = 'prpl-it-ekiden'
def lambda_handler(event:, context:)
c = config
unless event['detail-type']
L.warn('Invalid Lambda trigger, ignoring')
return
end
if event['detail-type'].start_with?(c[:prefix])
channel = event['detail-type'][c[:prefix].length..-1]
L.debug("channel: #{channel}")
else
L.info('"detail-type" prefix does not match, ignoring')
return
end
url = c[:channels][channel]
data = event['detail']
unless url && data
L.warn('Insufficient data, ignoring')
return
end
r = Neko::HTTP.post_json(url, data)
if r[:code] == 200
L.info('Data sent')
L.debug("Slack: #{r[:message]}; URL: #{url}")
else
L.warn("From Slack: #{r[:code]}; #{r[:message]}")
end
end
def config
if @config_loaded.nil? || (Time.now - @config_loaded > 3600)
L.info('Loading config from SSMPS')
ssm = Aws::SSM::Client.new
rparams = {
path: ENV['EKIDEN_SSMPS_CHANNELS_PATH'],
with_decryption: true,
}
@config = {channels:{}}
ssm.get_parameters_by_path(rparams).parameters.each do |prm|
k = prm[:name].split('/').last
@config[:channels][k] = prm[:value]
end
@config[:prefix] = ENV['EKIDEN_DETAILTYPE_PREFIX']
@config_loaded = Time.now
L.debug("Config loaded #{@config_loaded}")
end
@config
end