Class: Kybus::AWS::Lambda

Inherits:
Resource show all
Defined in:
lib/kybus/aws/lambda.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#region

Instance Method Summary collapse

Methods inherited from Resource

#account_id, #with_retries

Constructor Details

#initialize(configs, name) ⇒ Lambda

Returns a new instance of Lambda.



12
13
14
15
16
17
18
19
# File 'lib/kybus/aws/lambda.rb', line 12

def initialize(configs, name)
  super(configs)
  @name = name
  @layers = configs['layers']
  @triggers = configs['triggers']
  @layer_manager = LayerManager.new(lambda_client, function_name)
  @trigger_manager = LambdaTrigger.new(lambda_client, function_name, @triggers)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/kybus/aws/lambda.rb', line 10

def name
  @name
end

Instance Method Details

#create_lambda!(layer_arns) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/kybus/aws/lambda.rb', line 93

def create_lambda!(layer_arns)
  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    puts "Creating function #{function_name} with role: #{"arn:aws:iam::#{account_id}:role/#{function_name}"}"
    lambda_client.create_function(
      function_name:,
      runtime: 'ruby3.3',
      role: "arn:aws:iam::#{account_id}:role/#{function_name}",
      handler: @config['handler'] || 'handler.lambda_handler',
      layers: layer_arns,
      code: { zip_file: File.read('.kybuscode.zip') },
      timeout: @config['timeout'] || 3,
      environment: { variables: { 'SECRET_TOKEN' => @config['secret_token'] } }
    )
    puts "Lambda function '#{function_name}' created."
  end
end

#create_or_update!Object



110
111
112
113
# File 'lib/kybus/aws/lambda.rb', line 110

def create_or_update!
  deploy_lambda!
  @trigger_manager.add_triggers
end

#deploy_lambda!Object



33
34
35
36
37
38
39
40
41
# File 'lib/kybus/aws/lambda.rb', line 33

def deploy_lambda!
  layer_arns = @layers.map { |layer| handle_layer(layer) }

  if lambda_function_exists?
    update_lambda!(layer_arns)
  else
    create_lambda!(layer_arns)
  end
end

#destroy!Object



115
116
117
118
119
120
# File 'lib/kybus/aws/lambda.rb', line 115

def destroy!
  lambda_client.delete_function(function_name:)
  puts "Lambda function '#{function_name}' deleted."
rescue Aws::Lambda::Errors::ResourceNotFoundException
  puts "Lambda function '#{function_name}' not found."
end

#function_nameObject



29
30
31
# File 'lib/kybus/aws/lambda.rb', line 29

def function_name
  @name
end

#handle_layer(layer) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kybus/aws/lambda.rb', line 43

def handle_layer(layer)
  puts "Processing layer:\n#{layer.to_yaml}"
  case layer['type']
  when 'codezip'
    raise 'Checksum file required for codezip layer' unless layer['checksumfile']

    @layer_manager.create_or_update_layer(layer['zipfile'], layer['name'], layer['checksumfile'])
  when 'existing'
    layer_arn = @layer_manager.get_layer_arn(layer['name'])
    raise "Layer #{layer['name']} not found" unless layer_arn

    layer_arn
  else
    raise "Unknown layer type: #{layer['type']}"
  end
end

#lambda_clientObject



21
22
23
# File 'lib/kybus/aws/lambda.rb', line 21

def lambda_client
  @lambda_client ||= Aws::Lambda::Client.new(region: @region)
end

#lambda_function_exists?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
# File 'lib/kybus/aws/lambda.rb', line 60

def lambda_function_exists?
  lambda_client.get_function(function_name:)
  true
rescue Aws::Lambda::Errors::ResourceNotFoundException
  false
end

#update_function_codeObject



84
85
86
87
88
89
90
91
# File 'lib/kybus/aws/lambda.rb', line 84

def update_function_code
  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    lambda_client.update_function_code(
      function_name:,
      zip_file: File.read('.kybuscode.zip')
    )
  end
end

#update_function_configuration(layer_arns) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/kybus/aws/lambda.rb', line 73

def update_function_configuration(layer_arns)
  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    lambda_client.update_function_configuration(
      function_name:,
      layers: layer_arns,
      timeout: @config['timeout'] || 3,
      environment: { variables: { 'SECRET_TOKEN' => @config['secret_token'] } }
    )
  end
end

#update_lambda!(layer_arns) ⇒ Object



67
68
69
70
71
# File 'lib/kybus/aws/lambda.rb', line 67

def update_lambda!(layer_arns)
  update_function_configuration(layer_arns)
  update_function_code
  puts "Lambda function '#{function_name}' updated."
end

#urlObject



25
26
27
# File 'lib/kybus/aws/lambda.rb', line 25

def url
  @trigger_manager.url
end