Class: LambdaWrap::Lambda
- Inherits:
-
AwsService
- Object
- AwsService
- LambdaWrap::Lambda
- Defined in:
- lib/lambda_wrap/lambda_manager.rb
Overview
Lambda Manager class. Front loads the configuration to the constructor so that the developer can be more declarative with configuration and deployments.
Instance Method Summary collapse
-
#delete(client, region = 'AWS_REGION') ⇒ Object
Deletes the Lambda Object with associated versions, code, configuration, and aliases.
-
#deploy(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deploys the Lambda to the specified Environment.
-
#initialize(options) ⇒ Lambda
constructor
Initializes a Lambda Manager.
-
#teardown(environment_options, client, region = 'AWS_REGION') ⇒ Object
Tearsdown an Environment.
- #to_s ⇒ Object
Constructor Details
#initialize(options) ⇒ Lambda
Initializes a Lambda Manager. Frontloaded configuration.
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 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 37 def initialize() defaults = { description: 'Deployed with LambdaWrap', subnet_ids: [], security_group_ids: [], timeout: 30, memory_size: 128, delete_unreferenced_versions: true, dead_letter_queue_arn: '' } = .reverse_merge(defaults) unless ([:lambda_name]) && ([:lambda_name].is_a? String) raise ArgumentError, 'lambda_name must be provided (String)!' end @lambda_name = [:lambda_name] unless ([:handler]) && ([:handler].is_a? String) raise ArgumentError, 'handler must be provided (String)!' end @handler = [:handler] unless ([:role_arn]) && ([:role_arn].is_a? String) raise ArgumentError, 'role_arn must be provided (String)!' end @role_arn = [:role_arn] unless ([:path_to_zip_file]) && ([:path_to_zip_file].is_a? String) raise ArgumentError, 'path_to_zip_file must be provided (String)!' end @path_to_zip_file = [:path_to_zip_file] unless ([:runtime]) && ([:runtime].is_a? String) raise ArgumentError, 'runtime must be provided (String)!' end unless SUPPORTED_RUNTIMES.include?([:runtime]) raise ArgumentError, "Invalid Runtime specified: #{[:runtime]}." \ "Only accepts: #{SUPPORTED_RUNTIMES}" end @runtime = [:runtime] unless ([:memory_size] % 64).zero? && ([:memory_size] >= 128) && ([:memory_size] <= 3008) raise ArgumentError, 'Invalid Memory Size.' end @memory_size = [:memory_size] # VPC if [:subnet_ids].empty? ^ [:security_group_ids].empty? raise ArgumentError, 'Must supply values for BOTH Subnet Ids and Security Group ID if VPC is desired.' end unless [:subnet_ids].empty? @vpc_configuration = { subnet_ids: [:subnet_ids], security_group_ids: [:security_group_ids] } end @description = [:description] @timeout = [:timeout] @delete_unreferenced_versions = [:delete_unreferenced_versions] @dead_letter_queue_arn = [:dead_letter_queue_arn] end |
Instance Method Details
#delete(client, region = 'AWS_REGION') ⇒ Object
Deletes the Lambda Object with associated versions, code, configuration, and aliases.
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 146 def delete(client, region = 'AWS_REGION') super puts "Deleting all versions and aliases for Lambda: #{@lambda_name}" lambda_details = retrieve_lambda_details if lambda_details.nil? puts 'No Lambda to delete.' else = { function_name: @lambda_name } @client.delete_function() puts "Lambda #{@lambda_name} and all Versions & Aliases have been deleted." end true end |
#deploy(environment_options, client, region = 'AWS_REGION') ⇒ Object
Deploys the Lambda to the specified Environment. Creates a Lambda Function if one didn’t exist. Updates the Lambda’s configuration, Updates the Lambda’s Code, publishes a new version, and creates an alias that points to the newly published version. If the @delete_unreferenced_versions option is enabled, all Lambda Function versions that don’t have an alias pointing to them will be deleted.
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 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 103 def deploy(, client, region = 'AWS_REGION') super puts "Deploying Lambda: #{@lambda_name} to Environment: #{.name}" unless File.exist?(@path_to_zip_file) raise ArgumentError, "Deployment Package Zip File does not exist: #{@path_to_zip_file}!" end lambda_details = retrieve_lambda_details if lambda_details.nil? function_version = create_lambda else update_lambda_config function_version = update_lambda_code end create_alias(function_version, .name, .description) cleanup_unused_versions if @delete_unreferenced_versions puts "Lambda: #{@lambda_name} successfully deployed!" true end |
#teardown(environment_options, client, region = 'AWS_REGION') ⇒ Object
Tearsdown an Environment. Deletes an alias with the same name as the environment. Deletes Unreferenced Lambda Function Versions if the option was specified.
135 136 137 138 139 140 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 135 def teardown(, client, region = 'AWS_REGION') super remove_alias(.name) cleanup_unused_versions if @delete_unreferenced_versions true end |
#to_s ⇒ Object
160 161 162 163 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 160 def to_s return @lambda_name if @lambda_name && @lambda_name.is_a?(String) super end |