Class: LambdaWrap::ApiGatewayManager

Inherits:
Object
  • Object
show all
Defined in:
lib/lambda_wrap/api_gateway_manager.rb

Overview

Note: The concept of an environment of the LambdaWrap gem matches a stage in AWS ApiGateway terms.

Instance Method Summary collapse

Constructor Details

#initializeApiGatewayManager

The constructor does some basic setup

  • Validating basic AWS configuration

  • Creating the underlying client to interact with the AWS SDK.

  • Defining the temporary path of the api-gateway-importer jar file



16
17
18
19
20
21
22
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 16

def initialize
  # AWS api gateway client
  @client = Aws::APIGateway::Client.new
  # path to apigateway-importer jar
  @jarpath = File.join(Dir.tmpdir, 'aws-apigateway-importer-1.0.3-SNAPSHOT-jar-with-dependencies.jar')
  @versionpath = File.join(Dir.tmpdir, 'aws-apigateway-importer-1.0.3-SNAPSHOT-jar-with-dependencies.s3version')
end

Instance Method Details

#download_apigateway_importer(s3_bucket, s3_key) ⇒ Object

Downloads the aws-apigateway-importer jar from an S3 bucket. This is a workaround since aws-apigateway-importer does not provide a binary. Once a binary is available on the public internet, we’ll start using this instead of requiring users of this gem to upload their custom binary to an S3 bucket.

Arguments

s3_bucket

An S3 bucket from where the aws-apigateway-importer binary can be downloaded.

s3_key

The path (key) to the aws-apigateay-importer binary on the s3 bucket.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 33

def download_apigateway_importer(s3_bucket, s3_key)
  s3 = Aws::S3::Client.new

  # current version
  current_s3_version = File.open(@versionpath, 'rb').read if File.exist?(@versionpath)

  # online s3 version
  desired_s3_version = s3.head_object(bucket: s3_bucket, key: s3_key).version_id

  # compare local with remote version
  if current_s3_version != desired_s3_version || !File.exist?(@jarpath)
    puts "Downloading aws-apigateway-importer jar with S3 version #{desired_s3_version}"
    s3.get_object(response_target: @jarpath, bucket: s3_bucket, key: s3_key)
    File.write(@versionpath, desired_s3_version)
  end
end

#setup_apigateway(api_name, env, swagger_file, api_description = 'Deployed with LambdaWrap', stage_variables = {}, region = ENV['AWS_REGION']) ⇒ Object

Sets up the API gateway by searching whether the API Gateway already exists and updates it with the latest information from the swagger file.

Arguments

api_name

The name of the API to which the swagger file should be applied to.

env

The environment where it should be published (which is matching an API gateway stage)

swagger_file

A handle to a swagger file that should be used by aws-apigateway-importer

api_description

The description of the API to be displayed.

stage_variables

A Hash of stage variables to be deployed with the stage. Adds an ‘environment’ by default.

region

A string representing the region to deploy the API. Defaults to what is set as an environment variable.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 61

def setup_apigateway(api_name, env, swagger_file, api_description = 'Deployed with LambdaWrap',
                     stage_variables = {}, region = ENV['AWS_REGION'])
  # ensure API is created
  api_id = get_existing_rest_api(api_name)
  api_id = setup_apigateway_create_rest_api(api_name, api_description) unless api_id

  # create resources
  setup_apigateway_create_resources(api_id, swagger_file, region)

  # create stages
  stage_variables.store('environment', env)
  create_stages(api_id, env, stage_variables)

  # return URI of created stage
  "https://#{api_id}.execute-api.#{region}.amazonaws.com/#{env}/"
end

#setup_apigateway_by_swagger_file(api_name, local_swagger_file, env, stage_variables = {}) ⇒ Object

Generate or Update the API Gateway by using the swagger file and the SDK importer

Arguments

api_name

API Gateway name

local_swagger_file

Path of the local swagger file

env

Environment identifier

stage_variables

hash of stage variables



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
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 166

def setup_apigateway_by_swagger_file(api_name, local_swagger_file, env, stage_variables = {})
  # If API gateway with the name is already present then update it else create a new one
  api_id = get_existing_rest_api(api_name)
  swagger_file_content = File.read(local_swagger_file)

  gateway_response = nil
  if api_id.nil?
    # Create a new APIGateway
    gateway_response =  @client.import_rest_api(fail_on_warnings: false, body: swagger_file_content)
  else
    # Update the exsiting APIgateway. By Merge the exsisting gateway will be merged with the new
    # one supplied in the Swagger file.
    gateway_response =  @client.put_rest_api(rest_api_id: api_id, mode: 'merge', fail_on_warnings: false,
                                             body: swagger_file_content)
  end

  raise "Failed to create API gateway with name #{api_name}" if gateway_response.nil? && gateway_response.id.nil?

  if api_id.nil?
    puts "Created api gateway #{api_name} having id #{gateway_response.id}"
  else
    puts "Updated api gateway #{api_name} having id #{gateway_response.id}"
  end

  # Deploy the service
  stage_variables.store('environment', env)
  create_stages(gateway_response.id, env, stage_variables)

  service_uri = "https://#{gateway_response.id}.execute-api.#{ENV['AWS_REGION']}.amazonaws.com/#{env}/"
  puts "Service deployed at #{service_uri}"

  service_uri
end

#shutdown_apigateway(api_name, env) ⇒ Object

Shuts down an environment from the API Gateway. This basically deletes the stage from the API Gateway, but does not delete the API Gateway itself.

Argument

api_name

The name of the API where the environment should be shut down.

env

The environment (matching an API Gateway stage) to shutdown.



85
86
87
88
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 85

def shutdown_apigateway(api_name, env)
  api_id = get_existing_rest_api(api_name)
  delete_stage(api_id, env)
end