Class: Lamma::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/lamma/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yaml_path) ⇒ Function

Returns a new instance of Function.

Parameters:

  • yaml_path (String)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lamma/function.rb', line 27

def initialize(yaml_path)
  path = Pathname.new(yaml_path)
  yaml = YAML.load(path.open)

  @conf = yaml.fetch('function', {})

  @publish = false
  @name = @conf.fetch('name', nil)
  @role_arn = @conf.fetch('role_arn', nil)
  @description = @conf.fetch('description', nil)
  @timeout = @conf.fetch('timeout', 3)
  @memory_size = @conf.fetch('memory_size', 128)
  # @dead_letter_config = dead_letter_config(@conf)
  @region = @conf.fetch('region') { raise Lamma::ValidationError.new('region must be set.') }
  @kms_key_arn = @conf.fetch('kms_key_arn', nil)
end

Instance Attribute Details

#confObject (readonly)

Returns the value of attribute conf.



23
# File 'lib/lamma/function.rb', line 23

attr_reader :name, :region, :conf

#nameString (readonly)

Returns:

  • (String)


23
24
25
# File 'lib/lamma/function.rb', line 23

def name
  @name
end

#publishBool

Returns:

  • (Bool)


23
# File 'lib/lamma/function.rb', line 23

attr_reader :name, :region, :conf

#regionString (readonly)

Returns:

  • (String)


23
# File 'lib/lamma/function.rb', line 23

attr_reader :name, :region, :conf

Instance Method Details

#aliasesObject



107
108
109
110
111
112
113
# File 'lib/lamma/function.rb', line 107

def aliases
  lambda_client.list_aliases({
    function_name: @name
  }).aliases.map do |a|
    Lamma::Alias.new(self, a.name, a.description)
  end
end

#codeLamma::Code

Returns:



45
46
47
# File 'lib/lamma/function.rb', line 45

def code
  @code ||= Code.new(self, @conf.fetch('code', {}))
end

#createObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lamma/function.rb', line 49

def create
  Lamma.logger.info("Creating new function #{@name}...")

  resp = lambda_client.create_function({
    function_name: @name,
    runtime: runtime.to_s,
    role: @role_arn,
    handler: handler,
    code: code.to_h,
    description: @description,
    timeout: @timeout,
    memory_size: @memory_size,
    publish: publish,
    vpc_config: vpc_config.to_h,
    environment: environment.to_h,
    kms_key_arn: @kms_key_arn
  })

  Lamma.logger.info("Created new function #{resp.function_arn}")
end

#publish_version(v_desc, validate = nil) ⇒ Object



121
122
123
124
125
126
127
128
129
130
# File 'lib/lamma/function.rb', line 121

def publish_version(v_desc, validate=nil)
  Lamma.logger.info("Publishing...")
  resp = lambda_client.publish_version({
    function_name: @name,
    code_sha_256: validate,
    description: v_desc
  })
  Lamma.logger.info("Published $LATEST version as version #{resp.version} of funtion: #{resp.function_arn}")
  resp
end

#remote_exist?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
# File 'lib/lamma/function.rb', line 132

def remote_exist?
  begin
    lambda_client.get_function_configuration({
      function_name: @name,
    })
  rescue Aws::Lambda::Errors::ResourceNotFoundException
    false
  end
end

#runtimeObject



142
143
144
145
146
# File 'lib/lamma/function.rb', line 142

def runtime
  runtime_str = @conf.fetch('runtime') { raise ArgumentError.new('runtime must be set') }

  @runtime ||= Lamma::Runtime.new(runtime_str)
end

#updateObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lamma/function.rb', line 70

def update
  Lamma.logger.info('Updating function configuration...')

  resp = lambda_client.update_function_configuration({
    function_name: @name,
    runtime: runtime.to_s,
    role: @role_arn,
    handler: handler,
    description: @description,
    timeout: @timeout,
    memory_size: @memory_size,
    vpc_config: vpc_config.to_h,
    environment: environment.to_h,
    kms_key_arn: @kms_key_arn
  })

  Lamma.logger.info("Updated configuration for function: #{resp.function_arn}")

  Lamma.logger.info('Updating function code...')

  resp = lambda_client.update_function_code({
    function_name: @name,
    zip_file: code.to_h[:zip_file],
    publish: publish
  })

  Lamma.logger.info("Updated code for function: #{resp.function_arn}")
end

#update_or_createObject



99
100
101
102
103
104
105
# File 'lib/lamma/function.rb', line 99

def update_or_create
  if remote_exist?
    update
  else
    create
  end
end

#versionsObject



115
116
117
118
119
# File 'lib/lamma/function.rb', line 115

def versions
  lambda_client.list_versions_by_function({
    function_name: @name
  }).versions
end