class Lamma::Function

Attributes

conf[R]

@!attribute [r] name

@return [String]

@!attribute [r] region

@return [String]

@!attribute [r] conf

@return [Hash]

@!attribute [rw] publish

@return [Bool]
name[R]

@!attribute [r] name

@return [String]

@!attribute [r] region

@return [String]

@!attribute [r] conf

@return [Hash]

@!attribute [rw] publish

@return [Bool]
publish[RW]
region[R]

@!attribute [r] name

@return [String]

@!attribute [r] region

@return [String]

@!attribute [r] conf

@return [Hash]

@!attribute [rw] publish

@return [Bool]

Public Class Methods

new(yaml_path) click to toggle source

@param [String] yaml_path

# 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

Public Instance Methods

aliases() click to toggle source
# 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
code() click to toggle source

@return [Lamma::Code]

# File lib/lamma/function.rb, line 45
def code
  @code ||= Code.new(self, @conf.fetch('code', {}))
end
create() click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# 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
runtime() click to toggle source
# 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
update() click to toggle source
# 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_create() click to toggle source
# File lib/lamma/function.rb, line 99
def update_or_create
  if remote_exist?
    update
  else
    create
  end
end
versions() click to toggle source
# File lib/lamma/function.rb, line 115
def versions
  lambda_client.list_versions_by_function({
    function_name: @name
  }).versions
end

Private Instance Methods

dead_letter_config() click to toggle source
# File lib/lamma/function.rb, line 154
def dead_letter_config
  @dead_letter_config ||= DeadLetterConfig.new(@conf.fetch('dead_letter_config', {}))
end
default_handler() click to toggle source
# File lib/lamma/function.rb, line 166
def default_handler
  case runtime.type
  when Runtime::C_SHARP
    raise ValidationError.new('handler must be set for C# runtime')
  when Runtime::JAVA_8
    raise ValidationError.new('handler must be set for Java8 runtime')
  when Runtime::NODE_43
    'index.handler'
  when Runtime::EDGE_NODE_43
    'index.handler'
  when Runtime::PYTHON_27, Runtime::PYTHON_36
    'lambda_function.lambda_handler'
  end
end
environment() click to toggle source
# File lib/lamma/function.rb, line 158
def environment
  @environment ||= Environment.new(@conf.fetch('environment', {}))
end
handler() click to toggle source
# File lib/lamma/function.rb, line 150
def handler
  @conf.fetch('handler', default_handler)
end
lambda_client() click to toggle source
# File lib/lamma/function.rb, line 182
def lambda_client
  @lambda_client ||= Aws::Lambda::Client.new(region: @region)
end
vpc_config() click to toggle source
# File lib/lamma/function.rb, line 162
def vpc_config
  VpcConfig.new(@conf.fetch('vpc_config', {}))
end