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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lamma/function.rb', line 15

def initialize(yaml_path)
  path = Pathname.new(yaml_path)
  @id = path.basename.sub_ext('').to_s
  @root_path = path.parent
  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 ValidationError.new('region must be set.') }
  @kms_key_arn = @conf.fetch('kms_key_arn', nil)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/lamma/function.rb', line 12

def name
  @name
end

#publishObject

Returns the value of attribute publish.



13
14
15
# File 'lib/lamma/function.rb', line 13

def publish
  @publish
end

#regionObject (readonly)

Returns the value of attribute region.



12
13
14
# File 'lib/lamma/function.rb', line 12

def region
  @region
end

Instance Method Details

#aliasesObject



96
97
98
99
100
101
102
# File 'lib/lamma/function.rb', line 96

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

#codeObject



34
35
36
# File 'lib/lamma/function.rb', line 34

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

#createObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/lamma/function.rb', line 38

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(resp)
end

#publish_version(v_desc, validate = nil) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/lamma/function.rb', line 110

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_exists?Boolean

Returns:

  • (Boolean)


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

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

#updateObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/lamma/function.rb', line 59

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



88
89
90
91
92
93
94
# File 'lib/lamma/function.rb', line 88

def update_or_create
  if remote_exists?
    update
  else
    create
  end
end

#versionsObject



104
105
106
107
108
# File 'lib/lamma/function.rb', line 104

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