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

Includes:
Util::Camelize
Defined in:
lib/jets/stack/main/dsl/lambda.rb

Instance Method Summary collapse

Methods included from Util::Camelize

#camelize

Instance Method Details

#default_runtimesObject



63
64
65
# File 'lib/jets/stack/main/dsl/lambda.rb', line 63

def default_runtimes
  Jets::Cfn::Resource::Lambda::Function.default_runtimes
end

#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
)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/jets/stack/main/dsl/lambda.rb', line 23

def function(id, props={})
  # Required: code, handler, role, runtime Docs: https://amzn.to/2pdot7S
  meth = sanitize_method_name(id)
  class_namespace = self.to_s.underscore.gsub('/','-') # IE: Jets::Domain => jets-domain
  description = "#{self.to_s} #{meth}" # not bother adding extension
  defaults = {
    Code: {
      S3Bucket: "!Ref S3Bucket",
      S3Key: code_s3_key
    },
    Role: "!Ref IamRole",
    Handler: "#{id}.lambda_handler", # default ruby convention
    Timeout: Jets.config.function.timeout,
    MemorySize: Jets.config.function.memory_size,
    EphemeralStorage: Jets.config.function.ephemeral_storage,
    Description: description,
  }

  function_name = "#{Jets.project_namespace}-#{class_namespace}-#{meth}"
  function_name = function_name.size > Jets::Cfn::Resource::Lambda::Function::MAX_FUNCTION_NAME_SIZE ? nil : function_name
  defaults[:FunctionName] = function_name if function_name

  props = defaults.merge(props)
  # shared/functions do not include the GemLayer and no custom runtime support
  props[:Runtime] ||= Jets.ruby_runtime
  props[:Handler] = handler(props[:Handler])

  logical_id = id.to_s.gsub('/','_')
  resource(logical_id, "AWS::Lambda::Function", props)
end

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



67
68
69
70
71
72
# File 'lib/jets/stack/main/dsl/lambda.rb', line 67

def node_function(id, props={})
  meth = sanitize_method_name(id)
  props[:Handler] ||= "#{meth}.handler" # default python convention
  props[:Runtime] ||= default_runtimes[:node]
  function(id, props)
end

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

Usage:

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


78
79
80
81
82
# File 'lib/jets/stack/main/dsl/lambda.rb', line 78

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

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



56
57
58
59
60
61
# File 'lib/jets/stack/main/dsl/lambda.rb', line 56

def python_function(id, props={})
  meth = sanitize_method_name(id)
  props[:Handler] ||= "#{meth}.lambda_handler" # default python convention
  props[:Runtime] ||= default_runtimes[:python]
  function(id, props)
end