Class: Jarbs::Lambda
- Inherits:
-
Object
- Object
- Jarbs::Lambda
- Includes:
- Commander::UI, CrashReporter::DSL
- Defined in:
- lib/jarbs/lambda.rb
Instance Attribute Summary collapse
-
#function ⇒ Object
Returns the value of attribute function.
Instance Method Summary collapse
- #create ⇒ Object
- #delete ⇒ Object
- #deploy ⇒ Object
- #deployed? ⇒ Boolean
- #exists? ⇒ Boolean
- #generate ⇒ Object
- #info ⇒ Object
-
#initialize(name, options) ⇒ Lambda
constructor
A new instance of Lambda.
- #invoke(payload = "") ⇒ Object
- #prepare ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize(name, options) ⇒ Lambda
Returns a new instance of Lambda.
18 19 20 21 22 23 24 25 |
# File 'lib/jarbs/lambda.rb', line 18 def initialize(name, ) @options = @function = FunctionDefinition.new(name, @options.env) credentials = Aws::SharedCredentials.new(profile_name: @options.profile) @client = Aws::Lambda::Client.new region: default_region, credentials: credentials end |
Instance Attribute Details
#function ⇒ Object
Returns the value of attribute function.
16 17 18 |
# File 'lib/jarbs/lambda.rb', line 16 def function @function end |
Instance Method Details
#create ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/jarbs/lambda.rb', line 44 def create role = @options[:role] || ask("IAM role for function: ") say "Creating #{@function.env_name} on Lambda..." capture_errors do @client.create_function function_name: @function.env_name, runtime: 'nodejs', handler: 'index.handler', role: role, memory_size: 128, timeout: 10, code: { zip_file: @package } end say_ok "Complete!" end |
#delete ⇒ Object
73 74 75 76 |
# File 'lib/jarbs/lambda.rb', line 73 def delete res = @client.delete_function function_name: @function.env_name say_ok "Removed #{@function.env_name}." if res.successful? end |
#deploy ⇒ Object
40 41 42 |
# File 'lib/jarbs/lambda.rb', line 40 def deploy deployed? ? update : create end |
#deployed? ⇒ Boolean
101 102 103 104 105 |
# File 'lib/jarbs/lambda.rb', line 101 def deployed? true if info rescue Aws::Lambda::Errors::ResourceNotFoundException => e false end |
#exists? ⇒ Boolean
107 108 109 |
# File 'lib/jarbs/lambda.rb', line 107 def exists? Dir.exists? function.source_path end |
#generate ⇒ Object
27 28 29 |
# File 'lib/jarbs/lambda.rb', line 27 def generate FunctionGenerator.new(@function).generate end |
#info ⇒ Object
78 79 80 |
# File 'lib/jarbs/lambda.rb', line 78 def info @client.get_function(function_name: @function.env_name) end |
#invoke(payload = "") ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/jarbs/lambda.rb', line 82 def invoke(payload="") payload = JSON.generate(payload) unless payload.is_a? String say "Invoking with:\n\n#{payload}\n\n" capture_errors do resp = @client.invoke function_name: @function.env_name, log_type: 'Tail', payload: payload if resp.successful? say_ok Base64.decode64(resp.data.log_result) else say_error resp.error. end end end |
#prepare ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/jarbs/lambda.rb', line 31 def prepare node = NodeBuild.new(@function) node.npm_build @package = Packager.new(@function).package ensure node.clean end |
#update ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/jarbs/lambda.rb', line 62 def update say "Updating #{@function.env_name} on Lambda..." capture_errors do @client.update_function_code function_name: @function.env_name, zip_file: @package end say_ok "Complete!" end |