-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathlambda_basics.rb
More file actions
295 lines (275 loc) · 11.9 KB
/
lambda_basics.rb
File metadata and controls
295 lines (275 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# frozen_string_literal: true
require 'aws-sdk-lambda'
require 'aws-sdk-s3'
require 'aws-sdk-iam'
require 'aws-sdk-cloudwatchlogs'
require 'logger'
require 'json'
require 'zip'
# snippet-start:[ruby.example_code.ruby.LambdaWrapper.full]
# snippet-start:[ruby.example_code.ruby.LambdaWrapper.decl]
class LambdaWrapper
attr_accessor :lambda_client, :cloudwatch_client, :iam_client
def initialize
@lambda_client = Aws::Lambda::Client.new
@cloudwatch_client = Aws::CloudWatchLogs::Client.new(region: 'us-east-1')
@iam_client = Aws::IAM::Client.new(region: 'us-east-1')
@logger = Logger.new($stdout)
@logger.level = Logger::WARN
end
# snippet-end:[ruby.example_code.ruby.LambdaWrapper.decl]
# snippet-start:[ruby.example_code.lambda.setup_iam]
# Get an AWS Identity and Access Management (IAM) role.
#
# @param iam_role_name: The name of the role to retrieve.
# @param action: Whether to create or destroy the IAM apparatus.
# @return: The IAM role.
def manage_iam(iam_role_name, action)
case action
when 'create'
create_iam_role(iam_role_name)
when 'destroy'
destroy_iam_role(iam_role_name)
else
raise "Incorrect action provided. Must provide 'create' or 'destroy'"
end
end
private
def create_iam_role(iam_role_name)
role_policy = {
'Version': '2012-10-17',
'Statement': [
{
'Effect': 'Allow',
'Principal': { 'Service': 'lambda.amazonaws.com' },
'Action': 'sts:AssumeRole'
}
]
}
role = @iam_client.create_role(
role_name: iam_role_name,
assume_role_policy_document: role_policy.to_json
)
@iam_client.attach_role_policy(
{
policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
role_name: iam_role_name
}
)
wait_for_role_to_exist(iam_role_name)
@logger.debug("Successfully created IAM role: #{role['role']['arn']}")
sleep(10)
[role, role_policy.to_json]
end
def destroy_iam_role(iam_role_name)
@iam_client.detach_role_policy(
{
policy_arn: 'arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole',
role_name: iam_role_name
}
)
@iam_client.delete_role(role_name: iam_role_name)
@logger.debug("Detached policy & deleted IAM role: #{iam_role_name}")
end
def wait_for_role_to_exist(iam_role_name)
@iam_client.wait_until(:role_exists, { role_name: iam_role_name }) do |w|
w.max_attempts = 5
w.delay = 5
end
end
# snippet-end:[ruby.example_code.lambda.setup_iam]
# snippet-start:[ruby.example_code.lambda.create_deployment_package]
# Creates a Lambda deployment package in .zip format.
#
# @param source_file: The name of the object, without suffix, for the Lambda file and zip.
# @return: The deployment package.
def create_deployment_package(source_file)
Dir.chdir(File.dirname(__FILE__))
if File.exist?('lambda_function.zip')
File.delete('lambda_function.zip')
@logger.debug('Deleting old zip: lambda_function.zip')
end
Zip::File.open('lambda_function.zip', create: true) do |zipfile|
zipfile.add('lambda_function.rb', "#{source_file}.rb")
end
@logger.debug("Zipping #{source_file}.rb into: lambda_function.zip.")
File.read('lambda_function.zip').to_s
rescue StandardError => e
@logger.error("There was an error creating deployment package:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.create_deployment_package]
# snippet-start:[ruby.example_code.lambda.GetFunction]
# Gets data about a Lambda function.
#
# @param function_name: The name of the function.
# @return response: The function data, or nil if no such function exists.
def get_function(function_name)
@lambda_client.get_function(
{
function_name: function_name
}
)
rescue Aws::Lambda::Errors::ResourceNotFoundException => e
@logger.debug("Could not find function: #{function_name}:\n #{e.message}")
nil
end
# snippet-end:[ruby.example_code.lambda.GetFunction]
# snippet-start:[ruby.example_code.lambda.CreateFunction]
# Deploys a Lambda function.
#
# @param function_name: The name of the Lambda function.
# @param handler_name: The fully qualified name of the handler function.
# @param role_arn: The IAM role to use for the function.
# @param deployment_package: The deployment package that contains the function code in .zip format.
# @return: The Amazon Resource Name (ARN) of the newly created function.
def create_function(function_name, handler_name, role_arn, deployment_package)
response = @lambda_client.create_function({
role: role_arn.to_s,
function_name: function_name,
handler: handler_name,
runtime: 'ruby2.7',
code: {
zip_file: deployment_package
},
environment: {
variables: {
'LOG_LEVEL' => 'info'
}
}
})
@lambda_client.wait_until(:function_active_v2, { function_name: function_name }) do |w|
w.max_attempts = 5
w.delay = 5
end
response
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error creating #{function_name}:\n #{e.message}")
rescue Aws::Waiters::Errors::WaiterFailed => e
@logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.CreateFunction]
# snippet-start:[ruby.example_code.lambda.Invoke]
# Invokes a Lambda function.
# @param function_name [String] The name of the function to invoke.
# @param payload [nil] Payload containing runtime parameters.
# @return [Object] The response from the function invocation.
def invoke_function(function_name, payload = nil)
params = { function_name: function_name }
params[:payload] = payload unless payload.nil?
@lambda_client.invoke(params)
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error executing #{function_name}:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.Invoke]
# snippet-start:[ruby.example_code.lambda.GetLogs]
# Get function logs from the latest Amazon CloudWatch log stream.
# @param function_name [String] The name of the function.
# @param string_match [String] A string to look for in the logs.
# @return all_logs [Array] An array of all the log messages found for that stream.
def get_cloudwatch_logs(function_name, string_match)
@logger.debug('Enforcing a 10 second sleep to allow CloudWatch logs to appear.')
sleep(10)
streams = @cloudwatch_client.describe_log_streams({ log_group_name: "/aws/lambda/#{function_name}" })
streams['log_streams'].each do |log_stream|
resp = @cloudwatch_client.get_log_events({
log_group_name: "/aws/lambda/#{function_name}",
log_stream_name: log_stream['log_stream_name']
})
resp.events.each do |event|
next unless "/#{event.message}/".match(string_match)
msg = event.message.split(' -- : ')[1].green
puts "CloudWatch log stream: #{msg}"
return msg
end
end
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error fetching CloudWatch logs:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.GetLogs]
def invoke_and_verify(function_name, log_statement, payload = nil)
response = invoke_function(function_name, payload)
print "Done!\n".green
JSON.pretty_generate(response).green
get_cloudwatch_logs(function_name, log_statement)
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error executing #{function_name}:\n #{e.message}")
end
# snippet-start:[ruby.example_code.lambda.UpdateFunctionConfiguration]
# Updates the environment variables for a Lambda function.
# @param function_name: The name of the function to update.
# @param log_level: The log level of the function.
# @return: Data about the update, including the status.
def update_function_configuration(function_name, log_level)
@lambda_client.update_function_configuration({
function_name: function_name,
environment: {
variables: {
'LOG_LEVEL' => log_level
}
}
})
@lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w|
w.max_attempts = 5
w.delay = 5
end
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error updating configurations for #{function_name}:\n #{e.message}")
rescue Aws::Waiters::Errors::WaiterFailed => e
@logger.error("Failed waiting for #{function_name} to activate:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.UpdateFunctionConfiguration]
# snippet-start:[ruby.example_code.lambda.UpdateFunctionCode]
# Updates the code for a Lambda function by submitting a .zip archive that contains
# the code for the function.
#
# @param function_name: The name of the function to update.
# @param deployment_package: The function code to update, packaged as bytes in
# .zip format.
# @return: Data about the update, including the status.
def update_function_code(function_name, deployment_package)
@lambda_client.update_function_code(
function_name: function_name,
zip_file: deployment_package
)
@lambda_client.wait_until(:function_updated_v2, { function_name: function_name }) do |w|
w.max_attempts = 5
w.delay = 5
end
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error updating function code for: #{function_name}:\n #{e.message}")
nil
rescue Aws::Waiters::Errors::WaiterFailed => e
@logger.error("Failed waiting for #{function_name} to update:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.UpdateFunctionCode]
# snippet-start:[ruby.example_code.lambda.ListFunctions]
# Lists the Lambda functions for the current account.
def list_functions
functions = []
@lambda_client.list_functions.each do |response|
response['functions'].each do |function|
functions.append(function['function_name'])
end
end
functions
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error listing functions:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.ListFunctions]
# snippet-start:[ruby.example_code.lambda.DeleteFunction]
# Deletes a Lambda function.
# @param function_name: The name of the function to delete.
def delete_function(function_name)
print "Deleting function: #{function_name}..."
@lambda_client.delete_function(
function_name: function_name
)
print 'Done!'.green
rescue Aws::Lambda::Errors::ServiceException => e
@logger.error("There was an error deleting #{function_name}:\n #{e.message}")
end
# snippet-end:[ruby.example_code.lambda.DeleteFunction]
end
# snippet-end:[ruby.example_code.ruby.LambdaWrapper.full]