Class: LambdaWrap::Lambda

Inherits:
AwsService show all
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.

Since:

  • 1.0

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Lambda

Initializes a Lambda Manager. Frontloaded configuration.

Parameters:

  • options (Hash)

    The Configuration for the Lambda

Options Hash (options):

  • :lambda_name (String)

    The name you want to assign to the function you are uploading. The function names appear in the console and are returned in the ListFunctions API. Function names are used to specify functions to other AWS Lambda API operations, such as Invoke. Note that the length constraint applies only to the ARN. If you specify only the function name, it is limited to 64 characters in length.

  • :handler (String)

    The function within your code that Lambda calls to begin execution.

  • :role_arn (String)

    The Amazon Resource Name (ARN) of the IAM role that Lambda assumes when it executes your function to access any other Amazon Web Services (AWS) resources.

  • :path_to_zip_file (String)

    The absolute path to the Deployment Package zip file

  • :runtime (String)

    The runtime environment for the Lambda function you are uploading.

  • :description (String) — default: 'Deployed with LambdaWrap'

    A short, user-defined function description. Lambda does not use this value. Assign a meaningful description as you see fit.

  • :timeout (Integer) — default: 30

    The function execution time at which Lambda should terminate the function.

  • :memory_size (Integer) — default: 128

    The amount of memory, in MB, your Lambda function is given. Lambda uses this memory size to infer the amount of CPU and memory allocated to your function. The value must be a multiple of 64MB. Minimum: 128, Maximum: 3008.

  • :subnet_ids (Array<String>) — default: []

    If your Lambda function accesses resources in a VPC, you provide this parameter identifying the list of subnet IDs. These must belong to the same VPC. You must provide at least one security group and one subnet ID to configure VPC access.

  • :security_group_ids (Array<String>) — default: []

    If your Lambda function accesses resources in a VPC, you provide this parameter identifying the list of security group IDs. These must belong to the same VPC. You must provide at least one security group and one subnet ID.

  • :delete_unreferenced_versions (Boolean) — default: true

    Option to delete any Lambda Function Versions upon deployment that do not have an alias pointing to them.

  • :dead_letter_queue_arn (String) — default: ''

    The ARN of the SQS Queue for failed async invocations.

Since:

  • 1.0



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(options)
  defaults = {
    description: 'Deployed with LambdaWrap', subnet_ids: [], security_group_ids: [], timeout: 30, memory_size: 128,
    delete_unreferenced_versions: true, dead_letter_queue_arn: ''
  }
  options_with_defaults = options.reverse_merge(defaults)
  unless (options_with_defaults[:lambda_name]) && (options_with_defaults[:lambda_name].is_a? String)
    raise ArgumentError, 'lambda_name must be provided (String)!'
  end
  @lambda_name = options_with_defaults[:lambda_name]

  unless (options_with_defaults[:handler]) && (options_with_defaults[:handler].is_a? String)
    raise ArgumentError, 'handler must be provided (String)!'
  end
  @handler = options_with_defaults[:handler]

  unless (options_with_defaults[:role_arn]) && (options_with_defaults[:role_arn].is_a? String)
    raise ArgumentError, 'role_arn must be provided (String)!'
  end
  @role_arn = options_with_defaults[:role_arn]

  unless (options_with_defaults[:path_to_zip_file]) && (options_with_defaults[:path_to_zip_file].is_a? String)
    raise ArgumentError, 'path_to_zip_file must be provided (String)!'
  end
  @path_to_zip_file = options_with_defaults[:path_to_zip_file]

  unless (options_with_defaults[:runtime]) && (options_with_defaults[:runtime].is_a? String)
    raise ArgumentError, 'runtime must be provided (String)!'
  end

  unless SUPPORTED_RUNTIMES.include?(options_with_defaults[:runtime])
    raise ArgumentError, "Invalid Runtime specified: #{options_with_defaults[:runtime]}." \
      "Only accepts: #{SUPPORTED_RUNTIMES}"
  end

  @runtime = options_with_defaults[:runtime]

  unless (options_with_defaults[:memory_size] % 64).zero? && (options_with_defaults[:memory_size] >= 128) &&
         (options_with_defaults[:memory_size] <= 3008)
    raise ArgumentError, 'Invalid Memory Size.'
  end
  @memory_size = options_with_defaults[:memory_size]
  # VPC
  if options_with_defaults[:subnet_ids].empty? ^ options_with_defaults[:security_group_ids].empty?
    raise ArgumentError, 'Must supply values for BOTH Subnet Ids and Security Group ID if VPC is desired.'
  end
  unless options_with_defaults[:subnet_ids].empty?
    @vpc_configuration = {
      subnet_ids: options_with_defaults[:subnet_ids],
      security_group_ids: options_with_defaults[:security_group_ids]
    }
  end
  @description = options_with_defaults[:description]
  @timeout = options_with_defaults[:timeout]
  @delete_unreferenced_versions = options_with_defaults[:delete_unreferenced_versions]
  @dead_letter_queue_arn = options_with_defaults[: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.

Parameters:

  • client (Aws::Lambda::Client)

    Client to use with SDK. Should be passed in by the API class.

  • region (String) (defaults to: 'AWS_REGION')

    AWS Region string. Should be passed in by the API class.

Since:

  • 1.0



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
    options = { function_name: @lambda_name }
    @client.delete_function(options)
    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.

Parameters:

  • environment_options (LambdaWrap::Environment)

    The target Environment to deploy

  • client (Aws::Lambda::Client)

    Client to use with SDK. Should be passed in by the API class.

  • region (String) (defaults to: 'AWS_REGION')

    AWS Region string. Should be passed in by the API class.

Since:

  • 1.0



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(environment_options, client, region = 'AWS_REGION')
  super

  puts "Deploying Lambda: #{@lambda_name} to Environment: #{environment_options.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, environment_options.name, environment_options.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.

Parameters:

  • environment_options (LambdaWrap::Environment)

    The target Environment to teardown.

  • client (Aws::Lambda::Client)

    Client to use with SDK. Should be passed in by the API class.

  • region (String) (defaults to: 'AWS_REGION')

    AWS Region string. Should be passed in by the API class.

Since:

  • 1.0



135
136
137
138
139
140
# File 'lib/lambda_wrap/lambda_manager.rb', line 135

def teardown(environment_options, client, region = 'AWS_REGION')
  super
  remove_alias(environment_options.name)
  cleanup_unused_versions if @delete_unreferenced_versions
  true
end

#to_sObject

Since:

  • 1.0



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