Class: Jarbs::Lambda

Inherits:
Object
  • Object
show all
Includes:
Commander::UI, CrashReporter::DSL
Defined in:
lib/jarbs/lambda.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Lambda

Returns a new instance of Lambda.



17
18
19
20
21
22
# File 'lib/jarbs/lambda.rb', line 17

def initialize(name, options)
  @options = options

  @function = FunctionDefinition.new(name, @options.env)
  @client = Aws::Lambda::Client.new region: default_region
end

Instance Attribute Details

#functionObject

Returns the value of attribute function.



15
16
17
# File 'lib/jarbs/lambda.rb', line 15

def function
  @function
end

Instance Method Details

#createObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jarbs/lambda.rb', line 32

def create
  data = prepare_for_aws
  role = @options[:role] || ask("IAM role for function: ")

  say "Creating #{@function.env_name} on Lambda..."

  capture_errors do
    @client.create_function function_name: @function.env_name,
      runtime: 'nodejs',
      handler: 'index.handler',
      role: role,
      memory_size: 128,
      timeout: 10,
      code: { zip_file: data }
  end

  say_ok "Complete!"
end

#deleteObject



63
64
65
66
# File 'lib/jarbs/lambda.rb', line 63

def delete
  res = @client.delete_function function_name: @function.env_name
  say_ok "Removed #{@function.env_name}." if res.successful?
end

#deployObject



28
29
30
# File 'lib/jarbs/lambda.rb', line 28

def deploy
  deployed? ? update : create
end

#deployed?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/jarbs/lambda.rb', line 72

def deployed?
  true if info
rescue Aws::Lambda::Errors::ResourceNotFoundException => e
  false
end

#exists?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/jarbs/lambda.rb', line 78

def exists?
  Dir.exists? function.source_path
end

#generateObject



24
25
26
# File 'lib/jarbs/lambda.rb', line 24

def generate
  FunctionGenerator.new(@function).generate
end

#infoObject



68
69
70
# File 'lib/jarbs/lambda.rb', line 68

def info
  @client.get_function(function_name: @function.env_name)
end

#updateObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/jarbs/lambda.rb', line 51

def update
  data = prepare_for_aws

  say "Updating #{@function.env_name} on Lambda..."

  capture_errors do
    @client.update_function_code function_name: @function.env_name, zip_file: data
  end

  say_ok "Complete!"
end