Class: Chef::Handler::Sns

Inherits:
Chef::Handler show all
Includes:
Config
Defined in:
lib/chef/handler/sns.rb,
lib/chef/handler/sns/config.rb,
lib/chef/handler/sns/version.rb,
lib/chef/handler/sns/config/ohai.rb

Overview

Chef Handler SNS main class.

A simple Chef report handler that reports status of a Chef run through [Amazon SNS](aws.amazon.com/sns/), [including IAM roles support](#usage-with-amazon-iam-roles).

Defined Under Namespace

Modules: Config

Constant Summary collapse

VERSION =

chef-handler-sns Ruby Gem version.

'2.1.0'.freeze

Constants included from Config

Config::REQUIRED

Instance Method Summary collapse

Methods included from Config

#access_key, #body_template, #config_check, #config_from_ohai, #config_init, #filter_opsworks_activity, #message_structure, #region, #secret_key, #subject, #token, #topic_arn

Constructor Details

#initialize(config = {}) ⇒ Sns

Constructs a new ‘Sns` object.

Examples:

‘/etc/chef/client.rb` Configuration Example

require 'chef/handler/sns'
sns_handler = Chef::Handler::Sns.new
sns_handler.access_key '***AMAZON-KEY***'
sns_handler.secret_key '***AMAZON-SECRET***'
sns_handler.topic_arn 'arn:aws:sns:***'
sns_handler.region 'us-east-1' # optional
exception_handlers << sns_handler

‘/etc/chef/client.rb` Example Using a Hash for Configuration

require 'chef/handler/sns'
exception_handlers << Chef::Handler::Sns.new(
  access_key: '***AMAZON-KEY***',
  secret_key: '***AMAZON-SECRET***',
  topic_arn: 'arn:aws:sns:***',
  region: 'us-east-1' # optional
)

‘/etc/chef/client.rb` Using IAM Roles

require 'chef/handler/sns'
exception_handlers << Chef::Handler::Sns.new(
  topic_arn: 'arn:aws:sns:us-east-1:12341234:MyTopicName'
)

Using the ‘chef_handler` Cookbook

# Install the `chef-handler-sns` RubyGem during the compile phase
chef_gem 'chef-handler-sns' do
  compile_time true # Only for Chef 12
end
# Then activate the handler with the `chef_handler` LWRP
chef_handler 'Chef::Handler::Sns' do
  source 'chef/handler/sns'
  arguments(
    access_key: '***AMAZON-KEY***',
    secret_key: '***AMAZON-SECRET***',
    topic_arn: 'arn:aws:sns:***'
  )
  supports exception: true
  action :enable
end

Using the ‘chef-client` Cookbook

node.default['chef_client']['config']['exception_handlers'] = [{
  'class' => 'Chef::Handler::Sns',
  'arguments' => {
    access_key: '***AMAZON-KEY***',
    secret_key: '***AMAZON-SECRET***',
    topic_arn: 'arn:aws:sns:***'
  }.map { |k, v| "#{k}: #{v.inspect}" }
}]

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options.

Options Hash (config):

  • :access_key (String)

    AWS access key (required, but will try to read it from Ohai with IAM roles).

  • :secret_key (String)

    AWS secret key (required, but will try to read it from Ohai with IAM roles).

  • :token (String)

    AWS security token (optional, read from Ohai with IAM roles). Set to ‘false` to disable the token detected by Ohai.

  • :topic_arn (String)

    AWS topic ARN name (required).

  • :region (String)

    AWS region (optional).

  • :subject (String)

    Message subject string in erubis format (optional).

  • :body_template (String)

    Full path of an erubis template file to use for the message body (optional).

  • :filter_opsworks_activities (Array)

    An array of OpsWorks activities to be triggered with (optional). When set, everything else will be discarded.



119
120
121
122
# File 'lib/chef/handler/sns.rb', line 119

def initialize(config = {})
  Chef::Log.debug("#{self.class} initialized.")
  config_init(config)
end

Instance Method Details

#reportObject

Send a SNS report message.

This is called by Chef internally.

Returns:

  • void



133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/handler/sns.rb', line 133

def report
  config_check(node)
  return unless allow_publish(node)
  sns.publish(
    topic_arn: topic_arn,
    message: sns_body,
    subject: sns_subject,
    message_structure: message_structure
  )
end