class Balancer::Create

Public Class Methods

new(options) click to toggle source
# File lib/balancer/create.rb, line 9
def initialize(options)
  @options = options
  @name = options[:name]
end

Public Instance Methods

add_tags(arn) click to toggle source
# File lib/balancer/create.rb, line 108
def add_tags(arn)
  params = {
    resource_arns: [arn],
    tags: [{ key: "balancer", value: @name }]
  }
  aws_cli_command("aws elbv2 add-tags", params)
  elb.add_tags(params)
end
create_elb() click to toggle source
# File lib/balancer/create.rb, line 41
def create_elb
  puts "Creating load balancer with params:"
  params = param.create_load_balancer
  params[:security_groups] ||= []
  params[:security_groups] += [@security_group_id]
  params[:security_groups] = params[:security_groups].uniq
  pretty_display(params)
  aws_cli_command("aws elbv2 create-load-balancer", params)
  return if @options[:noop]

  begin
    resp = elb.create_load_balancer(params)
  rescue Exception => e
    puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
    exit 1
  end

  elb = resp.load_balancers.first
  puts "Load balancer created: #{elb.load_balancer_arn}"
  @load_balancer_arn = elb.load_balancer_arn # used later
  puts
end
create_listener() click to toggle source
# File lib/balancer/create.rb, line 83
def create_listener
  puts "Creating listener with params:"
  params = param.create_listener
  params.merge!(
    load_balancer_arn: @load_balancer_arn,
    default_actions: [{type: "forward", target_group_arn: @target_group_arn}]
  )
  pretty_display(params)
  aws_cli_command("aws elbv2 create-listener", params)

  resp = run_with_error_handling do
    elb.create_listener(params)
  end
  listener = resp.listeners.first
  puts "Listener created: #{listener.listener_arn}"
  puts
end
create_target_group() click to toggle source
# File lib/balancer/create.rb, line 64
def create_target_group
  puts "Creating target group with params:"
  params = param.create_target_group
  pretty_display(params)
  aws_cli_command("aws elbv2 create-target-group", params)

  begin
    resp = elb.create_target_group(params)
  rescue Exception => e
    puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
    exit 1
  end
  target_group = resp.target_groups.first
  puts "Target group created: #{target_group.target_group_arn}"
  @target_group_arn = target_group.target_group_arn # used later
  add_tags(@target_group_arn)
  puts
end
elb_exists?() click to toggle source
# File lib/balancer/create.rb, line 32
def elb_exists?
  begin
    resp = elb.describe_load_balancers(names: [@name])
    true
  rescue Aws::ElasticLoadBalancingV2::Errors::LoadBalancerNotFound
    false
  end
end
run() click to toggle source

docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html

# File lib/balancer/create.rb, line 15
def run
  if ENV['TEST'] # ghetto way to for sanity cli specs
    puts "Creating load balancer"
    return
  end

  if elb_exists?
    puts "Load balancer #{@name} already exists"
    return
  end

  @security_group_id = create_security_group
  create_elb
  create_target_group
  create_listener
end
run_with_error_handling() { || ... } click to toggle source
# File lib/balancer/create.rb, line 101
def run_with_error_handling
  yield
rescue Exception => e
  puts "ERROR: #{e.class}: #{e.message}".colorize(:red)
  exit 1
end