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



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

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.



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

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

The region to deploy the API. Defaults to what is set as an environment variable.



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

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

#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.



83
84
85
86
# File 'lib/lambda_wrap/api_gateway_manager.rb', line 83

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