Class: Jets::Resource::Lambda::Function

Inherits:
Base
  • Object
show all
Includes:
Environment
Defined in:
lib/jets/resource/lambda/function.rb,
lib/jets/resource/lambda/function/environment.rb

Defined Under Namespace

Modules: Environment

Instance Method Summary collapse

Methods included from Environment

#env_properties, #environment, #jets_env

Methods inherited from Base

#resource

Constructor Details

#initialize(task) ⇒ Function

Returns a new instance of Function.



6
7
8
9
# File 'lib/jets/resource/lambda/function.rb', line 6

def initialize(task)
  @task = task
  @app_class = task.class_name.to_s
end

Instance Method Details

#class_propertiesObject

Class properties example:

class PostsController < ApplicationController
  class_timeout 22
  ...
end

Also handles iam policy override at the class level. Example:

class_iam_policy("logs:*")


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jets/resource/lambda/function.rb', line 71

def class_properties
  # klass is PostsController, HardJob, GameRule, Hello or HelloFunction
  klass = Jets::Klass.from_task(@task)

  class_properties = lookup_class_properties(klass)
  if klass.build_class_iam?
    iam_policy = Jets::Resource::Iam::ClassRole.new(klass)
    class_properties[:role] = "!GetAtt #{iam_policy.logical_id}.Arn"
  end

  class_properties
end

#code_s3_keyObject



176
177
178
179
# File 'lib/jets/resource/lambda/function.rb', line 176

def code_s3_key
  checksum = Jets::Builders::Md5.checksums["stage/code"]
  "jets/code/code-#{checksum}.zip" # s3_key
end

#combined_propertiesObject



28
29
30
31
32
33
34
# File 'lib/jets/resource/lambda/function.rb', line 28

def combined_properties
  props = env_properties
    .deep_merge(global_properties)
    .deep_merge(class_properties)
    .deep_merge(function_properties)
  finalize_properties!(props)
end

#default_handlerObject



149
150
151
152
153
154
155
156
# File 'lib/jets/resource/lambda/function.rb', line 149

def default_handler
  map = {
    node: @task.full_handler(:handler), # IE: handlers/controllers/posts/show.handler
    python: @task.full_handler(:lambda_handler), # IE: handlers/controllers/posts/show.lambda_handler
    ruby: handler, # IE: handlers/controllers/posts_controllers.index
  }
  map[@task.lang]
end

#default_runtimeObject



140
141
142
143
144
145
146
147
# File 'lib/jets/resource/lambda/function.rb', line 140

def default_runtime
  map = {
    node: "nodejs8.10",
    python: "python3.6",
    ruby: "ruby2.5",
  }
  map[@task.lang]
end

#definitionObject



11
12
13
14
15
16
17
18
# File 'lib/jets/resource/lambda/function.rb', line 11

def definition
  {
    function_logical_id => {
      type: "AWS::Lambda::Function",
      properties: combined_properties
    }
  }
end

#finalize_properties!(props) ⇒ Object

Properties managed by Jets with merged with finality.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/jets/resource/lambda/function.rb', line 124

def finalize_properties!(props)
  handler = full_handler(props)
  runtime = get_runtime(props)
  managed = {
    function_name: function_name,
    handler: handler,
    runtime: runtime,
  }
  managed[:layers] = ["!Ref GemLayer"] if runtime =~ /^ruby/
  props.merge!(managed)
end

#full_handler(props) ⇒ Object

Ensure that the handler path is normalized.



168
169
170
171
172
173
174
# File 'lib/jets/resource/lambda/function.rb', line 168

def full_handler(props)
  if props[:handler]
    handler_value(props[:handler])
  else
    default_handler
  end
end

#function_logical_idObject



20
21
22
# File 'lib/jets/resource/lambda/function.rb', line 20

def function_logical_id
  "{namespace}_lambda_function".underscore
end

#function_nameObject

Examples:

"#{Jets.config.project_namespace}-sleep_job-perform"
"demo-dev-sleep_job-perform"


184
185
186
187
188
189
190
191
192
193
# File 'lib/jets/resource/lambda/function.rb', line 184

def function_name
  # Example values:
  #   @app_class: admin/pages_controller
  #   @task.meth: index
  #   method: admin/pages_controller
  #   method: admin-pages_controller-index
  method = @app_class.underscore
  method = method.sub('/','-').gsub(/[^0-9a-z\-_]/i, '') + "-#{@task.meth}"
  "#{Jets.config.project_namespace}-#{method}"
end

#function_propertiesObject

Function properties example:

class PostsController < ApplicationController

timeout 18
def index
  ...
end

Also handles iam policy override at the function level. Example:

iam_policy("ec2:*")
def new
  render json: params.merge(action: "new")
end


114
115
116
117
118
119
120
121
# File 'lib/jets/resource/lambda/function.rb', line 114

def function_properties
  properties = @task.properties
  if @task.build_function_iam?
    iam_policy = Jets::Resource::Iam::FunctionRole.new(@task)
    properties[:role] = "!GetAtt #{iam_policy.logical_id}.Arn"
  end
  properties
end

#get_runtime(props) ⇒ Object



136
137
138
# File 'lib/jets/resource/lambda/function.rb', line 136

def get_runtime(props)
  props[:runtime] || default_runtime
end

#global_propertiesObject

Global properties example: jets defaults are in jets/default/application.rb. Your application’s default config/application.rb then get used. Example:

Jets.application.configure do
  config.function = ActiveSupport::OrderedOptions.new
  config.function.timeout = 30
  config.function.runtime = "nodejs8.10"
  config.function.memory_size = 1536
end


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jets/resource/lambda/function.rb', line 46

def global_properties
  baseline = {
    code: {
      s3_bucket: "!Ref S3Bucket",
      s3_key: code_s3_key
    },
    role: "!Ref IamRole",
    environment: { variables: environment },
  }

  appplication_config = Jets.application.config.function.to_h
  baseline.merge(appplication_config)
end

#handlerObject



158
159
160
# File 'lib/jets/resource/lambda/function.rb', line 158

def handler
  handler_value(@task.meth)  # IE: handlers/controllers/posts_controllers.index
end

#handler_value(meth) ⇒ Object

Used for node-shim also



163
164
165
# File 'lib/jets/resource/lambda/function.rb', line 163

def handler_value(meth)
  "handlers/#{@task.type.pluralize}/#{@app_class.underscore}.#{meth}"
end

#lookup_class_properties(klass) ⇒ Object

Accounts for inherited class_properties



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/jets/resource/lambda/function.rb', line 85

def lookup_class_properties(klass)
  all_classes = []
  while klass != Object
    all_classes << klass
    klass = klass.superclass
  end
  class_properties = {}
  # Go back down class heirachry top to down
  all_classes.reverse.each do |k|
    class_properties.merge!(k.class_properties)
  end
  class_properties
end

#replacementsObject



24
25
26
# File 'lib/jets/resource/lambda/function.rb', line 24

def replacements
  @task.replacements # has namespace replacement
end