Module: MovableInk::AWS::SNS

Included in:
MovableInk::AWS
Defined in:
lib/movable_ink/aws/sns.rb

Instance Method Summary collapse

Instance Method Details

#notify_and_sleep(seconds, error_class) ⇒ Object



25
26
27
28
29
30
# File 'lib/movable_ink/aws/sns.rb', line 25

def notify_and_sleep(seconds, error_class)
  message = "Throttled by AWS. Sleeping #{seconds} seconds, (#{error_class})"
  notify_slack(subject: 'API Throttled', message: message)
  puts message
  sleep seconds
end

#notify_slack(subject:, message:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/movable_ink/aws/sns.rb', line 56

def notify_slack(subject:, message:)
  instance_link = "<https://#{my_region}.console.aws.amazon.com/ec2/v2/home?region=#{my_region}#Instances:search=#{instance_id};sort=instanceId|#{instance_id}>"
  required_info = "Instance: #{instance_link}, `#{private_ipv4}`, `#{my_region}`"

  run_with_backoff do
    sns.publish(topic_arn: sns_slack_topic_arn,
                subject: subject.slice(0,99),
                message: "#{required_info}\n#{message}")
  end
end

#send_alert(source: instance_id, links: [], custom_details: {}, summary:, dedup_key:) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/movable_ink/aws/sns.rb', line 32

def send_alert(
  source: instance_id,
  links: [],
  custom_details: {},
  summary:,
  dedup_key:
)
  run_with_backoff do
    message_json = pd_message_json({
      source: source,
      summary: summary,
      links: links,
      custom_details: custom_details,
      dedup_key: dedup_key,
    })

    sns.publish({
      topic_arn: sns_pagerduty_topic_arn,
      subject: summary,
      message: message_json
    })
  end
end

#sns(region: my_region) ⇒ Object



8
9
10
11
# File 'lib/movable_ink/aws/sns.rb', line 8

def sns(region: my_region)
  @sns_client ||= {}
  @sns_client[region] ||= Aws::SNS::Client.new(region: region)
end

#sns_pagerduty_topic_arnObject



17
18
19
# File 'lib/movable_ink/aws/sns.rb', line 17

def sns_pagerduty_topic_arn
  @sns_pagerduty_topic_arn ||= sns_topics.detect { |topic| topic.topic_arn.include? "pagerduty-custom-alerts" }.topic_arn rescue nil
end

#sns_slack_topic_arnObject



13
14
15
# File 'lib/movable_ink/aws/sns.rb', line 13

def sns_slack_topic_arn
  @sns_slack_topic_arn ||= sns_topics.detect { |topic| topic.topic_arn.include? "slack-aws-alerts" }.topic_arn rescue nil
end

#sns_topicsObject



21
22
23
# File 'lib/movable_ink/aws/sns.rb', line 21

def sns_topics
  @sns_topics ||= run_with_backoff { sns.list_topics.flat_map(&:topics) }
end