Module: Jets::Stack::Main::Dsl::Lambda

Defined in:
lib/jets/stack/main/extensions/lambda.rb

Instance Method Summary collapse

Instance Method Details

#function(id, props = {}) ⇒ Object Also known as: ruby_function, lambda_function

Example:

function(:hello,
  handler: handler("hello.lambda_hander"),
  runtime: "python3.6"
)

Defaults to ruby. So:

function(:hello)

is the same as:

function(:hello,
  handler: handler("hello.hande"),
  runtime: :ruby
)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jets/stack/main/extensions/lambda.rb', line 21

def function(id, props={})
  # Required: code, handler, role, runtime Docs: https://amzn.to/2pdot7S
  meth = id.to_s.underscore
  class_namespace = self.to_s.underscore.gsub('/','-') # IE: Jets::Domain => jets-domain
  function_name = "#{Jets.config.project_namespace}-#{class_namespace}-#{id.to_s.underscore}"
  defaults = {
    function_name: function_name,
    code: {
      s3_bucket: "!Ref S3Bucket",
      s3_key: code_s3_key
    },
    role: "!Ref IamRole",
    handler: "#{meth}.handle", # default ruby convention
    runtime: :ruby,
    timeout: Jets.config.function.timeout,
    memory_size: Jets.config.function.memory_size,
  }
  props = defaults.merge(props)
  props[:runtime] = "ruby2.5" if props[:runtime].to_s == "ruby"
  props[:handler] = handler(props[:handler])

  resource(id, "AWS::Lambda::Function", props)
end

#node_function(id, props = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/jets/stack/main/extensions/lambda.rb', line 54

def node_function(id, props={})
  meth = id.to_s.underscore
  props[:handler] ||= "#{meth}.handler" # default python convention
  props[:runtime] = "nodejs8.10"
  function(id, props)
end

#permission(id, props = {}) ⇒ Object

Usage:

permission(:my_permission, principal: "events.amazonaws.com")


65
66
67
68
69
# File 'lib/jets/stack/main/extensions/lambda.rb', line 65

def permission(id, props={})
  defaults = { action: "lambda:InvokeFunction" }
  props = defaults.merge(props)
  resource(id, "AWS::Lambda::Permission", props)
end

#python_function(id, props = {}) ⇒ Object



47
48
49
50
51
52
# File 'lib/jets/stack/main/extensions/lambda.rb', line 47

def python_function(id, props={})
  meth = id.to_s.underscore
  props[:handler] ||= "#{meth}.lambda_handler" # default python convention
  props[:runtime] = "python3.6"
  function(id, props)
end