Class: DPL::Provider::Lambda
- Inherits:
-
DPL::Provider
- Object
- DPL::Provider
- DPL::Provider::Lambda
- Defined in:
- lib/dpl/provider/lambda.rb
Instance Method Summary collapse
- #access_key_id ⇒ Object
- #check_auth ⇒ Object
- #cleanup ⇒ Object
- #create_zip(dest_file_path, src_directory_path, files) ⇒ Object
- #dead_letter_arn ⇒ Object
- #default_description ⇒ Object
- #default_kms_key_arn ⇒ Object
- #default_memory_size ⇒ Object
- #default_module_name ⇒ Object
- #default_runtime ⇒ Object
- #default_timeout ⇒ Object
- #environment_variables ⇒ Object
- #function_tags ⇒ Object
- #function_zip ⇒ Object
- #handler ⇒ Object
- #lambda ⇒ Object
- #lambda_options ⇒ Object
- #needs_key? ⇒ Boolean
- #output_file_path ⇒ Object
- #publish ⇒ Object
- #push_app ⇒ Object
- #random_chars(count = 8) ⇒ Object
- #secret_access_key ⇒ Object
- #split_string_array_to_hash(arr, delimiter = "=") ⇒ Object
- #tracing_mode ⇒ Object
- #uncleanup ⇒ Object
- #vpc_config ⇒ Object
- #zip_directory(dest_file_path, target_directory_path) ⇒ Object
- #zip_file(dest_file_path, target_file_path) ⇒ Object
Instance Method Details
#access_key_id ⇒ Object
14 15 16 |
# File 'lib/dpl/provider/lambda.rb', line 14 def access_key_id [:access_key_id] || context.env['AWS_ACCESS_KEY_ID'] || raise(Error, "missing access_key_id") end |
#check_auth ⇒ Object
169 170 171 |
# File 'lib/dpl/provider/lambda.rb', line 169 def check_auth log "Using Access Key: #{access_key_id[-4..-1].rjust(20, '*')}" end |
#cleanup ⇒ Object
238 239 |
# File 'lib/dpl/provider/lambda.rb', line 238 def cleanup end |
#create_zip(dest_file_path, src_directory_path, files) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/dpl/provider/lambda.rb', line 155 def create_zip(dest_file_path, src_directory_path, files) Zip::File.open(dest_file_path, Zip::File::CREATE) do |zipfile| files.each do |file| zipfile.add(file.sub(src_directory_path + File::SEPARATOR, ''), file) end end dest_file_path end |
#dead_letter_arn ⇒ Object
185 186 187 |
# File 'lib/dpl/provider/lambda.rb', line 185 def dead_letter_arn [:dead_letter_arn] ? { :target_arn => [:dead_letter_arn]} : nil end |
#default_description ⇒ Object
209 210 211 |
# File 'lib/dpl/provider/lambda.rb', line 209 def default_description "Deploy build #{context.env['TRAVIS_BUILD_NUMBER']} to AWS Lambda via Travis CI" end |
#default_kms_key_arn ⇒ Object
193 194 195 |
# File 'lib/dpl/provider/lambda.rb', line 193 def default_kms_key_arn nil end |
#default_memory_size ⇒ Object
213 214 215 |
# File 'lib/dpl/provider/lambda.rb', line 213 def default_memory_size 128 end |
#default_module_name ⇒ Object
217 218 219 |
# File 'lib/dpl/provider/lambda.rb', line 217 def default_module_name 'index' end |
#default_runtime ⇒ Object
201 202 203 |
# File 'lib/dpl/provider/lambda.rb', line 201 def default_runtime 'nodejs' end |
#default_timeout ⇒ Object
205 206 207 |
# File 'lib/dpl/provider/lambda.rb', line 205 def default_timeout 3 # seconds end |
#environment_variables ⇒ Object
181 182 183 |
# File 'lib/dpl/provider/lambda.rb', line 181 def environment_variables [:environment_variables] ? { :variables => split_string_array_to_hash([:environment_variables]) } : nil end |
#function_tags ⇒ Object
197 198 199 |
# File 'lib/dpl/provider/lambda.rb', line 197 def [:function_tags] ? split_string_array_to_hash([:function_tags]) : nil end |
#function_zip ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/dpl/provider/lambda.rb', line 119 def function_zip target_zip_path = File.absolute_path([:zip] || Dir.pwd) dest_file_path = output_file_path if File.directory?(target_zip_path) zip_directory(dest_file_path, target_zip_path) elsif File.file?(target_zip_path) zip_file(dest_file_path, target_zip_path) else error('Invalid zip option. If set, must be path to directory, js file, or a zip file.') end File.new(dest_file_path) end |
#handler ⇒ Object
112 113 114 115 116 117 |
# File 'lib/dpl/provider/lambda.rb', line 112 def handler module_name = [:module_name] || default_module_name handler_name = option(:handler_name) "#{module_name}.#{handler_name}" end |
#lambda ⇒ Object
10 11 12 |
# File 'lib/dpl/provider/lambda.rb', line 10 def lambda @lambda ||= ::Aws::Lambda::Client.new() end |
#lambda_options ⇒ Object
22 23 24 25 26 27 |
# File 'lib/dpl/provider/lambda.rb', line 22 def { region: [:region] || 'us-east-1', credentials: ::Aws::Credentials.new(access_key_id, secret_access_key) } end |
#needs_key? ⇒ Boolean
165 166 167 |
# File 'lib/dpl/provider/lambda.rb', line 165 def needs_key? false end |
#output_file_path ⇒ Object
173 174 175 |
# File 'lib/dpl/provider/lambda.rb', line 173 def output_file_path @output_file_path ||= '/tmp/' + random_chars(8) + '-lambda.zip' end |
#publish ⇒ Object
221 222 223 |
# File 'lib/dpl/provider/lambda.rb', line 221 def publish !![:publish] end |
#push_app ⇒ Object
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 |
# File 'lib/dpl/provider/lambda.rb', line 29 def push_app # The original LambdaPreview client supported create/update in one call # To keep compatibility we try to fetch the function and then decide # whether to update the code or create a new function function_name = [:name] || option(:function_name) begin response = lambda.get_function({function_name: function_name}) log "Function #{function_name} already exists, updating." # Options defined at # https://docs.aws.amazon.com/sdkforruby/api/Aws/Lambda/Client.html#update_function_configuration-instance_method response = lambda.update_function_configuration({ function_name: function_name, description: [:description] || default_description, timeout: [:timeout] || default_timeout, memory_size: [:memory_size] || default_memory_size, role: option(:role), handler: handler, runtime: [:runtime] || default_runtime, vpc_config: vpc_config, environment: environment_variables, dead_letter_config: dead_letter_arn, kms_key_arn: [:kms_key_arn] || default_kms_key_arn, tracing_config: tracing_mode }) log "Updated configuration of function: #{response.function_name}." if log "Add tags to function #{response.function_name}." response = lambda.tag_resource({ resource: response.function_arn, tags: }) end # Options defined at # https://docs.aws.amazon.com/sdkforruby/api/Aws/Lambda/Client.html#update_function_code-instance_method response = lambda.update_function_code({ function_name: [:name] || option(:function_name), zip_file: function_zip, publish: publish }) log "Updated code of function: #{response.function_name}." rescue ::Aws::Lambda::Errors::ResourceNotFoundException log "Function #{function_name} does not exist, creating." # Options defined at # https://docs.aws.amazon.com/lambda/latest/dg/API_CreateFunction.html response = lambda.create_function({ function_name: [:name] || option(:function_name), description: [:description] || default_description, timeout: [:timeout] || default_timeout, memory_size: [:memory_size] || default_memory_size, role: option(:role), handler: handler, code: { zip_file: function_zip, }, runtime: [:runtime] || default_runtime, publish: publish, vpc_config: vpc_config, environment: environment_variables, dead_letter_config: dead_letter_arn, kms_key_arn: [:kms_key_arn] || default_kms_key_arn, tracing_config: tracing_mode, tags: }) log "Created lambda: #{response.function_name}." end rescue ::Aws::Lambda::Errors::ServiceException => exception error(exception.) rescue ::Aws::Lambda::Errors::InvalidParameterValueException => exception error(exception.) rescue ::Aws::Lambda::Errors::ResourceNotFoundException => exception error(exception.) end |
#random_chars(count = 8) ⇒ Object
234 235 236 |
# File 'lib/dpl/provider/lambda.rb', line 234 def random_chars(count=8) (36**(count-1) + rand(36**count - 36**(count-1))).to_s(36) end |
#secret_access_key ⇒ Object
18 19 20 |
# File 'lib/dpl/provider/lambda.rb', line 18 def secret_access_key [:secret_access_key] || context.env['AWS_SECRET_ACCESS_KEY'] || raise(Error, "missing secret_access_key") end |
#split_string_array_to_hash(arr, delimiter = "=") ⇒ Object
225 226 227 228 229 230 231 232 |
# File 'lib/dpl/provider/lambda.rb', line 225 def split_string_array_to_hash(arr, delimiter="=") variables = {} Array(arr).map do |val| keyval = val.split(delimiter) variables[keyval[0]] = keyval[1] end variables end |
#tracing_mode ⇒ Object
189 190 191 |
# File 'lib/dpl/provider/lambda.rb', line 189 def tracing_mode [:tracing_mode] ? { :mode => [:tracing_mode]} : nil end |
#uncleanup ⇒ Object
241 242 |
# File 'lib/dpl/provider/lambda.rb', line 241 def uncleanup end |
#vpc_config ⇒ Object
177 178 179 |
# File 'lib/dpl/provider/lambda.rb', line 177 def vpc_config [:subnet_ids] && [:security_group_ids] ? { :subnet_ids => Array([:subnet_ids]), :security_group_ids => Array([:security_group_ids]) } : nil end |
#zip_directory(dest_file_path, target_directory_path) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/dpl/provider/lambda.rb', line 148 def zip_directory(dest_file_path, target_directory_path) glob_args = Array(File.join(target_directory_path, '**', '**')) glob_args << File::FNM_DOTMATCH if [:dot_match] files = Dir.glob(*glob_args).reject {|ent| File.directory?(ent)} create_zip(dest_file_path, target_directory_path, files) end |
#zip_file(dest_file_path, target_file_path) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/dpl/provider/lambda.rb', line 134 def zip_file(dest_file_path, target_file_path) if File.extname(target_file_path) == '.zip' # Just copy it to the destination right away, since it is already a zip. FileUtils.cp(target_file_path, dest_file_path) dest_file_path else # Zip up the file. src_directory_path = File.dirname(target_file_path) files = [ target_file_path ] create_zip(dest_file_path, src_directory_path, files) end end |