Class: Kybus::AWS::Lambda

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#account_id, #with_retries

Constructor Details

#initialize(configs, name) ⇒ Lambda

Returns a new instance of Lambda.



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

def initialize(configs, name)
  super(configs)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/kybus/aws/lambda.rb', line 17

def name
  @name
end

#urlObject (readonly)

Returns the value of attribute url.



17
18
19
# File 'lib/kybus/aws/lambda.rb', line 17

def url
  @url
end

Instance Method Details

#create_lambda!(layer_arn_deps) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/kybus/aws/lambda.rb', line 101

def create_lambda!(layer_arn_deps)
  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    lambda_client.create_function({
                                    function_name:,
                                    runtime: 'ruby3.3',
                                    role: "arn:aws:iam::#{}:role/#{function_name}",
                                    handler: 'handler.lambda_handler',
                                    layers: [layer_arn_deps],
                                    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_layer(zip_file, layer_name, zipfile_hash) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kybus/aws/lambda.rb', line 48

def create_layer(zip_file, layer_name, zipfile_hash)
  response = lambda_client.publish_layer_version({
                                                   layer_name:,
                                                   content: {
                                                     zip_file: File.read(zip_file)
                                                   },
                                                   description: zipfile_hash,
                                                   compatible_runtimes: ["ruby3.3"]
                                                 })
  puts "Layer '#{layer_name}' created: #{response.layer_version_arn}"
  response.layer_version_arn
end

#create_or_update!Object



153
154
155
156
# File 'lib/kybus/aws/lambda.rb', line 153

def create_or_update!
  deploy_lambda!
  make_public!
end

#create_or_update_layer(zip_file, layer_name) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kybus/aws/lambda.rb', line 61

def create_or_update_layer(zip_file, layer_name)
  layer_exists = begin
    response = lambda_client.list_layer_versions({
      layer_name: layer_name,
      max_items: 1
    })

    response.layer_versions.first
  end

  zipfile_hash = calculate_md5('Gemfile.lock')
  
  if !layer_exists || layer_exists.description != zipfile_hash
    create_layer(zip_file, layer_name, zipfile_hash)
  else
    puts "Layer unmodified: #{layer_name}"
    layer_exists.layer_version_arn
  end
end

#deploy_lambda!Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kybus/aws/lambda.rb', line 32

def deploy_lambda!
  layer_arn_deps = create_or_update_layer('.deps.zip', "#{function_name}-deps")

  function_exists = begin
    lambda_client.get_function(function_name:)
  rescue StandardError
    false
  end

  if function_exists
    update_lambda!(layer_arn_deps)
  else
    create_lambda!(layer_arn_deps)
  end
end

#destroy!Object



158
159
160
161
162
163
# File 'lib/kybus/aws/lambda.rb', line 158

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



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

def function_name
  @name
end

#lambda_clientObject



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

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

#make_public!Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kybus/aws/lambda.rb', line 123

def make_public!
  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    response = lambda_client.create_function_url_config({
                                                          function_name:,
                                                          auth_type: 'NONE'
                                                        })
    puts "Function URL created: #{response.function_url}"
    @url = response.function_url
  rescue Aws::Lambda::Errors::ResourceConflictException
    response = lambda_client.get_function_url_config({
                                                       function_name:
                                                     })
    puts "Function URL exists: #{response.function_url}"
    @url = response.function_url
  end

  begin
    response = lambda_client.add_permission({
                                              function_name:,
                                              statement_id: 'AllowPublicInvoke',
                                              action: 'lambda:InvokeFunctionUrl',
                                              principal: '*',
                                              function_url_auth_type: 'NONE'
                                            })
    puts "Permission added successfully: #{response}"
  rescue Aws::Lambda::Errors::ServiceError => e
    puts "Error adding permission: #{e.message}"
  end
end

#update_lambda!(layer_arn_deps) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kybus/aws/lambda.rb', line 81

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

  with_retries(Aws::Lambda::Errors::ResourceConflictException) do
    lambda_client.update_function_code(function_name:, zip_file: File.read('.kybuscode.zip'))
  end
  puts "Lambda function '#{function_name}' updated."
end