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.
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 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 36 def initialize() defaults = { description: 'Deployed with LambdaWrap', subnet_ids: [], security_group_ids: [], timeout: 30, memory_size: 128, delete_unreferenced_versions: true } = .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 case [:runtime] when 'nodejs' then raise ArgumentError, 'AWS Lambda Runtime NodeJS v0.10.42 is deprecated as of April 2017. \ Please see: https://forums.aws.amazon.com/ann.jspa?annID=4142' when 'nodejs4.3', 'nodejs6.10', 'java8', 'python2.7', 'python3.6', 'dotnetcore1.0', 'nodejs4.3-edge' @runtime = [:runtime] else raise ArgumentError, "Invalid Runtime specified: #{[:runtime]}. Only accepts: \ nodejs4.3, nodejs6.10, java8, python2.7, python3.6, dotnetcore1.0, or nodejs4.3-edge" end @description = [:description] @timeout = [:timeout] 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 @delete_unreferenced_versions = [:delete_unreferenced_versions] end |
Instance Method Details
#delete(client, region = 'AWS_REGION') ⇒ Object
Deletes the Lambda Object with associated versions, code, configuration, and aliases.
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 152 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.
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 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 109 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.
141 142 143 144 145 146 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 141 def teardown(, client, region = 'AWS_REGION') super remove_alias(.name) cleanup_unused_versions if @delete_unreferenced_versions true end |
#to_s ⇒ Object
166 167 168 169 |
# File 'lib/lambda_wrap/lambda_manager.rb', line 166 def to_s return @lambda_name if @lambda_name && @lambda_name.is_a?(String) super end |