Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
uoy-faculty_aws / lib / faculty_aws / notify_devs.rb
Size: Mime:
# frozen_string_literal: true

require 'aws-sdk-sns'

module FacultyAWS
  # Module to notify devs of errors etc and dump the log links to email
  module NotifyDevs
    module_function

    def log_group
      ENV.fetch('AWS_LAMBDA_LOG_GROUP_NAME', nil)
    end

    def log_stream
      ENV.fetch('AWS_LAMBDA_LOG_STREAM_NAME', nil)
    end

    def region
      ENV.fetch('AWS_REGION', nil)
    end

    def logs_url
      url_log_group = URI.encode_www_form_component(log_group)
      url_log_stream = URI.encode_www_form_component(log_stream)
      URI::HTTPS.build(
        host: "#{region}.console.aws.amazon.com",
        path: '/cloudwatch/home',
        query: "region=#{region}",
        fragment: "logsV2:log-groups/log-group/#{url_log_group}/log-events/#{url_log_stream}"
      ).to_s
    end

    def logs_cli_options
      "--log-group-name '#{log_group}' --log-stream-name '#{log_stream}'"
    end

    def message
      <<~MESSAGE_BODY
        Region: #{region}
        Lambda: #{ENV.fetch('AWS_LAMBDA_FUNCTION_NAME', nil)}
        Handler: #{ENV.fetch('_HANDLER', nil)}

        Log group: #{log_group}
        Log stream: #{log_stream}

        Web console link: #{logs_url}
        CLI parameters: #{logs_cli_options}
      MESSAGE_BODY
    end

    def send_dev_message(subject)
      Aws::SNS::Client.new.publish(
        topic_arn: ENV.fetch('SNS_TOPIC', nil),
        message: message,
        subject: subject
      )
    end

    def send_error_warning
      send_dev_message "Error: #{ENV.fetch('AWS_LAMBDA_FUNCTION_NAME', nil)} lambda threw an error"
    end

    def send_lambda_failed
      send_dev_message "Error: #{ENV.fetch('AWS_LAMBDA_FUNCTION_NAME', nil)} lambda failed"
    end
  end
end