Top Level Namespace

Defined Under Namespace

Modules: Hibernate Classes: CloudWatchEventManager, EC2Manager, HibernateCli, LambdaSetup

Instance Method Summary collapse

Instance Method Details

#lambda_handler(event:, context:) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hibernate/lambda_function/ec2_auto_shutdown_start_function.rb', line 4

def lambda_handler(event:, context:)
  ec2 = Aws::EC2::Client.new

  instance_id = event['instance_id']
  action = event['action']

  begin
    case action
    when 'start'
      ec2.start_instances(instance_ids: [instance_id])
      puts "Started EC2 instance: #{instance_id}"
    when 'stop'
      ec2.stop_instances(instance_ids: [instance_id])
      puts "Stopped EC2 instance: #{instance_id}"
    else
      raise "Invalid action: #{action}. Must be 'start' or 'stop'."
    end
  rescue Aws::EC2::Errors::ServiceError => e
    puts "Error during EC2 action: #{e.message}"
  end
end