Class: Jets::CLI::Lambda::Function

Inherits:
Object
  • Object
show all
Includes:
AwsServices
Defined in:
lib/jets/cli/lambda/function.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

#initialize(function_name) ⇒ Function

Returns a new instance of Function.



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

def initialize(function_name)
  @function_name = function_name
end

Instance Attribute Details

#function_nameObject (readonly) Also known as: name

Returns the value of attribute function_name.



4
5
6
# File 'lib/jets/cli/lambda/function.rb', line 4

def function_name
  @function_name
end

Instance Method Details

#environment_variablesObject

Environment Variables



12
13
14
15
# File 'lib/jets/cli/lambda/function.rb', line 12

def environment_variables
  response = lambda_client.get_function_configuration(function_name: function_name)
  response.environment.variables.sort.to_h
end

#environment_variables=(env_vars) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jets/cli/lambda/function.rb', line 17

def environment_variables=(env_vars)
  current_env = environment_variables

  # Update existing vars and remove vars set to nil
  updated_env = current_env.merge(env_vars.stringify_keys) { |key, old_val, new_val| new_val.nil? ? nil : new_val }
  updated_env.compact!  # Removes all key-value pairs where value is nil

  lambda_client.update_function_configuration(
    function_name: function_name,
    environment: {variables: updated_env}
  )
end

#provisioned_concurrency(qualifier = "live") ⇒ Object



49
50
51
52
# File 'lib/jets/cli/lambda/function.rb', line 49

def provisioned_concurrency(qualifier = "live")
  info = provisioned_concurrency_info(qualifier)
  (info[:status] == "not set") ? nil : info[:requested]
end

#provisioned_concurrency=(concurrency, qualifier = "live") ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jets/cli/lambda/function.rb', line 72

def provisioned_concurrency=(concurrency, qualifier = "live")
  if concurrency.nil? || concurrency == 0
    begin
      lambda_client.delete_provisioned_concurrency_config(
        function_name: function_name,
        qualifier: qualifier
      )
    rescue Aws::Lambda::Errors::ResourceNotFoundException
    end
  else
    lambda_client.put_provisioned_concurrency_config(
      function_name: function_name,
      qualifier: qualifier,
      provisioned_concurrent_executions: concurrency
    )
  end
end

#provisioned_concurrency_info(qualifier = "live") ⇒ Object

Provisioned Concurrency



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jets/cli/lambda/function.rb', line 55

def provisioned_concurrency_info(qualifier = "live")
  response = lambda_client.get_provisioned_concurrency_config(
    function_name: function_name,
    qualifier: qualifier
  )
  {
    requested: response.requested_provisioned_concurrent_executions,
    allocated: response.allocated_provisioned_concurrent_executions,
    status: response.status
  }
rescue Aws::Lambda::Errors::ResourceNotFoundException,
  Aws::Lambda::Errors::ProvisionedConcurrencyConfigNotFoundException
  {
    status: "not set"
  }
end

#provisioned_concurrency_unset?(qualifier = "live") ⇒ Boolean

Check if provisioned concurrency is unset

Returns:

  • (Boolean)


96
97
98
99
100
101
102
# File 'lib/jets/cli/lambda/function.rb', line 96

def provisioned_concurrency_unset?(qualifier = "live")
  pc = provisioned_concurrency(qualifier)
  pc.nil? || (pc[:requested] == 0 && pc[:allocated] == 0)
rescue Aws::Lambda::Errors::ResourceNotFoundException,
  Aws::Lambda::Errors::ProvisionedConcurrencyConfigNotFoundException
  true  # Treat not found as unset
end

#reserved_concurrencyObject

Reserved Concurrency



31
32
33
34
35
36
# File 'lib/jets/cli/lambda/function.rb', line 31

def reserved_concurrency
  response = lambda_client.get_function_concurrency(function_name: function_name)
  response.reserved_concurrent_executions
rescue Aws::Lambda::Errors::ResourceNotFoundException
  nil  # No reserved concurrency set implies no limit
end

#reserved_concurrency=(concurrency) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/jets/cli/lambda/function.rb', line 38

def reserved_concurrency=(concurrency)
  if concurrency.nil?
    lambda_client.delete_function_concurrency(function_name: function_name)
  else
    lambda_client.put_function_concurrency(
      function_name: function_name,
      reserved_concurrent_executions: concurrency
    )
  end
end

#reserved_concurrency_zero?Boolean

Check if reserved concurrency is zero or not set

Returns:

  • (Boolean)


91
92
93
# File 'lib/jets/cli/lambda/function.rb', line 91

def reserved_concurrency_zero?
  reserved_concurrency == 0
end